Search in sources :

Example 1 with HikariPool

use of com.zaxxer.hikari.pool.HikariPool 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 HikariPool

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

the class HikariDataSource method setMetricsTrackerFactory.

/**
 * {@inheritDoc}
 */
@Override
public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory) {
    boolean isAlreadySet = getMetricsTrackerFactory() != null;
    super.setMetricsTrackerFactory(metricsTrackerFactory);
    HikariPool p = pool;
    if (p != null) {
        if (isAlreadySet) {
            throw new IllegalStateException("MetricsTrackerFactory can only be set one time");
        } else {
            p.setMetricsTrackerFactory(super.getMetricsTrackerFactory());
        }
    }
}
Also used : HikariPool(com.zaxxer.hikari.pool.HikariPool)

Example 3 with HikariPool

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

the class BasicPoolTest method testIdleTimeout2.

@Test
public void testIdleTimeout2() throws InterruptedException, SQLException {
    HikariConfig config = newHikariConfig();
    config.setMaximumPoolSize(50);
    config.setConnectionTestQuery("SELECT 1");
    config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
    config.addDataSourceProperty("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "1000");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
        SECONDS.sleep(1);
        HikariPool pool = getPool(ds);
        getUnsealedConfig(ds).setIdleTimeout(3000);
        assertEquals("Total connections not as expected", 50, pool.getTotalConnections());
        assertEquals("Idle connections not as expected", 50, pool.getIdleConnections());
        try (Connection connection = ds.getConnection()) {
            assertNotNull(connection);
            MILLISECONDS.sleep(1500);
            assertEquals("Second total connections not as expected", 50, pool.getTotalConnections());
            assertEquals("Second idle connections not as expected", 49, pool.getIdleConnections());
        }
        assertEquals("Idle connections not as expected", 50, pool.getIdleConnections());
        SECONDS.sleep(3);
        assertEquals("Third total connections not as expected", 50, pool.getTotalConnections());
        assertEquals("Third idle connections not as expected", 50, pool.getIdleConnections());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariPool(com.zaxxer.hikari.pool.HikariPool) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 4 with HikariPool

use of com.zaxxer.hikari.pool.HikariPool in project bootique-jdbc by bootique.

the class HikariCPDerbyIT method testDerbyDataSource.

@Test
public void testDerbyDataSource() throws SQLException {
    BQRuntime runtime = testFactory.app("-c", "classpath:HikariCPDerbyIT.yml").autoLoadModules().createRuntime();
    DataSource ds4 = runtime.getInstance(DataSourceFactory.class).forName("derby4");
    assertNotNull(ds4);
    assertTrue(ds4 instanceof HikariDataSource);
    HikariDataSource hikariDS = (HikariDataSource) ds4;
    assertEquals("org.apache.derby.jdbc.EmbeddedDataSource", hikariDS.getDataSourceClassName());
    HikariPool pool = (HikariPool) hikariDS.getHikariPoolMXBean();
    assertTrue(pool.getUnwrappedDataSource().getClass().isAssignableFrom(EmbeddedDataSource.class));
    try (Connection c = hikariDS.getConnection()) {
        assertEquals("jdbc:derby:target/derby4", c.getMetaData().getURL());
    }
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariPool(com.zaxxer.hikari.pool.HikariPool) BQRuntime(io.bootique.BQRuntime) Connection(java.sql.Connection) EmbeddedDataSource(org.apache.derby.jdbc.EmbeddedDataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) EmbeddedDataSource(org.apache.derby.jdbc.EmbeddedDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 5 with HikariPool

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

the class HikariDataSource method close.

/**
 * Shutdown the DataSource and its associated pool.
 */
@Override
public void close() {
    if (isShutdown.getAndSet(true)) {
        return;
    }
    HikariPool p = pool;
    if (p != null) {
        try {
            LOGGER.info("{} - Shutdown initiated...", getPoolName());
            p.shutdown();
            LOGGER.info("{} - Shutdown completed.", getPoolName());
        } catch (InterruptedException e) {
            LOGGER.warn("{} - Interrupted during closing", getPoolName(), e);
            Thread.currentThread().interrupt();
        }
    }
}
Also used : HikariPool(com.zaxxer.hikari.pool.HikariPool)

Aggregations

HikariPool (com.zaxxer.hikari.pool.HikariPool)9 HikariDataSource (com.zaxxer.hikari.HikariDataSource)4 Connection (java.sql.Connection)4 Test (org.junit.Test)4 HikariConfig (com.zaxxer.hikari.HikariConfig)2 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)2 DriverDataSource (com.zaxxer.hikari.util.DriverDataSource)2 BQRuntime (io.bootique.BQRuntime)2 DataSourceFactory (io.bootique.jdbc.DataSourceFactory)2 DataSource (javax.sql.DataSource)2 EmbeddedDataSource (org.apache.derby.jdbc.EmbeddedDataSource)2 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)1 SQLException (java.sql.SQLException)1