Skip to content

Commit d982e5c

Browse files
committed
fix stackoverflow exception in testConnection
1 parent ae2ed1d commit d982e5c

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,10 @@ private void heartBeat() {
369369
}
370370

371371
private void testConnection() {
372-
Connection conn = null;
372+
PooledConnection conn = null;
373373
try {
374374
// Get a connection from the pool and test it
375-
conn = getConnection();
375+
conn = getPooledConnection();
376376
heartbeatPoolExhaustedCount = 0;
377377
if (testConnection(conn)) {
378378
notifyDataSourceIsUp();
@@ -394,7 +394,7 @@ private void testConnection() {
394394
if (!conn.getAutoCommit()) {
395395
conn.rollback();
396396
}
397-
conn.close();
397+
conn.closePooledConnection(false);
398398
}
399399
} catch (SQLException ex) {
400400
Log.warn("Can't close connection in checkDataSource!");
@@ -541,8 +541,12 @@ void returnConnection(PooledConnection pooledConnection) {
541541
/**
542542
* This is a bad connection and must be removed from the pool's busy list and fully closed.
543543
*/
544-
void returnConnectionForceClose(PooledConnection pooledConnection) {
544+
void returnConnectionForceClose(PooledConnection pooledConnection, boolean testPool) {
545545
returnTheConnection(pooledConnection, true);
546+
if (testPool) {
547+
// Got a bad connection so check the pool
548+
testConnection();
549+
}
546550
}
547551

548552
void removeClosedConnection(PooledConnection pooledConnection) {
@@ -558,10 +562,6 @@ private void returnTheConnection(PooledConnection pooledConnection, boolean forc
558562
poolListener.onBeforeReturnConnection(pooledConnection);
559563
}
560564
queue.returnPooledConnection(pooledConnection, forceClose);
561-
if (forceClose) {
562-
// Got a bad connection so check the pool
563-
testConnection();
564-
}
565565
}
566566

567567
void returnConnectionReset(PooledConnection pooledConnection) {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,16 @@ private boolean isReadOnlyMessage(SQLException ex) {
445445
*/
446446
@Override
447447
public void close() throws SQLException {
448+
closePooledConnection(true);
449+
}
450+
451+
void closePooledConnection(boolean testPool) throws SQLException {
448452
if (status == STATUS_IDLE) {
449453
throw new SQLException(IDLE_CONNECTION_ACCESSED_ERROR + "close()");
450454
}
451455
boolean mayHaveUncommittedChanges = !autoCommit && !readOnly && status == STATUS_ACTIVE;
452456
if (mayHaveUncommittedChanges && pool.enforceCleanClose()) {
453-
pool.returnConnectionForceClose(this);
457+
pool.returnConnectionForceClose(this, testPool);
454458
throw new AssertionError("Tried to close a dirty connection. See https://github.com/ebean-orm/ebean-datasource/issues/116 for details.");
455459
}
456460
if (hadErrors) {
@@ -459,7 +463,7 @@ public void close() throws SQLException {
459463
return;
460464
} else if (pool.invalidConnection(this)) {
461465
// the connection is BAD, remove it, close it and test the pool
462-
pool.returnConnectionForceClose(this);
466+
pool.returnConnectionForceClose(this, testPool);
463467
return;
464468
}
465469
}
@@ -513,7 +517,7 @@ public void close() throws SQLException {
513517
} catch (Exception ex) {
514518
// the connection is BAD, remove it, close it and test the pool
515519
Log.warn("Error when trying to return connection to pool, closing fully.", ex);
516-
pool.returnConnectionForceClose(this);
520+
pool.returnConnectionForceClose(this, testPool);
517521
}
518522
}
519523

0 commit comments

Comments
 (0)