use of java.sql.SQLTransientConnectionException in project HikariCP by brettwooldridge.
the class HikariPool method createTimeoutException.
/**
* Create a timeout exception (specifically, {@link SQLTransientConnectionException}) to be thrown, because a
* timeout occurred when trying to acquire a Connection from the pool. If there was an underlying cause for the
* timeout, e.g. a SQLException thrown by the driver while trying to create a new Connection, then use the
* SQL State from that exception as our own and additionally set that exception as the "next" SQLException inside
* of our exception.
*
* As a side-effect, log the timeout failure at DEBUG, and record the timeout failure in the metrics tracker.
*
* @param startTime the start time (timestamp) of the acquisition attempt
* @return a SQLException to be thrown from {@link #getConnection()}
*/
private SQLException createTimeoutException(long startTime) {
logPoolState("Timeout failure ");
metricsTracker.recordConnectionTimeout();
String sqlState = null;
final Throwable originalException = getLastConnectionFailure();
if (originalException instanceof SQLException) {
sqlState = ((SQLException) originalException).getSQLState();
}
final SQLException connectionException = new SQLTransientConnectionException(poolName + " - Connection is not available, request timed out after " + elapsedMillis(startTime) + "ms.", sqlState, originalException);
if (originalException instanceof SQLException) {
connectionException.setNextException((SQLException) originalException);
}
return connectionException;
}
use of java.sql.SQLTransientConnectionException in project HikariCP by brettwooldridge.
the class PoolBase method newConnection.
/**
* Obtain connection from data source.
*
* @return a Connection connection
*/
private Connection newConnection() throws Exception {
final long start = currentTime();
Connection connection = null;
try {
String username = config.getUsername();
String password = config.getPassword();
connection = (username == null) ? dataSource.getConnection() : dataSource.getConnection(username, password);
if (connection == null) {
throw new SQLTransientConnectionException("DataSource returned null unexpectedly");
}
setupConnection(connection);
lastConnectionFailure.set(null);
return connection;
} catch (Exception e) {
if (connection != null) {
quietlyCloseConnection(connection, "(Failed to create/setup connection)");
} else if (getLastConnectionFailure() == null) {
LOGGER.debug("{} - Failed to create/setup connection: {}", poolName, e.getMessage());
}
lastConnectionFailure.set(e);
throw e;
} finally {
// tracker will be null during failFast check
if (metricsTracker != null) {
metricsTracker.recordConnectionCreated(elapsedMillis(start));
}
}
}
use of java.sql.SQLTransientConnectionException in project atlasdb by palantir.
the class HikariCpConnectionManagerTest method testConnectionsAreReturnedToPoolWhenClosedAndOverAllocationsAreStillRejected.
@SuppressWarnings("checkstyle:NestedTryDepth")
@Test
public void testConnectionsAreReturnedToPoolWhenClosedAndOverAllocationsAreStillRejected() throws SQLException {
try (Connection conn1 = manager.getConnection();
Connection conn2 = manager.getConnection()) {
try (Connection conn3 = manager.getConnection()) {
checkConnection(conn3);
// Make sure we exhausted the pool
boolean caught = false;
try (Connection conn4 = manager.getConnection()) {
Assert.fail();
} catch (SQLTransientConnectionException e) {
caught = true;
}
Assert.assertTrue(caught);
}
// Try getting a connection again after we returned the last one: should succeed
try (Connection conn3 = manager.getConnection()) {
checkConnection(conn3);
}
}
}
Aggregations