Search in sources :

Example 51 with HikariDataSource

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

the class PrometheusMetricsTrackerTest method recordConnectionTimeout.

@Test
public void recordConnectionTimeout() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
    config.setJdbcUrl("jdbc:h2:mem:");
    config.setMaximumPoolSize(2);
    config.setConnectionTimeout(250);
    String[] labelNames = { POOL_LABEL_NAME };
    String[] labelValues = { config.getPoolName() };
    try (HikariDataSource hikariDataSource = new HikariDataSource(config)) {
        try (Connection connection1 = hikariDataSource.getConnection();
            Connection connection2 = hikariDataSource.getConnection()) {
            try (Connection connection3 = hikariDataSource.getConnection()) {
            } catch (SQLTransientConnectionException ignored) {
            }
        }
        Double total = collectorRegistry.getSampleValue("hikaricp_connection_timeout_total", labelNames, labelValues);
        assertThat(total, is(1.0));
    }
}
Also used : SQLTransientConnectionException(java.sql.SQLTransientConnectionException) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 52 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);
            setSlf4jLogLevel(HikariPool.class, Level.INFO);
        }
    }
}
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)

Example 53 with HikariDataSource

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

the class PostgresTest method testCase4.

// @Test
public void testCase4() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(15);
    config.setConnectionTimeout(10000);
    config.setIdleTimeout(TimeUnit.MINUTES.toMillis(1));
    config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2));
    config.setRegisterMbeans(true);
    config.setJdbcUrl("jdbc:postgresql://localhost:5432/netld");
    config.setUsername("brettw");
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        countdown(20);
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            threads.add(new Thread() {

                public void run() {
                    UtilityElf.quietlySleep((long) (Math.random() * 2500L));
                    final long start = currentTime();
                    do {
                        try (Connection conn = ds.getConnection();
                            Statement stmt = conn.createStatement()) {
                            try (ResultSet rs = stmt.executeQuery("SELECT * FROM device WHERE device_id=0 ORDER BY device_id LIMIT 1 OFFSET 0")) {
                                rs.next();
                            }
                            // Math.max(50L, (long)(Math.random() * 250L)));
                            UtilityElf.quietlySleep(100L);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        // throw new RuntimeException(e);
                        }
                    // UtilityElf.quietlySleep(10L); //Math.max(50L, (long)(Math.random() * 250L)));
                    } while (elapsedMillis(start) < TimeUnit.MINUTES.toMillis(5));
                }
            });
        }
    // threads.forEach(t -> t.start());
    // threads.forEach(t -> { try { t.join(); } catch (InterruptedException e) {} });
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig)

Example 54 with HikariDataSource

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

the class TestConnections method testPopulationSlowAcquisition.

@Test
public void testPopulationSlowAcquisition() throws InterruptedException, SQLException {
    HikariConfig config = newHikariConfig();
    config.setMaximumPoolSize(20);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "1000");
    StubConnection.slowCreate = true;
    try (HikariDataSource ds = new HikariDataSource(config)) {
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
        getUnsealedConfig(ds).setIdleTimeout(3000);
        SECONDS.sleep(2);
        HikariPool pool = getPool(ds);
        assertSame("Total connections not as expected", 2, pool.getTotalConnections());
        assertSame("Idle connections not as expected", 2, pool.getIdleConnections());
        try (Connection connection = ds.getConnection()) {
            assertNotNull(connection);
            SECONDS.sleep(20);
            assertSame("Second total connections not as expected", 20, pool.getTotalConnections());
            assertSame("Second idle connections not as expected", 19, pool.getIdleConnections());
        }
        assertSame("Idle connections not as expected", 20, pool.getIdleConnections());
        SECONDS.sleep(5);
        assertSame("Third total connections not as expected", 20, pool.getTotalConnections());
        assertSame("Third idle connections not as expected", 20, pool.getIdleConnections());
    } finally {
        StubConnection.slowCreate = 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 HikariDataSource

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

Aggregations

HikariDataSource (com.zaxxer.hikari.HikariDataSource)185 HikariConfig (com.zaxxer.hikari.HikariConfig)109 Test (org.junit.Test)106 Connection (java.sql.Connection)61 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)57 SQLException (java.sql.SQLException)33 StubConnection (com.zaxxer.hikari.mocks.StubConnection)30 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)18 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)11 MetricRegistry (com.codahale.metrics.MetricRegistry)9 DataSource (javax.sql.DataSource)8 Statement (java.sql.Statement)7 ArrayList (java.util.ArrayList)7 FacesMessage (javax.faces.application.FacesMessage)7 BoneCPDataSource (com.jolbox.bonecp.BoneCPDataSource)6 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)6 Properties (java.util.Properties)6 ConnexionTest (connexion.ConnexionTest)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5