Search in sources :

Example 81 with HikariConfig

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

the class TestConnections method testDoubleClose.

@Test
public void testDoubleClose() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(2500);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config);
        Connection connection = ds.getConnection()) {
        connection.close();
        // should no-op
        connection.abort(null);
        assertTrue("Connection should have closed", connection.isClosed());
        assertFalse("Connection should have closed", connection.isValid(5));
        assertTrue("Expected to contain ClosedConnection, but was " + connection, connection.toString().contains("ClosedConnection"));
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) Test(org.junit.Test)

Example 82 with HikariConfig

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

the class TestConnections method testEviction.

@Test
public void testEviction() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(5);
    config.setConnectionTimeout(2500);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        Connection connection = ds.getConnection();
        HikariPool pool = getPool(ds);
        assertEquals(1, pool.getTotalConnections());
        ds.evictConnection(connection);
        assertEquals(0, pool.getTotalConnections());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) Test(org.junit.Test)

Example 83 with HikariConfig

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

the class TestConnections method testInvalidConnectionTestQuery.

@Test
public void testInvalidConnectionTestQuery() {
    class BadConnection extends StubConnection {

        /**
         * {@inheritDoc}
         */
        @Override
        public Statement createStatement() throws SQLException {
            throw new SQLException("Simulated exception in createStatement()");
        }
    }
    StubDataSource stubDataSource = new StubDataSource() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Connection getConnection() {
            return new BadConnection();
        }
    };
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(2);
    config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(3));
    config.setConnectionTestQuery("VALUES 1");
    config.setInitializationFailTimeout(TimeUnit.SECONDS.toMillis(2));
    config.setDataSource(stubDataSource);
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection ignored = ds.getConnection()) {
            fail("getConnection() should have failed");
        } catch (SQLException e) {
            assertSame("Simulated exception in createStatement()", e.getNextException().getMessage());
        }
    } catch (PoolInitializationException e) {
        assertSame("Simulated exception in createStatement()", e.getCause().getMessage());
    }
    config.setInitializationFailTimeout(0);
    try (HikariDataSource ignored = new HikariDataSource(config)) {
        fail("Initialization should have failed");
    } catch (PoolInitializationException e) {
    // passed
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) 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 84 with HikariConfig

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

the class TestConnections method testMaximumPoolLimit.

@Test
public void testMaximumPoolLimit() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(4);
    config.setConnectionTimeout(20000);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    final AtomicReference<Exception> ref = new AtomicReference<>();
    // reset counter
    StubConnection.count.set(0);
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        final HikariPool pool = getPool(ds);
        Thread[] threads = new Thread[20];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                try {
                    pool.logPoolState("Before acquire ");
                    try (Connection ignored = ds.getConnection()) {
                        pool.logPoolState("After  acquire ");
                        quietlySleep(500);
                    }
                } catch (Exception e) {
                    ref.set(e);
                }
            });
        }
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        pool.logPoolState("before check ");
        assertNull((ref.get() != null ? ref.get().toString() : ""), ref.get());
        assertSame("StubConnection count not as expected", 4, StubConnection.count.get());
    }
}
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)

Example 85 with HikariConfig

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

the class TestConnections method testMinimumIdleZero.

@Test
@SuppressWarnings("EmptyTryBlock")
public void testMinimumIdleZero() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(5);
    config.setConnectionTimeout(1000L);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config);
        Connection ignored = ds.getConnection()) {
    // passed
    } catch (SQLTransientConnectionException sqle) {
        fail("Failed to obtain connection");
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) 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