Search in sources :

Example 6 with HikariConfig

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

the class TestConnectionCloseBlocking method testConnectionCloseBlocking.

// @Test
public void testConnectionCloseBlocking() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(1500);
    config.setDataSource(new CustomMockDataSource());
    long start = currentTime();
    try (HikariDataSource ds = new HikariDataSource(config);
        Connection connection = ds.getConnection()) {
        connection.close();
        // Hikari only checks for validity for connections with lastAccess > 1000 ms so we sleep for 1001 ms to force
        // Hikari to do a connection validation which will fail and will trigger the connection to be closed
        quietlySleep(1100L);
        shouldFail = true;
        // on physical connection close we sleep 2 seconds
        try (Connection connection2 = ds.getConnection()) {
            assertTrue("Waited longer than timeout", (elapsedMillis(start) < config.getConnectionTimeout()));
        }
    } catch (SQLException e) {
        assertTrue("getConnection failed because close connection took longer than timeout", (elapsedMillis(start) < config.getConnectionTimeout()));
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig)

Example 7 with HikariConfig

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

the class TestConnectionTimeoutRetry method testConnectionRetries2.

@Test
public void testConnectionRetries2() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(2800);
    config.setValidationTimeout(2800);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        final StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
        stubDataSource.setThrowException(new SQLException("Connection refused"));
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.schedule(new Runnable() {

            @Override
            public void run() {
                stubDataSource.setThrowException(null);
            }
        }, 300, TimeUnit.MILLISECONDS);
        long start = currentTime();
        try {
            try (Connection connection = ds.getConnection()) {
            // close immediately
            }
            long elapsed = elapsedMillis(start);
            assertTrue("Connection returned too quickly, something is wrong.", elapsed > 250);
            assertTrue("Waited too long to get a connection.", elapsed < config.getConnectionTimeout());
        } catch (SQLException e) {
            fail("Should not have timed out: " + e.getMessage());
        } 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) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test)

Example 8 with HikariConfig

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

the class TestConnectionTimeoutRetry method testConnectionIdleFill.

@Test
public void testConnectionIdleFill() throws Exception {
    StubConnection.slowCreate = false;
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(10);
    config.setConnectionTimeout(2000);
    config.setValidationTimeout(2000);
    config.setConnectionTestQuery("VALUES 2");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "400");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, true);
    setSlf4jTargetStream(HikariPool.class, ps);
    try (HikariDataSource ds = new HikariDataSource(config)) {
        setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
        HikariPool pool = getPool(ds);
        try (Connection connection1 = ds.getConnection();
            Connection connection2 = ds.getConnection();
            Connection connection3 = ds.getConnection();
            Connection connection4 = ds.getConnection();
            Connection connection5 = ds.getConnection();
            Connection connection6 = ds.getConnection();
            Connection connection7 = ds.getConnection()) {
            sleep(1300);
            assertSame("Total connections not as expected", 10, pool.getTotalConnections());
            assertSame("Idle connections not as expected", 3, pool.getIdleConnections());
        }
        assertSame("Total connections not as expected", 10, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 10, pool.getIdleConnections());
    }
}
Also used : PrintStream(java.io.PrintStream) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 9 with HikariConfig

use of com.zaxxer.hikari.HikariConfig 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 10 with HikariConfig

use of com.zaxxer.hikari.HikariConfig 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)

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