Search in sources :

Example 76 with HikariConfig

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

the class TestConnections method testMinimumIdleZero.

@Test
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 connection = ds.getConnection()) {
    // passed
    } catch (SQLTransientConnectionException sqle) {
        fail("Failed to obtain connection");
    }
}
Also used : SQLTransientConnectionException(java.sql.SQLTransientConnectionException) 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 77 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 : 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 78 with HikariConfig

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

the class TestConnections method testSuspendResume.

@Test
public void testSuspendResume() 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");
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        HikariPool pool = getPool(ds);
        while (pool.getTotalConnections() < 3) {
            quietlySleep(50);
        }
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    ds.getConnection();
                    ds.getConnection();
                } catch (Exception e) {
                    fail();
                }
            }
        });
        try (Connection c3 = ds.getConnection()) {
            assertEquals(2, pool.getIdleConnections());
            pool.suspendPool();
            t.start();
            quietlySleep(500);
            assertEquals(2, pool.getIdleConnections());
        }
        assertEquals(3, pool.getIdleConnections());
        pool.resumePool();
        quietlySleep(500);
        assertEquals(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) 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)

Example 79 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(new Runnable() {

                @Override
                public void run() {
                    try {
                        pool.logPoolState("Before acquire ");
                        try (Connection connection = ds.getConnection()) {
                            pool.logPoolState("After  acquire ");
                            quietlySleep(500);
                        }
                    } catch (Exception e) {
                        ref.set(e);
                    }
                }
            });
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].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 : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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)

Example 80 with HikariConfig

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

the class TestElf method newHikariConfig.

public static HikariConfig newHikariConfig() {
    final StackTraceElement callerStackTrace = Thread.currentThread().getStackTrace()[2];
    String poolName = callerStackTrace.getMethodName();
    if ("setup".equals(poolName)) {
        poolName = callerStackTrace.getClassName();
    }
    final HikariConfig config = new HikariConfig();
    config.setPoolName(poolName);
    return config;
}
Also used : HikariConfig(com.zaxxer.hikari.HikariConfig)

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