1111import java .sql .SQLException ;
1212import java .util .Map ;
1313import java .util .Properties ;
14- import java .util .concurrent .ConcurrentLinkedQueue ;
1514import java .util .concurrent .Executors ;
1615import java .util .concurrent .ScheduledExecutorService ;
1716import java .util .concurrent .TimeUnit ;
1817
1918import org .hibernate .HibernateException ;
2019import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
2120import org .hibernate .cfg .AvailableSettings ;
22- import org .hibernate .cfg .Environment ;
2321import org .hibernate .engine .jdbc .connections .spi .ConnectionProvider ;
2422import org .hibernate .internal .CoreLogging ;
2523import org .hibernate .internal .CoreMessageLogger ;
26- import org .hibernate .internal .util .ReflectHelper ;
2724import org .hibernate .internal .util .config .ConfigurationHelper ;
2825import org .hibernate .service .UnknownUnwrapTypeException ;
2926import org .hibernate .service .spi .Configurable ;
@@ -56,10 +53,9 @@ public class DriverManagerConnectionProviderImpl
5653
5754 private boolean active = true ;
5855
59- private ConcurrentLinkedQueue <Connection > connections = new ConcurrentLinkedQueue <Connection >();
6056 private ConnectionCreator connectionCreator ;
6157 private ScheduledExecutorService executorService ;
62-
58+ private PooledConnections pool ;
6359
6460
6561 // create the pool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -76,54 +72,16 @@ public void configure(Map configurationValues) {
7672 log .usingHibernateBuiltInConnectionPool ();
7773
7874 connectionCreator = buildCreator ( configurationValues );
75+ pool = buildPool ( configurationValues );
7976
80- final int minSize = ConfigurationHelper .getInt ( MIN_SIZE , configurationValues , 1 );
81- final int maxSize = ConfigurationHelper .getInt ( AvailableSettings .POOL_SIZE , configurationValues , 20 );
82- final int initialSize = ConfigurationHelper .getInt ( INITIAL_SIZE , configurationValues , minSize );
8377 final long validationInterval = ConfigurationHelper .getLong ( VALIDATION_INTERVAL , configurationValues , 30 );
84-
85- log .hibernateConnectionPoolSize ( maxSize , minSize );
86-
87- log .debugf ( "Initializing Connection pool with %s Connections" , initialSize );
88- for ( int i = 0 ; i < initialSize ; i ++ ) {
89- connections .add ( connectionCreator .createConnection () );
90- }
91-
9278 executorService = Executors .newSingleThreadScheduledExecutor ();
9379 executorService .scheduleWithFixedDelay (
9480 new Runnable () {
9581 private boolean primed ;
9682 @ Override
9783 public void run () {
98- int size = connections .size ();
99-
100- if ( !primed && size >= minSize ) {
101- // IMPL NOTE : the purpose of primed is to allow the pool to lazily reach its
102- // defined min-size.
103- log .debug ( "Connection pool now considered primed; min-size will be maintained" );
104- primed = true ;
105- }
106-
107- if ( size < minSize && primed ) {
108- int numberToBeAdded = minSize - size ;
109- log .debugf ( "Adding %s Connections to the pool" , numberToBeAdded );
110- for (int i = 0 ; i < numberToBeAdded ; i ++) {
111- connections .add ( connectionCreator .createConnection () );
112- }
113- }
114- else if ( size > maxSize ) {
115- int numberToBeRemoved = size - maxSize ;
116- log .debugf ( "Removing %s Connections from the pool" , numberToBeRemoved );
117- for ( int i = 0 ; i < numberToBeRemoved ; i ++ ) {
118- Connection connection = connections .poll ();
119- try {
120- connection .close ();
121- }
122- catch (SQLException e ) {
123- log .unableToCloseConnection ( e );
124- }
125- }
126- }
84+ pool .validate ();
12785 }
12886 },
12987 validationInterval ,
@@ -132,6 +90,27 @@ else if ( size > maxSize ) {
13290 );
13391 }
13492
93+ private PooledConnections buildPool (Map configurationValues ) {
94+ final boolean autoCommit = ConfigurationHelper .getBoolean (
95+ AvailableSettings .AUTOCOMMIT ,
96+ configurationValues ,
97+ false
98+ );
99+ final int minSize = ConfigurationHelper .getInt ( MIN_SIZE , configurationValues , 1 );
100+ final int maxSize = ConfigurationHelper .getInt ( AvailableSettings .POOL_SIZE , configurationValues , 20 );
101+ final int initialSize = ConfigurationHelper .getInt ( INITIAL_SIZE , configurationValues , minSize );
102+
103+ PooledConnections .Builder pooledConnectionBuilder = new PooledConnections .Builder (
104+ connectionCreator ,
105+ autoCommit
106+ );
107+ pooledConnectionBuilder .initialSize ( initialSize );
108+ pooledConnectionBuilder .minSize ( minSize );
109+ pooledConnectionBuilder .maxSize ( maxSize );
110+
111+ return pooledConnectionBuilder .build ();
112+ }
113+
135114 private ConnectionCreator buildCreator (Map configurationValues ) {
136115 final ConnectionCreatorBuilder connectionCreatorBuilder = new ConnectionCreatorBuilder ( serviceRegistry );
137116
@@ -206,12 +185,11 @@ public Connection getConnection() throws SQLException {
206185 throw new HibernateException ( "Connection pool is no longer active" );
207186 }
208187
209- Connection connection ;
210- if ( ( connection = connections . poll ()) == null ) {
211- connection = connectionCreator .createConnection ();
188+ Connection conn = pool . poll () ;
189+ if ( conn == null ) {
190+ conn = connectionCreator .createConnection ();
212191 }
213-
214- return connection ;
192+ return conn ;
215193 }
216194
217195 @ Override
@@ -220,10 +198,9 @@ public void closeConnection(Connection conn) throws SQLException {
220198 return ;
221199 }
222200
223- this . connections . offer ( conn );
201+ pool . add ( conn );
224202 }
225203
226-
227204 @ Override
228205 public boolean supportsAggressiveRelease () {
229206 return false ;
@@ -265,17 +242,14 @@ public void stop() {
265242 }
266243 executorService = null ;
267244
268- for ( Connection connection : connections ) {
269- try {
270- connection .close ();
271- }
272- catch (SQLException e ) {
273- log .unableToClosePooledConnection ( e );
274- }
245+ try {
246+ pool .close ();
247+ }
248+ catch (SQLException e ) {
249+ log .unableToClosePooledConnection ( e );
275250 }
276251 }
277252
278-
279253 //CHECKSTYLE:START_ALLOW_FINALIZER
280254 @ Override
281255 protected void finalize () throws Throwable {
0 commit comments