mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Don't return connections to the pool if they've thrown exceptions.
This commit is contained in:
@@ -70,6 +70,7 @@ import static java.sql.Types.BOOLEAN;
|
|||||||
import static java.sql.Types.INTEGER;
|
import static java.sql.Types.INTEGER;
|
||||||
import static java.sql.Types.VARCHAR;
|
import static java.sql.Types.VARCHAR;
|
||||||
import static java.util.Arrays.asList;
|
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.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
@@ -572,6 +573,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
try {
|
try {
|
||||||
if (closed) throw new DbClosedException();
|
if (closed) throw new DbClosedException();
|
||||||
txn = connections.poll();
|
txn = connections.poll();
|
||||||
|
logConnectionCounts();
|
||||||
} finally {
|
} finally {
|
||||||
connectionsLock.unlock();
|
connectionsLock.unlock();
|
||||||
}
|
}
|
||||||
@@ -588,6 +590,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
throw new DbClosedException();
|
throw new DbClosedException();
|
||||||
}
|
}
|
||||||
openConnections++;
|
openConnections++;
|
||||||
|
logConnectionCounts();
|
||||||
connectionsChanged.signalAll();
|
connectionsChanged.signalAll();
|
||||||
} finally {
|
} finally {
|
||||||
connectionsLock.unlock();
|
connectionsLock.unlock();
|
||||||
@@ -599,42 +602,57 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
return txn;
|
return txn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GuardedBy("connectionsLock")
|
||||||
|
private void logConnectionCounts() {
|
||||||
|
if (LOG.isLoggable(FINE)) {
|
||||||
|
LOG.fine(openConnections + " connections open, "
|
||||||
|
+ connections.size() + " in pool");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void abortTransaction(Connection txn) {
|
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 {
|
try {
|
||||||
txn.rollback();
|
txn.rollback();
|
||||||
connectionsLock.lock();
|
|
||||||
try {
|
|
||||||
connections.add(txn);
|
|
||||||
connectionsChanged.signalAll();
|
|
||||||
} finally {
|
|
||||||
connectionsLock.unlock();
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
// Try to close the connection
|
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
tryToClose(txn, LOG, WARNING);
|
}
|
||||||
// Whatever happens, allow the database to close
|
closeConnection(txn);
|
||||||
connectionsLock.lock();
|
}
|
||||||
try {
|
|
||||||
openConnections--;
|
private void closeConnection(Connection txn) {
|
||||||
connectionsChanged.signalAll();
|
tryToClose(txn, LOG, WARNING);
|
||||||
} finally {
|
connectionsLock.lock();
|
||||||
connectionsLock.unlock();
|
try {
|
||||||
}
|
openConnections--;
|
||||||
|
logConnectionCounts();
|
||||||
|
connectionsChanged.signalAll();
|
||||||
|
} finally {
|
||||||
|
connectionsLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void commitTransaction(Connection txn) throws DbException {
|
public void commitTransaction(Connection txn) throws DbException {
|
||||||
|
// If the transaction commits successfully then return the connection
|
||||||
|
// to the pool, otherwise close it
|
||||||
try {
|
try {
|
||||||
txn.commit();
|
txn.commit();
|
||||||
|
returnConnectionToPool(txn);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
closeConnection(txn);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void returnConnectionToPool(Connection txn) {
|
||||||
connectionsLock.lock();
|
connectionsLock.lock();
|
||||||
try {
|
try {
|
||||||
connections.add(txn);
|
connections.add(txn);
|
||||||
|
logConnectionCounts();
|
||||||
connectionsChanged.signalAll();
|
connectionsChanged.signalAll();
|
||||||
} finally {
|
} finally {
|
||||||
connectionsLock.unlock();
|
connectionsLock.unlock();
|
||||||
@@ -650,6 +668,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
openConnections -= connections.size();
|
openConnections -= connections.size();
|
||||||
connections.clear();
|
connections.clear();
|
||||||
while (openConnections > 0) {
|
while (openConnections > 0) {
|
||||||
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("Waiting for " + openConnections
|
||||||
|
+ " connections to be closed");
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
connectionsChanged.await();
|
connectionsChanged.await();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -660,6 +682,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
openConnections -= connections.size();
|
openConnections -= connections.size();
|
||||||
connections.clear();
|
connections.clear();
|
||||||
}
|
}
|
||||||
|
LOG.info("All connections closed");
|
||||||
} finally {
|
} finally {
|
||||||
connectionsLock.unlock();
|
connectionsLock.unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user