Search in sources :

Example 66 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class BasicPoolTest method testIdleTimeout.

@Test
public void testIdleTimeout() throws InterruptedException, SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(10);
    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);
        ds.setIdleTimeout(3000);
        assertEquals("Total connections not as expected", 5, pool.getTotalConnections());
        assertEquals("Idle connections not as expected", 5, pool.getIdleConnections());
        try (Connection connection = ds.getConnection()) {
            Assert.assertNotNull(connection);
            MILLISECONDS.sleep(1500);
            assertEquals("Second total connections not as expected", 6, pool.getTotalConnections());
            assertEquals("Second idle connections not as expected", 5, pool.getIdleConnections());
        }
        assertEquals("Idle connections not as expected", 6, pool.getIdleConnections());
        SECONDS.sleep(2);
        assertEquals("Third total connections not as expected", 5, pool.getTotalConnections());
        assertEquals("Third idle connections not as expected", 5, 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 67 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class BasicPoolTest method setup.

@Before
public void setup() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(2);
    config.setConnectionTestQuery("SELECT 1");
    config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
    config.addDataSourceProperty("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    try (HikariDataSource ds = new HikariDataSource(config);
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement()) {
        stmt.executeUpdate("DROP TABLE IF EXISTS basic_pool_test");
        stmt.executeUpdate("CREATE TABLE basic_pool_test (" + "id INTEGER NOT NULL IDENTITY PRIMARY KEY, " + "timestamp TIMESTAMP, " + "string VARCHAR(128), " + "string_from_number NUMERIC " + ")");
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Before(org.junit.Before)

Example 68 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class HikariCPCollectorTest method noConnection.

@Test
public void noConnection() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    StubConnection.slowCreate = true;
    try (HikariDataSource ds = new HikariDataSource(config)) {
        assertThat(getValue("hikaricp_active_connections", "noConnection"), is(0.0));
        assertThat(getValue("hikaricp_idle_connections", "noConnection"), is(0.0));
        assertThat(getValue("hikaricp_pending_threads", "noConnection"), is(0.0));
        assertThat(getValue("hikaricp_connections", "noConnection"), is(0.0));
    } finally {
        StubConnection.slowCreate = false;
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 69 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class HikariCPCollectorTest method connectionClosed.

@Test
public void connectionClosed() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    config.setMaximumPoolSize(1);
    StubConnection.slowCreate = true;
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection connection1 = ds.getConnection()) {
        // close immediately
        }
        assertThat(getValue("hikaricp_active_connections", "connectionClosed"), is(0.0));
        assertThat(getValue("hikaricp_idle_connections", "connectionClosed"), is(1.0));
        assertThat(getValue("hikaricp_pending_threads", "connectionClosed"), is(0.0));
        assertThat(getValue("hikaricp_connections", "connectionClosed"), is(1.0));
    } finally {
        StubConnection.slowCreate = false;
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 70 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class TestConcurrentBag method setup.

@BeforeClass
public static void setup() {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(2);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    ds = new HikariDataSource(config);
    pool = getPool(ds);
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) BeforeClass(org.junit.BeforeClass)

Aggregations

HikariConfig (com.zaxxer.hikari.HikariConfig)101 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)85 HikariDataSource (com.zaxxer.hikari.HikariDataSource)75 Test (org.junit.Test)75 Connection (java.sql.Connection)48 StubConnection (com.zaxxer.hikari.mocks.StubConnection)28 SQLException (java.sql.SQLException)23 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)17 TestElf.setConfigUnitTest (com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)15 Properties (java.util.Properties)10 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)8 PreparedStatement (java.sql.PreparedStatement)6 DataSource (javax.sql.DataSource)6 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SQLTransientConnectionException (java.sql.SQLTransientConnectionException)5 Statement (java.sql.Statement)5 PrintStream (java.io.PrintStream)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 ResultSet (java.sql.ResultSet)3