Don't return connections to the pool if they've thrown exceptions.

This commit is contained in:
akwizgran
2022-04-08 15:49:43 +01:00
parent 9304a6b266
commit 5d952ff68e

View File

@@ -70,6 +70,7 @@ import static java.sql.Types.BOOLEAN;
import static java.sql.Types.INTEGER;
import static java.sql.Types.VARCHAR;
import static java.util.Arrays.asList;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
@@ -572,6 +573,7 @@ abstract class JdbcDatabase implements Database<Connection> {
try {
if (closed) throw new DbClosedException();
txn = connections.poll();
logConnectionCounts();
} finally {
connectionsLock.unlock();
}
@@ -588,6 +590,7 @@ abstract class JdbcDatabase implements Database<Connection> {
throw new DbClosedException();
}
openConnections++;
logConnectionCounts();
connectionsChanged.signalAll();
} finally {
connectionsLock.unlock();
@@ -599,42 +602,57 @@ abstract class JdbcDatabase implements Database<Connection> {
return txn;
}
@GuardedBy("connectionsLock")
private void logConnectionCounts() {
if (LOG.isLoggable(FINE)) {
LOG.fine(openConnections + " connections open, "
+ connections.size() + " in pool");
}
}
@Override
public void abortTransaction(Connection txn) {
// The transaction may have been aborted due to an earlier exception,
// so close the connection rather than returning it to the pool
try {
txn.rollback();
connectionsLock.lock();
try {
connections.add(txn);
connectionsChanged.signalAll();
} finally {
connectionsLock.unlock();
}
} catch (SQLException e) {
// Try to close the connection
logException(LOG, WARNING, e);
tryToClose(txn, LOG, WARNING);
// Whatever happens, allow the database to close
connectionsLock.lock();
try {
openConnections--;
connectionsChanged.signalAll();
} finally {
connectionsLock.unlock();
}
}
closeConnection(txn);
}
private void closeConnection(Connection txn) {
tryToClose(txn, LOG, WARNING);
connectionsLock.lock();
try {
openConnections--;
logConnectionCounts();
connectionsChanged.signalAll();
} finally {
connectionsLock.unlock();
}
}
@Override
public void commitTransaction(Connection txn) throws DbException {
// If the transaction commits successfully then return the connection
// to the pool, otherwise close it
try {
txn.commit();
returnConnectionToPool(txn);
} catch (SQLException e) {
logException(LOG, WARNING, e);
closeConnection(txn);
throw new DbException(e);
}
}
private void returnConnectionToPool(Connection txn) {
connectionsLock.lock();
try {
connections.add(txn);
logConnectionCounts();
connectionsChanged.signalAll();
} finally {
connectionsLock.unlock();
@@ -650,6 +668,10 @@ abstract class JdbcDatabase implements Database<Connection> {
openConnections -= connections.size();
connections.clear();
while (openConnections > 0) {
if (LOG.isLoggable(INFO)) {
LOG.info("Waiting for " + openConnections
+ " connections to be closed");
}
try {
connectionsChanged.await();
} catch (InterruptedException e) {
@@ -660,6 +682,7 @@ abstract class JdbcDatabase implements Database<Connection> {
openConnections -= connections.size();
connections.clear();
}
LOG.info("All connections closed");
} finally {
connectionsLock.unlock();
}