Search in sources :

Example 26 with HikariConfig

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

the class TestValidation method validateZeroConnectionTimeout.

@Test
public void validateZeroConnectionTimeout() {
    try {
        HikariConfig config = newHikariConfig();
        config.setConnectionTimeout(0);
        config.validate();
        assertEquals(Integer.MAX_VALUE, config.getConnectionTimeout());
    } catch (IllegalArgumentException ise) {
    // pass
    }
}
Also used : HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 27 with HikariConfig

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

the class TestValidation method validateInvalidLeakDetection.

@Test
public void validateInvalidLeakDetection() {
    try {
        HikariConfig config = newHikariConfig();
        config.setLeakDetectionThreshold(1000L);
        config.validate();
        fail();
    } catch (IllegalArgumentException ise) {
    // pass
    }
}
Also used : HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 28 with HikariConfig

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

the class UnwrapTest method testUnwrapConnection.

@Test
public void testUnwrapConnection() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.getConnection().close();
        assertSame("Idle connections not as expected", 1, getPool(ds).getIdleConnections());
        Connection connection = ds.getConnection();
        assertNotNull(connection);
        StubConnection unwrapped = connection.unwrap(StubConnection.class);
        assertTrue("unwrapped connection is not instance of StubConnection: " + unwrapped, (unwrapped != null && unwrapped instanceof StubConnection));
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) 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 29 with HikariConfig

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

the class TestConnections method testPopulationSlowAcquisition.

@Test
public void testPopulationSlowAcquisition() throws InterruptedException, SQLException {
    HikariConfig config = newHikariConfig();
    config.setMaximumPoolSize(20);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "1000");
    StubConnection.slowCreate = true;
    try (HikariDataSource ds = new HikariDataSource(config)) {
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
        ds.setIdleTimeout(3000);
        SECONDS.sleep(2);
        HikariPool pool = getPool(ds);
        assertSame("Total connections not as expected", 2, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 2, pool.getIdleConnections());
        try (Connection connection = ds.getConnection()) {
            assertNotNull(connection);
            SECONDS.sleep(20);
            assertSame("Second total connections not as expected", 20, pool.getTotalConnections());
            assertSame("Second idle connections not as expected", 19, pool.getIdleConnections());
        }
        assertSame("Idle connections not as expected", 20, pool.getIdleConnections());
        SECONDS.sleep(5);
        assertSame("Third total connections not as expected", 20, pool.getTotalConnections());
        assertSame("Third idle connections not as expected", 20, pool.getIdleConnections());
    } finally {
        StubConnection.slowCreate = false;
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) 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) TestElf.setConfigUnitTest(com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)

Example 30 with HikariConfig

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

the class TestConnections method testCreate.

@Test
public void testCreate() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setConnectionInitSql("SELECT 1");
    config.setReadOnly(true);
    config.setConnectionTimeout(2500);
    config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(30));
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.setLoginTimeout(10);
        assertSame(10, ds.getLoginTimeout());
        HikariPool pool = getPool(ds);
        ds.getConnection().close();
        assertSame("Total connections not as expected", 1, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
        try (Connection connection = ds.getConnection();
            PreparedStatement statement = connection.prepareStatement("SELECT * FROM device WHERE device_id=?")) {
            assertNotNull(connection);
            assertNotNull(statement);
            assertSame("Total connections not as expected", 1, pool.getTotalConnections());
            assertSame("Idle connections not as expected", 0, pool.getIdleConnections());
            statement.setInt(1, 0);
            try (ResultSet resultSet = statement.executeQuery()) {
                assertNotNull(resultSet);
                assertFalse(resultSet.next());
            }
        }
        assertSame("Total connections not as expected", 1, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test) TestElf.setConfigUnitTest(com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)

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