Skip to content

Commit 8820b11

Browse files
committed
Restore original behavior
The doc says that fair locks show slower (often much slower) throughput. Considering the nature of this class, higher throughput may be more important than fairness here.
1 parent d6a2d62 commit 8820b11

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.sql.Statement;
2525
import java.util.Properties;
2626
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.locks.Condition;
28+
import java.util.concurrent.locks.Lock;
2729
import java.util.concurrent.locks.ReentrantLock;
2830
import java.util.logging.Logger;
2931

@@ -43,7 +45,6 @@ public class PooledDataSource implements DataSource {
4345
private static final Log log = LogFactory.getLog(PooledDataSource.class);
4446

4547
private final PoolState state = new PoolState(this);
46-
private final ReentrantLock fairLock = new ReentrantLock(true);
4748

4849
private final UnpooledDataSource dataSource;
4950

@@ -59,6 +60,9 @@ public class PooledDataSource implements DataSource {
5960

6061
private int expectedConnectionTypeCode;
6162

63+
private Lock lock = new ReentrantLock();
64+
private Condition condition = lock.newCondition();
65+
6266
public PooledDataSource() {
6367
dataSource = new UnpooledDataSource();
6468
}
@@ -331,8 +335,8 @@ public int getPoolPingConnectionsNotUsedFor() {
331335
* Closes all active and idle connections in the pool.
332336
*/
333337
public void forceCloseAll() {
338+
lock.lock();
334339
try {
335-
fairLock.lock();
336340
expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
337341
for (int i = state.activeConnections.size(); i > 0; i--) {
338342
try {
@@ -363,9 +367,7 @@ public void forceCloseAll() {
363367
}
364368
}
365369
} finally {
366-
if (fairLock.isLocked()) {
367-
fairLock.unlock();
368-
}
370+
lock.unlock();
369371
}
370372
if (log.isDebugEnabled()) {
371373
log.debug("PooledDataSource forcefully closed/removed all connections.");
@@ -381,8 +383,9 @@ private int assembleConnectionTypeCode(String url, String username, String passw
381383
}
382384

383385
protected void pushConnection(PooledConnection conn) throws SQLException {
386+
387+
lock.lock();
384388
try {
385-
fairLock.lock();
386389
state.activeConnections.remove(conn);
387390
if (conn.isValid()) {
388391
if (state.idleConnections.size() < poolMaximumIdleConnections && conn.getConnectionTypeCode() == expectedConnectionTypeCode) {
@@ -398,9 +401,7 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
398401
if (log.isDebugEnabled()) {
399402
log.debug("Returned connection " + newConn.getRealHashCode() + " to pool.");
400403
}
401-
if (fairLock.isLocked()) {
402-
fairLock.unlock();
403-
}
404+
condition.signal();
404405
} else {
405406
state.accumulatedCheckoutTime += conn.getCheckoutTime();
406407
if (!conn.getRealConnection().getAutoCommit()) {
@@ -419,9 +420,7 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
419420
state.badConnectionCount++;
420421
}
421422
} finally {
422-
if (fairLock.isLocked()) {
423-
fairLock.unlock();
424-
}
423+
lock.unlock();
425424
}
426425
}
427426

@@ -432,8 +431,8 @@ private PooledConnection popConnection(String username, String password) throws
432431
int localBadConnectionCount = 0;
433432

434433
while (conn == null) {
434+
lock.lock();
435435
try {
436-
fairLock.lock();
437436
if (!state.idleConnections.isEmpty()) {
438437
// Pool has available connection
439438
conn = state.idleConnections.remove(0);
@@ -491,7 +490,7 @@ private PooledConnection popConnection(String username, String password) throws
491490
log.debug("Waiting as long as " + poolTimeToWait + " milliseconds for connection.");
492491
}
493492
long wt = System.currentTimeMillis();
494-
fairLock.tryLock(poolTimeToWait, TimeUnit.MILLISECONDS);
493+
condition.await(poolTimeToWait, TimeUnit.MILLISECONDS);
495494
state.accumulatedWaitTime += System.currentTimeMillis() - wt;
496495
} catch (InterruptedException e) {
497496
// set interrupt flag
@@ -529,9 +528,7 @@ private PooledConnection popConnection(String username, String password) throws
529528
}
530529
}
531530
} finally {
532-
if (fairLock.isLocked()) {
533-
fairLock.unlock();
534-
}
531+
lock.unlock();
535532
}
536533

537534
}

0 commit comments

Comments
 (0)