Search in sources :

Example 16 with SQLTransientConnectionException

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;
}
Also used : SQLTransientConnectionException(java.sql.SQLTransientConnectionException) SQLException(java.sql.SQLException) ClockSource.elapsedDisplayString(com.zaxxer.hikari.util.ClockSource.elapsedDisplayString)

Example 17 with SQLTransientConnectionException

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));
        }
    }
}
Also used : SQLTransientConnectionException(java.sql.SQLTransientConnectionException) Connection(java.sql.Connection) ProxyConnection(com.zaxxer.hikari.pool.ProxyConnection) NamingException(javax.naming.NamingException) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) SQLException(java.sql.SQLException) SQLTransientConnectionException(java.sql.SQLTransientConnectionException)

Example 18 with SQLTransientConnectionException

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);
        }
    }
}
Also used : SQLTransientConnectionException(java.sql.SQLTransientConnectionException) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

SQLTransientConnectionException (java.sql.SQLTransientConnectionException)18 Test (org.testng.annotations.Test)14 BaseTest (util.BaseTest)14 SQLException (java.sql.SQLException)4 Connection (java.sql.Connection)3 Test (org.junit.Test)2 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)1 ProxyConnection (com.zaxxer.hikari.pool.ProxyConnection)1 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)1 ClockSource.elapsedDisplayString (com.zaxxer.hikari.util.ClockSource.elapsedDisplayString)1 SQLTransientException (java.sql.SQLTransientException)1 NamingException (javax.naming.NamingException)1