Search in sources :

Example 1 with ViburDBCPException

use of org.vibur.dbcp.ViburDBCPException in project vibur-dbcp by vibur.

the class ConnectionFactory method postCreate.

private ConnHolder postCreate(Connection rawConnection, SQLException sqlException, long startNanoTime) throws ViburDBCPException {
    Hook.InitConnection[] onInit = connHooksAccessor.onInit();
    long currentNanoTime = onInit.length > 0 || config.getConnectionIdleLimitInSeconds() >= 0 ? System.nanoTime() : 0;
    if (onInit.length > 0) {
        try {
            long takenNanos = currentNanoTime - startNanoTime;
            for (Hook.InitConnection hook : onInit) hook.on(rawConnection, takenNanos);
        } catch (SQLException e) {
            quietClose(rawConnection);
            sqlException = chainSQLException(sqlException, e);
        }
    }
    if (sqlException != null)
        throw new ViburDBCPException(sqlException);
    logger.debug("Created rawConnection {}", rawConnection);
    return prepareTracking(new ConnHolder(rawConnection, version(), config.getConnectionIdleLimitInSeconds() >= 0 ? currentNanoTime : 0));
}
Also used : ViburDBCPException(org.vibur.dbcp.ViburDBCPException) SQLException(java.sql.SQLException)

Example 2 with ViburDBCPException

use of org.vibur.dbcp.ViburDBCPException in project vibur-dbcp by vibur.

the class PoolOperations method getConnHolder.

/**
 * Tries to take a {@code ConnHolder} object from the underlying object pool. If successful, never returns
 * {@code null}.
 *
 * @param timeoutMs timeout in millis to pass to the underlying object pool {@code take} methods
 * @throws SQLException to indicate a generic non-recoverable error that cannot be retried
 * @throws SQLTimeoutException to indicate a non-recoverable error due to timeout that cannot be retried
 * @throws ViburDBCPException to indicate a recoverable error that can be retried
 */
private ConnHolder getConnHolder(long timeoutMs) throws SQLException, ViburDBCPException {
    Hook.GetConnection[] onGet = ((ConnHooksAccessor) dataSource.getConnHooks()).onGet();
    ConnHolder connHolder = null;
    long[] waitedNanos = NO_WAIT;
    SQLException sqlException = null;
    ViburDBCPException viburException = null;
    try {
        if (onGet.length > 0) {
            waitedNanos = new long[1];
            connHolder = timeoutMs > 0 ? poolService.tryTake(timeoutMs, MILLISECONDS, waitedNanos) : poolService.take(waitedNanos);
        } else {
            connHolder = timeoutMs > 0 ? poolService.tryTake(timeoutMs, MILLISECONDS) : poolService.take();
        }
        if (connHolder == null) {
            // we were *not* able to obtain a connection from the pool
            sqlException = createSQLException(onGet.length > 0 ? waitedNanos[0] : MILLISECONDS.toNanos(timeoutMs));
        }
    } catch (ViburDBCPException e) {
        // thrown (indirectly) by the ConnectionFactory.create() methods
        viburException = e;
        // currently all such errors are treated as recoverable, i.e., can be retried
        sqlException = e.unwrapSQLException();
    } finally {
        Connection rawConnection = connHolder != null ? connHolder.rawConnection() : null;
        try {
            for (Hook.GetConnection hook : onGet) {
                hook.on(rawConnection, waitedNanos[0]);
            }
        } catch (SQLException e) {
            sqlException = chainSQLException(sqlException, e);
        }
    }
    if (viburException != null) {
        // a recoverable error
        throw viburException;
    }
    if (sqlException != null) {
        // a non-recoverable error
        throw sqlException;
    }
    // never null if we reach this point
    return connHolder;
}
Also used : ViburDBCPException(org.vibur.dbcp.ViburDBCPException) SQLException(java.sql.SQLException) JdbcUtils.chainSQLException(org.vibur.dbcp.util.JdbcUtils.chainSQLException) ConnHooksAccessor(org.vibur.dbcp.pool.HookHolder.ConnHooksAccessor) Proxy.newProxyConnection(org.vibur.dbcp.proxy.Proxy.newProxyConnection) Connection(java.sql.Connection)

Example 3 with ViburDBCPException

use of org.vibur.dbcp.ViburDBCPException in project vibur-dbcp by vibur.

the class PoolOperations method getProxyConnection.

// //////////// getProxyConnection(...) //////////////
public Connection getProxyConnection(long timeoutMs) throws SQLException {
    int attempt = 1;
    ConnHolder connHolder = null;
    SQLException sqlException = null;
    long startNanoTime = System.nanoTime();
    while (connHolder == null) {
        try {
            connHolder = getConnHolder(timeoutMs);
        } catch (ViburDBCPException e) {
            // thrown only if we can retry the operation, see getConnHolder(...)
            sqlException = chainSQLException(e.unwrapSQLException(), sqlException);
            if (attempt++ > dataSource.getAcquireRetryAttempts()) {
                // check the max retries limit
                throw sqlException;
            }
            if (timeoutMs > 0) {
                // check the time limit if applicable
                timeoutMs = NANOSECONDS.toMillis(connectionTimeoutInNanos - (System.nanoTime() - startNanoTime)) - // calculates the remaining timeout
                dataSource.getAcquireRetryDelayInMs();
                if (timeoutMs <= 0) {
                    throw sqlException;
                }
            }
            try {
                MILLISECONDS.sleep(dataSource.getAcquireRetryDelayInMs());
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw chainSQLException(new SQLException(ie), sqlException);
            }
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Taking rawConnection {}", connHolder.rawConnection());
    }
    Connection proxy = newProxyConnection(connHolder, this, dataSource);
    if (dataSource.isPoolEnableConnectionTracking()) {
        connHolder.setProxyConnection(proxy);
    }
    return proxy;
}
Also used : ViburDBCPException(org.vibur.dbcp.ViburDBCPException) SQLException(java.sql.SQLException) JdbcUtils.chainSQLException(org.vibur.dbcp.util.JdbcUtils.chainSQLException) Proxy.newProxyConnection(org.vibur.dbcp.proxy.Proxy.newProxyConnection) Connection(java.sql.Connection)

Aggregations

SQLException (java.sql.SQLException)3 ViburDBCPException (org.vibur.dbcp.ViburDBCPException)3 Connection (java.sql.Connection)2 Proxy.newProxyConnection (org.vibur.dbcp.proxy.Proxy.newProxyConnection)2 JdbcUtils.chainSQLException (org.vibur.dbcp.util.JdbcUtils.chainSQLException)2 ConnHooksAccessor (org.vibur.dbcp.pool.HookHolder.ConnHooksAccessor)1