24
24
import java .sql .Statement ;
25
25
import java .util .Properties ;
26
26
import java .util .concurrent .TimeUnit ;
27
+ import java .util .concurrent .locks .Condition ;
28
+ import java .util .concurrent .locks .Lock ;
27
29
import java .util .concurrent .locks .ReentrantLock ;
28
30
import java .util .logging .Logger ;
29
31
@@ -43,7 +45,6 @@ public class PooledDataSource implements DataSource {
43
45
private static final Log log = LogFactory .getLog (PooledDataSource .class );
44
46
45
47
private final PoolState state = new PoolState (this );
46
- private final ReentrantLock fairLock = new ReentrantLock (true );
47
48
48
49
private final UnpooledDataSource dataSource ;
49
50
@@ -59,6 +60,9 @@ public class PooledDataSource implements DataSource {
59
60
60
61
private int expectedConnectionTypeCode ;
61
62
63
+ private Lock lock = new ReentrantLock ();
64
+ private Condition condition = lock .newCondition ();
65
+
62
66
public PooledDataSource () {
63
67
dataSource = new UnpooledDataSource ();
64
68
}
@@ -331,8 +335,8 @@ public int getPoolPingConnectionsNotUsedFor() {
331
335
* Closes all active and idle connections in the pool.
332
336
*/
333
337
public void forceCloseAll () {
338
+ lock .lock ();
334
339
try {
335
- fairLock .lock ();
336
340
expectedConnectionTypeCode = assembleConnectionTypeCode (dataSource .getUrl (), dataSource .getUsername (), dataSource .getPassword ());
337
341
for (int i = state .activeConnections .size (); i > 0 ; i --) {
338
342
try {
@@ -363,9 +367,7 @@ public void forceCloseAll() {
363
367
}
364
368
}
365
369
} finally {
366
- if (fairLock .isLocked ()) {
367
- fairLock .unlock ();
368
- }
370
+ lock .unlock ();
369
371
}
370
372
if (log .isDebugEnabled ()) {
371
373
log .debug ("PooledDataSource forcefully closed/removed all connections." );
@@ -381,8 +383,9 @@ private int assembleConnectionTypeCode(String url, String username, String passw
381
383
}
382
384
383
385
protected void pushConnection (PooledConnection conn ) throws SQLException {
386
+
387
+ lock .lock ();
384
388
try {
385
- fairLock .lock ();
386
389
state .activeConnections .remove (conn );
387
390
if (conn .isValid ()) {
388
391
if (state .idleConnections .size () < poolMaximumIdleConnections && conn .getConnectionTypeCode () == expectedConnectionTypeCode ) {
@@ -398,9 +401,7 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
398
401
if (log .isDebugEnabled ()) {
399
402
log .debug ("Returned connection " + newConn .getRealHashCode () + " to pool." );
400
403
}
401
- if (fairLock .isLocked ()) {
402
- fairLock .unlock ();
403
- }
404
+ condition .signal ();
404
405
} else {
405
406
state .accumulatedCheckoutTime += conn .getCheckoutTime ();
406
407
if (!conn .getRealConnection ().getAutoCommit ()) {
@@ -419,9 +420,7 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
419
420
state .badConnectionCount ++;
420
421
}
421
422
} finally {
422
- if (fairLock .isLocked ()) {
423
- fairLock .unlock ();
424
- }
423
+ lock .unlock ();
425
424
}
426
425
}
427
426
@@ -432,8 +431,8 @@ private PooledConnection popConnection(String username, String password) throws
432
431
int localBadConnectionCount = 0 ;
433
432
434
433
while (conn == null ) {
434
+ lock .lock ();
435
435
try {
436
- fairLock .lock ();
437
436
if (!state .idleConnections .isEmpty ()) {
438
437
// Pool has available connection
439
438
conn = state .idleConnections .remove (0 );
@@ -491,7 +490,7 @@ private PooledConnection popConnection(String username, String password) throws
491
490
log .debug ("Waiting as long as " + poolTimeToWait + " milliseconds for connection." );
492
491
}
493
492
long wt = System .currentTimeMillis ();
494
- fairLock . tryLock (poolTimeToWait , TimeUnit .MILLISECONDS );
493
+ condition . await (poolTimeToWait , TimeUnit .MILLISECONDS );
495
494
state .accumulatedWaitTime += System .currentTimeMillis () - wt ;
496
495
} catch (InterruptedException e ) {
497
496
// set interrupt flag
@@ -529,9 +528,7 @@ private PooledConnection popConnection(String username, String password) throws
529
528
}
530
529
}
531
530
} finally {
532
- if (fairLock .isLocked ()) {
533
- fairLock .unlock ();
534
- }
531
+ lock .unlock ();
535
532
}
536
533
537
534
}
0 commit comments