Skip to content

Commit bb5702e

Browse files
committed
refactor: fairLock
1 parent 44cf870 commit bb5702e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.sql.SQLException;
2424
import java.sql.Statement;
2525
import java.util.Properties;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.locks.ReentrantLock;
2628
import java.util.logging.Logger;
2729

2830
import javax.sql.DataSource;
@@ -41,6 +43,7 @@ public class PooledDataSource implements DataSource {
4143
private static final Log log = LogFactory.getLog(PooledDataSource.class);
4244

4345
private final PoolState state = new PoolState(this);
46+
private final ReentrantLock fairLock = new ReentrantLock(true);
4447

4548
private final UnpooledDataSource dataSource;
4649

@@ -328,6 +331,7 @@ public int getPoolPingConnectionsNotUsedFor() {
328331
* Closes all active and idle connections in the pool.
329332
*/
330333
public void forceCloseAll() {
334+
fairLock.lock();
331335
synchronized (state) {
332336
expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
333337
for (int i = state.activeConnections.size(); i > 0; i--) {
@@ -374,7 +378,8 @@ private int assembleConnectionTypeCode(String url, String username, String passw
374378

375379
protected void pushConnection(PooledConnection conn) throws SQLException {
376380

377-
synchronized (state) {
381+
try {
382+
fairLock.lock();
378383
state.activeConnections.remove(conn);
379384
if (conn.isValid()) {
380385
if (state.idleConnections.size() < poolMaximumIdleConnections && conn.getConnectionTypeCode() == expectedConnectionTypeCode) {
@@ -390,7 +395,7 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
390395
if (log.isDebugEnabled()) {
391396
log.debug("Returned connection " + newConn.getRealHashCode() + " to pool.");
392397
}
393-
state.notifyAll();
398+
fairLock.unlock();
394399
} else {
395400
state.accumulatedCheckoutTime += conn.getCheckoutTime();
396401
if (!conn.getRealConnection().getAutoCommit()) {
@@ -408,6 +413,10 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
408413
}
409414
state.badConnectionCount++;
410415
}
416+
} finally {
417+
if (fairLock.isLocked()) {
418+
fairLock.unlock();
419+
}
411420
}
412421
}
413422

@@ -418,7 +427,8 @@ private PooledConnection popConnection(String username, String password) throws
418427
int localBadConnectionCount = 0;
419428

420429
while (conn == null) {
421-
synchronized (state) {
430+
fairLock.lock();
431+
try {
422432
if (!state.idleConnections.isEmpty()) {
423433
// Pool has available connection
424434
conn = state.idleConnections.remove(0);
@@ -476,7 +486,7 @@ private PooledConnection popConnection(String username, String password) throws
476486
log.debug("Waiting as long as " + poolTimeToWait + " milliseconds for connection.");
477487
}
478488
long wt = System.currentTimeMillis();
479-
state.wait(poolTimeToWait);
489+
fairLock.tryLock(poolTimeToWait, TimeUnit.MILLISECONDS);
480490
state.accumulatedWaitTime += System.currentTimeMillis() - wt;
481491
} catch (InterruptedException e) {
482492
// set interrupt flag
@@ -513,6 +523,10 @@ private PooledConnection popConnection(String username, String password) throws
513523
}
514524
}
515525
}
526+
} finally {
527+
if (fairLock.isLocked()) {
528+
fairLock.unlock();
529+
}
516530
}
517531

518532
}

0 commit comments

Comments
 (0)