Search in sources :

Example 51 with HikariConfig

use of com.zaxxer.hikari.HikariConfig 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 : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) Test(org.junit.Test)

Example 52 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 : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) Test(org.junit.Test)

Example 53 with HikariConfig

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

the class TestConnections method testInitializationFailure2.

@Test
public void testInitializationFailure2() throws SQLException {
    StubDataSource stubDataSource = new StubDataSource();
    stubDataSource.setThrowException(new SQLException("Connection refused"));
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSource(stubDataSource);
    try (HikariDataSource ds = new HikariDataSource(config);
        Connection ignored = ds.getConnection()) {
        fail("Initialization should have failed");
    } catch (PoolInitializationException e) {
    // passed
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) Test(org.junit.Test)

Example 54 with HikariConfig

use of com.zaxxer.hikari.HikariConfig 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");
        getUnsealedConfig(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 : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) Test(org.junit.Test)

Example 55 with HikariConfig

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

the class TestConnections method testSuspendResumeWithThrow.

@Test
public void testSuspendResumeWithThrow() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(3);
    config.setMaximumPoolSize(3);
    config.setConnectionTimeout(2500);
    config.setAllowPoolSuspension(true);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.throwIfSuspended", "true");
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        HikariPool pool = getPool(ds);
        while (pool.getTotalConnections() < 3) {
            quietlySleep(50);
        }
        AtomicReference<Exception> exception = new AtomicReference<>();
        Thread t = new Thread(() -> {
            try {
                ds.getConnection();
                ds.getConnection();
            } catch (Exception e) {
                exception.set(e);
            }
        });
        try (Connection ignored = ds.getConnection()) {
            assertEquals(2, pool.getIdleConnections());
            pool.suspendPool();
            t.start();
            quietlySleep(500);
            assertEquals(SQLTransientException.class, exception.get().getClass());
            assertEquals(2, pool.getIdleConnections());
        }
        assertEquals(3, pool.getIdleConnections());
        pool.resumePool();
        try (Connection ignored = ds.getConnection()) {
            assertEquals(2, pool.getIdleConnections());
        }
    } finally {
        System.getProperties().remove("com.zaxxer.hikari.throwIfSuspended");
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) HikariConfig(com.zaxxer.hikari.HikariConfig) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) Test(org.junit.Test)

Aggregations

HikariConfig (com.zaxxer.hikari.HikariConfig)136 HikariDataSource (com.zaxxer.hikari.HikariDataSource)109 Test (org.junit.Test)82 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)79 Connection (java.sql.Connection)41 StubConnection (com.zaxxer.hikari.mocks.StubConnection)29 SQLException (java.sql.SQLException)26 Properties (java.util.Properties)11 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)9 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)6 FacesMessage (javax.faces.application.FacesMessage)6 DataSource (javax.sql.DataSource)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Statement (java.sql.Statement)5 PrintStream (java.io.PrintStream)4 PreparedStatement (java.sql.PreparedStatement)4 Connexion (mom.trd.opentheso.bdd.helper.Connexion)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)3 File (java.io.File)3