Search in sources :

Example 21 with HikariDataSource

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

the class IsolationTest method testNonIsolation.

@Test
public void testNonIsolation() throws SQLException {
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMinimumIdle(1);
        ds.setMaximumPoolSize(1);
        ds.setIsolateInternalQueries(false);
        ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        try (Connection connection = ds.getConnection()) {
            connection.close();
            try (Connection connection2 = ds.getConnection()) {
                connection2.close();
                assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
            }
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 22 with HikariDataSource

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

the class IsolationTest method testIsolation.

@Test
public void testIsolation() throws SQLException {
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMinimumIdle(1);
        ds.setMaximumPoolSize(1);
        ds.setIsolateInternalQueries(true);
        ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        try (Connection connection = ds.getConnection()) {
            connection.close();
            try (Connection connection2 = ds.getConnection()) {
                connection2.close();
                assertNotSame(connection, connection2);
                assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
            }
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 23 with HikariDataSource

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

the class JdbcDriverTest method driverTest1.

@Test
public void driverTest1() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
    config.setJdbcUrl("jdbc:stub");
    config.addDataSourceProperty("user", "bart");
    config.addDataSourceProperty("password", "simpson");
    ds = new HikariDataSource(config);
    assertTrue(ds.isWrapperFor(DriverDataSource.class));
    DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
    assertNotNull(unwrap);
    try (Connection connection = ds.getConnection()) {
    // test that getConnection() succeeds
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 24 with HikariDataSource

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

the class MetricsTrackerTest method connectionTimeoutIsRecorded.

@Test(expected = SQLTransientConnectionException.class)
public void connectionTimeoutIsRecorded() throws Exception {
    int timeoutMillis = 1000;
    int timeToCreateNewConnectionMillis = timeoutMillis * 2;
    StubDataSource stubDataSource = new StubDataSource();
    stubDataSource.setConnectionAcquistionTime(timeToCreateNewConnectionMillis);
    StubMetricsTracker metricsTracker = new StubMetricsTracker();
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMinimumIdle(0);
        ds.setMaximumPoolSize(1);
        ds.setConnectionTimeout(timeoutMillis);
        ds.setDataSource(stubDataSource);
        ds.setMetricsTrackerFactory((poolName, poolStats) -> metricsTracker);
        try (Connection c = ds.getConnection()) {
            fail("Connection shouldn't have been successfully created due to configured connection timeout");
        } finally {
            // assert that connection timeout was measured
            assertThat(metricsTracker.connectionTimeoutRecorded, is(true));
            // assert that measured time to acquire connection should be roughly equal or greater than the configured connection timeout time
            assertTrue(metricsTracker.connectionAcquiredNanos >= TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS));
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) Connection(java.sql.Connection) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test)

Example 25 with HikariDataSource

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

the class MiscTest method testLeakDetection.

@Test
public void testLeakDetection() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (PrintStream ps = new PrintStream(baos, true)) {
        setSlf4jTargetStream(Class.forName("com.zaxxer.hikari.pool.ProxyLeakTask"), ps);
        setConfigUnitTest(true);
        HikariConfig config = newHikariConfig();
        config.setMinimumIdle(0);
        config.setMaximumPoolSize(4);
        config.setThreadFactory(Executors.defaultThreadFactory());
        config.setMetricRegistry(null);
        config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(1));
        config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        try (HikariDataSource ds = new HikariDataSource(config)) {
            setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
            getPool(ds).logPoolState();
            try (Connection connection = ds.getConnection()) {
                quietlySleep(SECONDS.toMillis(4));
                connection.close();
                quietlySleep(SECONDS.toMillis(1));
                ps.close();
                String s = new String(baos.toByteArray());
                assertNotNull("Exception string was null", s);
                assertTrue("Expected exception to contain 'Connection leak detection' but contains *" + s + "*", s.contains("Connection leak detection"));
            }
        } finally {
            setConfigUnitTest(false);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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)

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