Skip to content

Commit

Permalink
Merge pull request #2602 from why168/refactor/lock
Browse files Browse the repository at this point in the history
PooledDataSource : replace synchronized block with ReentrantLock
  • Loading branch information
harawata committed Jul 17, 2022
2 parents d081405 + 9990dd7 commit 84cc6a1
Showing 1 changed file with 21 additions and 5 deletions.
Expand Up @@ -23,6 +23,10 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

import javax.sql.DataSource;
Expand Down Expand Up @@ -56,6 +60,9 @@ public class PooledDataSource implements DataSource {

private int expectedConnectionTypeCode;

private final Lock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();

public PooledDataSource() {
dataSource = new UnpooledDataSource();
}
Expand Down Expand Up @@ -328,7 +335,8 @@ public int getPoolPingConnectionsNotUsedFor() {
* Closes all active and idle connections in the pool.
*/
public void forceCloseAll() {
synchronized (state) {
lock.lock();
try {
expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
for (int i = state.activeConnections.size(); i > 0; i--) {
try {
Expand Down Expand Up @@ -358,6 +366,8 @@ public void forceCloseAll() {
// ignore
}
}
} finally {
lock.unlock();
}
if (log.isDebugEnabled()) {
log.debug("PooledDataSource forcefully closed/removed all connections.");
Expand All @@ -374,7 +384,8 @@ private int assembleConnectionTypeCode(String url, String username, String passw

protected void pushConnection(PooledConnection conn) throws SQLException {

synchronized (state) {
lock.lock();
try {
state.activeConnections.remove(conn);
if (conn.isValid()) {
if (state.idleConnections.size() < poolMaximumIdleConnections && conn.getConnectionTypeCode() == expectedConnectionTypeCode) {
Expand All @@ -390,7 +401,7 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
if (log.isDebugEnabled()) {
log.debug("Returned connection " + newConn.getRealHashCode() + " to pool.");
}
state.notifyAll();
condition.signal();
} else {
state.accumulatedCheckoutTime += conn.getCheckoutTime();
if (!conn.getRealConnection().getAutoCommit()) {
Expand All @@ -408,6 +419,8 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
}
state.badConnectionCount++;
}
} finally {
lock.unlock();
}
}

Expand All @@ -418,7 +431,8 @@ private PooledConnection popConnection(String username, String password) throws
int localBadConnectionCount = 0;

while (conn == null) {
synchronized (state) {
lock.lock();
try {
if (!state.idleConnections.isEmpty()) {
// Pool has available connection
conn = state.idleConnections.remove(0);
Expand Down Expand Up @@ -476,7 +490,7 @@ private PooledConnection popConnection(String username, String password) throws
log.debug("Waiting as long as " + poolTimeToWait + " milliseconds for connection.");
}
long wt = System.currentTimeMillis();
state.wait(poolTimeToWait);
condition.await(poolTimeToWait, TimeUnit.MILLISECONDS);
state.accumulatedWaitTime += System.currentTimeMillis() - wt;
} catch (InterruptedException e) {
// set interrupt flag
Expand Down Expand Up @@ -513,6 +527,8 @@ private PooledConnection popConnection(String username, String password) throws
}
}
}
} finally {
lock.unlock();
}

}
Expand Down

0 comments on commit 84cc6a1

Please sign in to comment.