39
39
import software .amazon .jdbc .targetdriverdialect .ConnectInfo ;
40
40
import software .amazon .jdbc .targetdriverdialect .TargetDriverDialect ;
41
41
import software .amazon .jdbc .util .Messages ;
42
+ import software .amazon .jdbc .util .Pair ;
42
43
import software .amazon .jdbc .util .PropertyUtils ;
43
44
import software .amazon .jdbc .util .RdsUrlType ;
44
45
import software .amazon .jdbc .util .RdsUtils ;
@@ -61,17 +62,22 @@ public class HikariPooledConnectionProvider implements PooledConnectionProvider,
61
62
});
62
63
63
64
protected static final RdsUtils rdsUtils = new RdsUtils ();
64
- protected static SlidingExpirationCache <PoolKey , HikariDataSource > databasePools =
65
- new SlidingExpirationCache <>(
66
- (hikariDataSource ) -> hikariDataSource .getHikariPoolMXBean ().getActiveConnections () == 0 ,
67
- HikariDataSource ::close
68
- );
69
65
protected static long poolExpirationCheckNanos = TimeUnit .MINUTES .toNanos (30 );
70
66
protected final HikariPoolConfigurator poolConfigurator ;
71
67
protected final HikariPoolMapping poolMapping ;
72
68
protected final AcceptsUrlFunc acceptsUrlFunc ;
73
69
protected final LeastConnectionsHostSelector leastConnectionsHostSelector ;
74
70
71
+ static {
72
+ HikariPoolsHolder .databasePools .setShouldDisposeFunc (
73
+ (hikariDataSource ) -> {
74
+ if (hikariDataSource instanceof HikariDataSource ) {
75
+ return ((HikariDataSource ) hikariDataSource ).getHikariPoolMXBean ().getActiveConnections () == 0 ;
76
+ }
77
+ return true ;
78
+ });
79
+ }
80
+
75
81
/**
76
82
* {@link HikariPooledConnectionProvider} constructor. This class can be passed to
77
83
* {@link ConnectionProviderManager#setConnectionProvider} to enable internal connection pools for
@@ -114,7 +120,7 @@ public HikariPooledConnectionProvider(
114
120
this .poolConfigurator = hikariPoolConfigurator ;
115
121
this .poolMapping = mapping ;
116
122
this .acceptsUrlFunc = null ;
117
- this .leastConnectionsHostSelector = new LeastConnectionsHostSelector (databasePools );
123
+ this .leastConnectionsHostSelector = new LeastConnectionsHostSelector (HikariPoolsHolder . databasePools );
118
124
}
119
125
120
126
/**
@@ -151,8 +157,8 @@ public HikariPooledConnectionProvider(
151
157
this .poolMapping = mapping ;
152
158
this .acceptsUrlFunc = null ;
153
159
poolExpirationCheckNanos = poolExpirationNanos ;
154
- databasePools .setCleanupIntervalNanos (poolCleanupNanos );
155
- this .leastConnectionsHostSelector = new LeastConnectionsHostSelector (databasePools );
160
+ HikariPoolsHolder . databasePools .setCleanupIntervalNanos (poolCleanupNanos );
161
+ this .leastConnectionsHostSelector = new LeastConnectionsHostSelector (HikariPoolsHolder . databasePools );
156
162
}
157
163
158
164
/**
@@ -193,8 +199,8 @@ public HikariPooledConnectionProvider(
193
199
this .poolMapping = mapping ;
194
200
this .acceptsUrlFunc = acceptsUrlFunc ;
195
201
poolExpirationCheckNanos = poolExpirationNanos ;
196
- databasePools .setCleanupIntervalNanos (poolCleanupNanos );
197
- this .leastConnectionsHostSelector = new LeastConnectionsHostSelector (databasePools );
202
+ HikariPoolsHolder . databasePools .setCleanupIntervalNanos (poolCleanupNanos );
203
+ this .leastConnectionsHostSelector = new LeastConnectionsHostSelector (HikariPoolsHolder . databasePools );
198
204
}
199
205
200
206
@@ -272,8 +278,8 @@ public Connection connect(
272
278
final HostSpec finalHostSpec = connectionHostSpec ;
273
279
dialect .prepareConnectProperties (copy , protocol , finalHostSpec );
274
280
275
- final HikariDataSource ds = databasePools .computeIfAbsent (
276
- new PoolKey (hostSpec .getUrl (), getPoolKey (finalHostSpec , copy )),
281
+ final HikariDataSource ds = ( HikariDataSource ) HikariPoolsHolder . databasePools .computeIfAbsent (
282
+ Pair . create (hostSpec .getUrl (), getPoolKey (finalHostSpec , copy )),
277
283
(lambdaPoolKey ) -> createHikariDataSource (protocol , finalHostSpec , copy , targetDriverDialect ),
278
284
poolExpirationCheckNanos
279
285
);
@@ -297,22 +303,7 @@ protected String getPoolKey(HostSpec hostSpec, Properties props) {
297
303
298
304
@ Override
299
305
public void releaseResources () {
300
- databasePools .getEntries ().forEach ((poolKey , pool ) -> {
301
- if (!pool .isClosed ()) {
302
- pool .close ();
303
- }
304
- });
305
- databasePools .clear ();
306
- }
307
-
308
- // For testing purposes
309
- public static void clearCache () {
310
- databasePools .getEntries ().forEach ((poolKey , pool ) -> {
311
- if (!pool .isClosed ()) {
312
- pool .close ();
313
- }
314
- });
315
- databasePools .clear ();
306
+ HikariPoolsHolder .closeAllPools ();
316
307
}
317
308
318
309
/**
@@ -378,7 +369,7 @@ protected void configurePool(
378
369
* @return the number of active connection pools
379
370
*/
380
371
public int getHostCount () {
381
- return databasePools .size ();
372
+ return HikariPoolsHolder . databasePools .size ();
382
373
}
383
374
384
375
/**
@@ -388,8 +379,8 @@ public int getHostCount() {
388
379
*/
389
380
public Set <String > getHosts () {
390
381
return Collections .unmodifiableSet (
391
- databasePools .getEntries ().keySet ().stream ()
392
- .map (poolKey -> poolKey .url )
382
+ HikariPoolsHolder . databasePools .getEntries ().keySet ().stream ()
383
+ .map (poolKey -> ( String ) poolKey .getValue1 () )
393
384
.collect (Collectors .toSet ()));
394
385
}
395
386
@@ -398,8 +389,8 @@ public Set<String> getHosts() {
398
389
*
399
390
* @return a set containing every key associated with an active connection pool
400
391
*/
401
- public Set <PoolKey > getKeys () {
402
- return databasePools .getEntries ().keySet ();
392
+ public Set <Pair > getKeys () {
393
+ return HikariPoolsHolder . databasePools .getEntries ().keySet ();
403
394
}
404
395
405
396
@ Override
@@ -413,7 +404,7 @@ public String getTargetName() {
413
404
public void logConnections () {
414
405
LOGGER .finest (() -> {
415
406
final StringBuilder builder = new StringBuilder ();
416
- databasePools .getEntries ().forEach ((key , dataSource ) -> {
407
+ HikariPoolsHolder . databasePools .getEntries ().forEach ((key , dataSource ) -> {
417
408
builder .append ("\t [ " );
418
409
builder .append (key ).append (":" );
419
410
builder .append ("\n \t {" );
@@ -436,51 +427,8 @@ HikariDataSource createHikariDataSource(
436
427
return new HikariDataSource (config );
437
428
}
438
429
439
- public static class PoolKey {
440
- private final @ NonNull String url ;
441
- private final @ NonNull String extraKey ;
442
-
443
- public PoolKey (final @ NonNull String url , final @ NonNull String extraKey ) {
444
- this .url = url ;
445
- this .extraKey = extraKey ;
446
- }
447
-
448
- public String getUrl () {
449
- return this .url ;
450
- }
451
-
452
- @ Override
453
- public int hashCode () {
454
- final int prime = 31 ;
455
- int result = 1 ;
456
- result = prime * result + ((url == null ) ? 0 : url .hashCode ()) + ((extraKey == null ) ? 0 : extraKey .hashCode ());
457
- return result ;
458
- }
459
-
460
- @ Override
461
- public boolean equals (final Object obj ) {
462
- if (this == obj ) {
463
- return true ;
464
- }
465
- if (obj == null ) {
466
- return false ;
467
- }
468
- if (getClass () != obj .getClass ()) {
469
- return false ;
470
- }
471
- final PoolKey other = (PoolKey ) obj ;
472
- return this .url .equals (other .url ) && this .extraKey .equals (other .extraKey );
473
- }
474
-
475
- @ Override
476
- public String toString () {
477
- return "PoolKey [url=" + url + ", extraKey=" + extraKey + "]" ;
478
- }
479
-
480
- }
481
-
482
430
// For testing purposes only
483
- void setDatabasePools (SlidingExpirationCache <PoolKey , HikariDataSource > connectionPools ) {
484
- databasePools = connectionPools ;
431
+ void setDatabasePools (SlidingExpirationCache <Pair , AutoCloseable > connectionPools ) {
432
+ HikariPoolsHolder . databasePools = connectionPools ;
485
433
}
486
434
}
0 commit comments