Search in sources :

Example 1 with PoolInitializationException

use of com.zaxxer.hikari.pool.HikariPool.PoolInitializationException in project HikariCP by brettwooldridge.

the class HikariDataSource method getConnection.

// ***********************************************************************
// DataSource methods
// ***********************************************************************
/**
 * {@inheritDoc}
 */
@Override
public Connection getConnection() throws SQLException {
    if (isClosed()) {
        throw new SQLException("HikariDataSource " + this + " has been closed.");
    }
    if (fastPathPool != null) {
        return fastPathPool.getConnection();
    }
    // See http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
    HikariPool result = pool;
    if (result == null) {
        synchronized (this) {
            result = pool;
            if (result == null) {
                validate();
                LOGGER.info("{} - Starting...", getPoolName());
                try {
                    pool = result = new HikariPool(this);
                    this.seal();
                } catch (PoolInitializationException pie) {
                    if (pie.getCause() instanceof SQLException) {
                        throw (SQLException) pie.getCause();
                    } else {
                        throw pie;
                    }
                }
                LOGGER.info("{} - Start completed.", getPoolName());
            }
        }
    }
    return result.getConnection();
}
Also used : HikariPool(com.zaxxer.hikari.pool.HikariPool) SQLException(java.sql.SQLException) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)

Example 2 with PoolInitializationException

use of com.zaxxer.hikari.pool.HikariPool.PoolInitializationException in project HikariCP by brettwooldridge.

the class PoolBase method initializeDataSource.

// ***********************************************************************
// Private methods
// ***********************************************************************
/**
 * Create/initialize the underlying DataSource.
 */
private void initializeDataSource() {
    final String jdbcUrl = config.getJdbcUrl();
    final String username = config.getUsername();
    final String password = config.getPassword();
    final String dsClassName = config.getDataSourceClassName();
    final String driverClassName = config.getDriverClassName();
    final String dataSourceJNDI = config.getDataSourceJNDI();
    final Properties dataSourceProperties = config.getDataSourceProperties();
    DataSource dataSource = config.getDataSource();
    if (dsClassName != null && dataSource == null) {
        dataSource = createInstance(dsClassName, DataSource.class);
        PropertyElf.setTargetFromProperties(dataSource, dataSourceProperties);
    } else if (jdbcUrl != null && dataSource == null) {
        dataSource = new DriverDataSource(jdbcUrl, driverClassName, dataSourceProperties, username, password);
    } else if (dataSourceJNDI != null && dataSource == null) {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup(dataSourceJNDI);
        } catch (NamingException e) {
            throw new PoolInitializationException(e);
        }
    }
    if (dataSource != null) {
        setLoginTimeout(dataSource);
        createNetworkTimeoutExecutor(dataSource, dsClassName, jdbcUrl);
    }
    this.dataSource = dataSource;
}
Also used : DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) NamingException(javax.naming.NamingException) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)

Example 3 with PoolInitializationException

use of com.zaxxer.hikari.pool.HikariPool.PoolInitializationException in project HikariCP by brettwooldridge.

the class TestConnections method testInitializationFailure2.

@Test
public void testInitializationFailure2() throws SQLException {
    StubDataSource stubDataSource = new StubDataSource();
    stubDataSource.setThrowException(new SQLException("Connection refused"));
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSource(stubDataSource);
    try (HikariDataSource ds = new HikariDataSource(config);
        Connection ignored = ds.getConnection()) {
        fail("Initialization should have failed");
    } catch (PoolInitializationException e) {
    // passed
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) Test(org.junit.Test)

Example 4 with PoolInitializationException

use of com.zaxxer.hikari.pool.HikariPool.PoolInitializationException in project HikariCP by brettwooldridge.

the class TestConnections method testInvalidConnectionTestQuery.

@Test
public void testInvalidConnectionTestQuery() {
    class BadConnection extends StubConnection {

        /**
         * {@inheritDoc}
         */
        @Override
        public Statement createStatement() throws SQLException {
            throw new SQLException("Simulated exception in createStatement()");
        }
    }
    StubDataSource stubDataSource = new StubDataSource() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Connection getConnection() {
            return new BadConnection();
        }
    };
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(2);
    config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(3));
    config.setConnectionTestQuery("VALUES 1");
    config.setInitializationFailTimeout(TimeUnit.SECONDS.toMillis(2));
    config.setDataSource(stubDataSource);
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection ignored = ds.getConnection()) {
            fail("getConnection() should have failed");
        } catch (SQLException e) {
            assertSame("Simulated exception in createStatement()", e.getNextException().getMessage());
        }
    } catch (PoolInitializationException e) {
        assertSame("Simulated exception in createStatement()", e.getCause().getMessage());
    }
    config.setInitializationFailTimeout(0);
    try (HikariDataSource ignored = new HikariDataSource(config)) {
        fail("Initialization should have failed");
    } catch (PoolInitializationException e) {
    // passed
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) Test(org.junit.Test)

Example 5 with PoolInitializationException

use of com.zaxxer.hikari.pool.HikariPool.PoolInitializationException in project atlasdb by palantir.

the class HikariCPConnectionManager method getDataSourcePool.

private HikariDataSource getDataSourcePool() {
    // Print a stack trace whenever we initialize a pool
    log.debug("Initializing connection pool: {}", connConfig, new RuntimeException("Initializing connection pool"));
    HikariDataSource dataSourcePool;
    try {
        try {
            dataSourcePool = new HikariDataSource(connConfig.getHikariConfig());
        } catch (IllegalArgumentException e) {
            // allow multiple pools on same JVM (they need unique names / endpoints)
            if (e.getMessage().contains("A metric named")) {
                String poolName = connConfig.getHikariConfig().getPoolName();
                connConfig.getHikariConfig().setPoolName(poolName + "-" + ThreadLocalRandom.current().nextInt());
                dataSourcePool = new HikariDataSource(connConfig.getHikariConfig());
            } else {
                throw e;
            }
        }
    } catch (PoolInitializationException e) {
        log.error("Failed to initialize hikari data source: {}", connConfig.getUrl(), e);
        if (ExceptionCheck.isTimezoneInvalid(e)) {
            String tzname = TimeZone.getDefault().getID();
            final String errorPreamble = "Invalid system timezone " + tzname + " detected.";
            final String errorAfterward = "This can be corrected at the system level or overridden in the JVM startup flags by appending -Duser.timezone=UTC. Contact support for more information.";
            if (tzname.equals("Etc/Universal")) {
                // Really common failure case. UTC is both a name AND an abbreviation.
                log.error("{} The timezone *name* should be UTC. {}", errorPreamble, errorAfterward);
            } else {
                log.error("{} This is caused by using non-standard or unsupported timezone names. {}", errorPreamble, errorAfterward);
            }
        }
        // This exception gets thrown by HikariCP and is useless outside of ConnectionManagers.
        RuntimeException e2 = new RuntimeException(ExceptionUtils.getMessage(e), e.getCause());
        e2.setStackTrace(e.getStackTrace());
        throw e2;
    }
    return dataSourcePool;
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)

Aggregations

PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)5 HikariDataSource (com.zaxxer.hikari.HikariDataSource)3 HikariConfig (com.zaxxer.hikari.HikariConfig)2 StubConnection (com.zaxxer.hikari.mocks.StubConnection)2 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)2 Test (org.junit.Test)2 HikariPool (com.zaxxer.hikari.pool.HikariPool)1 DriverDataSource (com.zaxxer.hikari.util.DriverDataSource)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1