Skip to content

Commit 973b23e

Browse files
committed
#30 - Refactor replace synchronised block with ReentrantLock in PooledConnection
1 parent 88e3d68 commit 973b23e

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/main/java/io/ebean/datasource/pool/PooledConnection.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
1717
import java.util.Map;
18+
import java.util.concurrent.locks.ReentrantLock;
1819

1920
/**
2021
* Is a connection that belongs to a DataSourcePool.
@@ -90,7 +91,7 @@ public class PooledConnection extends ConnectionDelegator {
9091
*/
9192
private final PstmtCache pstmtCache;
9293

93-
private final Object pstmtMonitor = new Object();
94+
private final ReentrantLock lock = new ReentrantLock();
9495

9596
/**
9697
* The status of the connection. IDLE, ACTIVE or ENDED.
@@ -303,7 +304,8 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency) th
303304
* Return a PreparedStatement back into the cache.
304305
*/
305306
void returnPreparedStatement(ExtendedPreparedStatement pstmt) {
306-
synchronized (pstmtMonitor) {
307+
lock.lock();
308+
try {
307309
if (!pstmtCache.returnStatement(pstmt)) {
308310
try {
309311
// Already an entry in the cache with the exact same SQL...
@@ -312,6 +314,8 @@ void returnPreparedStatement(ExtendedPreparedStatement pstmt) {
312314
logger.error("Error closing Pstmt", e);
313315
}
314316
}
317+
} finally {
318+
lock.unlock();
315319
}
316320
}
317321

@@ -341,27 +345,27 @@ private PreparedStatement prepareStatement(String sql, boolean useFlag, int flag
341345
if (status == STATUS_IDLE) {
342346
throw new SQLException(IDLE_CONNECTION_ACCESSED_ERROR + "prepareStatement()");
343347
}
348+
lock.lock();
344349
try {
345-
synchronized (pstmtMonitor) {
346-
lastStatement = sql;
347-
// try to get a matching cached PStmt from the cache.
348-
ExtendedPreparedStatement pstmt = pstmtCache.remove(cacheKey);
349-
if (pstmt != null) {
350-
return pstmt.reset();
351-
}
352-
353-
PreparedStatement actualPstmt;
354-
if (useFlag) {
355-
actualPstmt = connection.prepareStatement(sql, flag);
356-
} else {
357-
actualPstmt = connection.prepareStatement(sql);
358-
}
359-
return new ExtendedPreparedStatement(this, actualPstmt, sql, cacheKey);
350+
lastStatement = sql;
351+
// try to get a matching cached PStmt from the cache.
352+
ExtendedPreparedStatement pstmt = pstmtCache.remove(cacheKey);
353+
if (pstmt != null) {
354+
return pstmt.reset();
360355
}
361356

357+
PreparedStatement actualPstmt;
358+
if (useFlag) {
359+
actualPstmt = connection.prepareStatement(sql, flag);
360+
} else {
361+
actualPstmt = connection.prepareStatement(sql);
362+
}
363+
return new ExtendedPreparedStatement(this, actualPstmt, sql, cacheKey);
362364
} catch (SQLException ex) {
363365
markWithError();
364366
throw ex;
367+
} finally {
368+
lock.unlock();
365369
}
366370
}
367371

0 commit comments

Comments
 (0)