11
11
import java .sql .SQLException ;
12
12
import java .util .Map ;
13
13
import java .util .Properties ;
14
- import java .util .concurrent .ConcurrentLinkedQueue ;
15
14
import java .util .concurrent .Executors ;
16
15
import java .util .concurrent .ScheduledExecutorService ;
17
16
import java .util .concurrent .TimeUnit ;
18
17
19
18
import org .hibernate .HibernateException ;
20
19
import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
21
20
import org .hibernate .cfg .AvailableSettings ;
22
- import org .hibernate .cfg .Environment ;
23
21
import org .hibernate .engine .jdbc .connections .spi .ConnectionProvider ;
24
22
import org .hibernate .internal .CoreLogging ;
25
23
import org .hibernate .internal .CoreMessageLogger ;
26
- import org .hibernate .internal .util .ReflectHelper ;
27
24
import org .hibernate .internal .util .config .ConfigurationHelper ;
28
25
import org .hibernate .service .UnknownUnwrapTypeException ;
29
26
import org .hibernate .service .spi .Configurable ;
@@ -56,10 +53,9 @@ public class DriverManagerConnectionProviderImpl
56
53
57
54
private boolean active = true ;
58
55
59
- private ConcurrentLinkedQueue <Connection > connections = new ConcurrentLinkedQueue <Connection >();
60
56
private ConnectionCreator connectionCreator ;
61
57
private ScheduledExecutorService executorService ;
62
-
58
+ private PooledConnections pool ;
63
59
64
60
65
61
// create the pool ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -76,54 +72,16 @@ public void configure(Map configurationValues) {
76
72
log .usingHibernateBuiltInConnectionPool ();
77
73
78
74
connectionCreator = buildCreator ( configurationValues );
75
+ pool = buildPool ( configurationValues );
79
76
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 );
83
77
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
-
92
78
executorService = Executors .newSingleThreadScheduledExecutor ();
93
79
executorService .scheduleWithFixedDelay (
94
80
new Runnable () {
95
81
private boolean primed ;
96
82
@ Override
97
83
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 ();
127
85
}
128
86
},
129
87
validationInterval ,
@@ -132,6 +90,27 @@ else if ( size > maxSize ) {
132
90
);
133
91
}
134
92
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
+
135
114
private ConnectionCreator buildCreator (Map configurationValues ) {
136
115
final ConnectionCreatorBuilder connectionCreatorBuilder = new ConnectionCreatorBuilder ( serviceRegistry );
137
116
@@ -206,12 +185,11 @@ public Connection getConnection() throws SQLException {
206
185
throw new HibernateException ( "Connection pool is no longer active" );
207
186
}
208
187
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 ();
212
191
}
213
-
214
- return connection ;
192
+ return conn ;
215
193
}
216
194
217
195
@ Override
@@ -220,10 +198,9 @@ public void closeConnection(Connection conn) throws SQLException {
220
198
return ;
221
199
}
222
200
223
- this . connections . offer ( conn );
201
+ pool . add ( conn );
224
202
}
225
203
226
-
227
204
@ Override
228
205
public boolean supportsAggressiveRelease () {
229
206
return false ;
@@ -265,17 +242,14 @@ public void stop() {
265
242
}
266
243
executorService = null ;
267
244
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 );
275
250
}
276
251
}
277
252
278
-
279
253
//CHECKSTYLE:START_ALLOW_FINALIZER
280
254
@ Override
281
255
protected void finalize () throws Throwable {
0 commit comments