Search in sources :

Example 16 with HikariDataSource

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

the class TestConnectionTimeoutRetry method testConnectionRetries.

@Test
public void testConnectionRetries() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(2800);
    config.setValidationTimeout(2800);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
        stubDataSource.setThrowException(new SQLException("Connection refused"));
        long start = currentTime();
        try (Connection connection = ds.getConnection()) {
            connection.close();
            fail("Should not have been able to get a connection.");
        } catch (SQLException e) {
            long elapsed = elapsedMillis(start);
            long timeout = config.getConnectionTimeout();
            assertTrue("Didn't wait long enough for timeout", (elapsed >= timeout));
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test)

Example 17 with HikariDataSource

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

the class TestConnectionTimeoutRetry method testConnectionRetries3.

@Test
public void testConnectionRetries3() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(2);
    config.setConnectionTimeout(2800);
    config.setValidationTimeout(2800);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        final Connection connection1 = ds.getConnection();
        final Connection connection2 = ds.getConnection();
        assertNotNull(connection1);
        assertNotNull(connection2);
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
        scheduler.schedule(new Runnable() {

            @Override
            public void run() {
                try {
                    connection1.close();
                } catch (Exception e) {
                    e.printStackTrace(System.err);
                }
            }
        }, 800, MILLISECONDS);
        long start = currentTime();
        try {
            try (Connection connection3 = ds.getConnection()) {
            // close immediately
            }
            long elapsed = elapsedMillis(start);
            assertTrue("Waited too long to get a connection.", (elapsed >= 700) && (elapsed < 950));
        } catch (SQLException e) {
            fail("Should not have timed out.");
        } finally {
            scheduler.shutdownNow();
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 18 with HikariDataSource

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

the class TestConnections method testInitializationFailure1.

@Test
public void testInitializationFailure1() throws SQLException {
    StubDataSource stubDataSource = new StubDataSource();
    stubDataSource.setThrowException(new SQLException("Connection refused"));
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMinimumIdle(1);
        ds.setMaximumPoolSize(1);
        ds.setConnectionTimeout(2500);
        ds.setConnectionTestQuery("VALUES 1");
        ds.setDataSource(stubDataSource);
        try (Connection c = ds.getConnection()) {
            fail("Initialization should have failed");
        } catch (SQLException e) {
        // passed
        }
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test) TestElf.setConfigUnitTest(com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)

Example 19 with HikariDataSource

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

the class TestConnections method testMaxLifetime.

@Test
public void testMaxLifetime() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(2500);
    config.setConnectionTestQuery("VALUES 1");
    config.setInitializationFailTimeout(Long.MAX_VALUE);
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100");
    setConfigUnitTest(true);
    try (HikariDataSource ds = new HikariDataSource(config)) {
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
        ds.setMaxLifetime(700);
        HikariPool pool = getPool(ds);
        assertSame("Total connections not as expected", 0, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 0, pool.getIdleConnections());
        Connection unwrap;
        Connection unwrap2;
        try (Connection connection = ds.getConnection()) {
            unwrap = connection.unwrap(Connection.class);
            assertNotNull(connection);
            assertSame("Second total connections not as expected", 1, pool.getTotalConnections());
            assertSame("Second idle connections not as expected", 0, pool.getIdleConnections());
        }
        assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
        try (Connection connection = ds.getConnection()) {
            unwrap2 = connection.unwrap(Connection.class);
            assertSame(unwrap, unwrap2);
            assertSame("Second total connections not as expected", 1, pool.getTotalConnections());
            assertSame("Second idle connections not as expected", 0, pool.getIdleConnections());
        }
        quietlySleep(TimeUnit.SECONDS.toMillis(2));
        try (Connection connection = ds.getConnection()) {
            unwrap2 = connection.unwrap(Connection.class);
            assertNotSame("Expected a different connection", unwrap, unwrap2);
        }
        assertSame("Post total connections not as expected", 1, pool.getTotalConnections());
        assertSame("Post idle connections not as expected", 1, pool.getIdleConnections());
    } finally {
        setConfigUnitTest(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 20 with HikariDataSource

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

the class TestConnections method testBackfill.

@Test
public void testBackfill() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(4);
    config.setConnectionTimeout(1000);
    config.setInitializationFailTimeout(Long.MAX_VALUE);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    StubConnection.slowCreate = true;
    try (HikariDataSource ds = new HikariDataSource(config)) {
        HikariPool pool = getPool(ds);
        quietlySleep(1250);
        assertSame("Total connections not as expected", 1, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
        // This will take the pool down to zero
        try (Connection connection = ds.getConnection()) {
            assertNotNull(connection);
            assertSame("Total connections not as expected", 1, pool.getTotalConnections());
            assertSame("Idle connections not as expected", 0, pool.getIdleConnections());
            PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?");
            assertNotNull(statement);
            ResultSet resultSet = statement.executeQuery();
            assertNotNull(resultSet);
            try {
                statement.getMaxFieldSize();
                fail();
            } catch (Exception e) {
                assertSame(SQLException.class, e.getClass());
            }
            pool.logPoolState("testBackfill() before close...");
        // The connection will be ejected from the pool here
        }
        assertSame("Total connections not as expected", 0, pool.getTotalConnections());
        pool.logPoolState("testBackfill() after close...");
        quietlySleep(1250);
        assertSame("Total connections not as expected", 1, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
    } finally {
        StubConnection.slowCreate = false;
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) 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) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) SQLException(java.sql.SQLException) SQLTransientConnectionException(java.sql.SQLTransientConnectionException) Test(org.junit.Test) TestElf.setConfigUnitTest(com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)

Aggregations

HikariDataSource (com.zaxxer.hikari.HikariDataSource)114 Test (org.junit.Test)78 HikariConfig (com.zaxxer.hikari.HikariConfig)75 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)63 Connection (java.sql.Connection)63 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)33 StubConnection (com.zaxxer.hikari.mocks.StubConnection)29 SQLException (java.sql.SQLException)27 TestElf.setConfigUnitTest (com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)16 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)10 MetricRegistry (com.codahale.metrics.MetricRegistry)8 PreparedStatement (java.sql.PreparedStatement)6 Statement (java.sql.Statement)6 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)5 SQLTransientConnectionException (java.sql.SQLTransientConnectionException)5 Properties (java.util.Properties)5 ResultSet (java.sql.ResultSet)4 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3