Skip to content

Commit d6a2d62

Browse files
committed
fix: check lock status
1 parent 8b4f303 commit d6a2d62

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ public int getPoolPingConnectionsNotUsedFor() {
331331
* Closes all active and idle connections in the pool.
332332
*/
333333
public void forceCloseAll() {
334-
fairLock.lock();
335334
try {
335+
fairLock.lock();
336336
expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
337337
for (int i = state.activeConnections.size(); i > 0; i--) {
338338
try {
@@ -381,7 +381,6 @@ private int assembleConnectionTypeCode(String url, String username, String passw
381381
}
382382

383383
protected void pushConnection(PooledConnection conn) throws SQLException {
384-
385384
try {
386385
fairLock.lock();
387386
state.activeConnections.remove(conn);
@@ -399,7 +398,9 @@ protected void pushConnection(PooledConnection conn) throws SQLException {
399398
if (log.isDebugEnabled()) {
400399
log.debug("Returned connection " + newConn.getRealHashCode() + " to pool.");
401400
}
402-
fairLock.unlock();
401+
if (fairLock.isLocked()) {
402+
fairLock.unlock();
403+
}
403404
} else {
404405
state.accumulatedCheckoutTime += conn.getCheckoutTime();
405406
if (!conn.getRealConnection().getAutoCommit()) {
@@ -431,8 +432,8 @@ private PooledConnection popConnection(String username, String password) throws
431432
int localBadConnectionCount = 0;
432433

433434
while (conn == null) {
434-
fairLock.lock();
435435
try {
436+
fairLock.lock();
436437
if (!state.idleConnections.isEmpty()) {
437438
// Pool has available connection
438439
conn = state.idleConnections.remove(0);

0 commit comments

Comments
 (0)