Search in sources :

Example 86 with HikariConfig

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

the class PostgresTest method testCase3.

//@Test
public void testCase3() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(3);
    config.setMaximumPoolSize(10);
    config.setConnectionTimeout(1000);
    config.setIdleTimeout(TimeUnit.SECONDS.toMillis(60));
    config.setJdbcUrl("jdbc:pgsql://localhost:5432/test");
    config.setUsername("brettw");
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        for (int i = 0; i < 10; i++) {
            new Thread() {

                public void run() {
                    try (Connection conn = ds.getConnection()) {
                        System.err.println("ERROR: should not have acquired connection.");
                    } catch (SQLException e) {
                    // expected
                    }
                }
            }.start();
        }
        quietlySleep(5000L);
        System.err.println("Now, bring the DB online.  Checking pool in 15 seconds.");
        quietlySleep(15000L);
        getPool(ds).logPoolState();
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig)

Example 87 with HikariConfig

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

the class PostgresTest method testCase2.

//@Test
public void testCase2() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(3);
    config.setMaximumPoolSize(10);
    config.setConnectionTimeout(1000);
    config.setIdleTimeout(TimeUnit.SECONDS.toMillis(60));
    config.setJdbcUrl("jdbc:pgsql://localhost:5432/test");
    config.setUsername("brettw");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection conn = ds.getConnection()) {
            System.err.println("\nGot a connection, and released it.  Now, enable the firewall.");
        }
        getPool(ds).logPoolState();
        quietlySleep(5000L);
        System.err.println("\nNow attempting another getConnection(), expecting a timeout...");
        long start = currentTime();
        try (Connection conn = ds.getConnection()) {
            System.err.println("\nOpps, got a connection.  Did you enable the firewall? " + conn);
            fail("Opps, got a connection.  Did you enable the firewall?");
        } catch (SQLException e) {
            assertTrue("Timeout less than expected " + elapsedMillis(start) + "ms", elapsedMillis(start) > 5000);
        }
        System.err.println("\nOk, so far so good.  Now, disable the firewall again.  Attempting connection in 5 seconds...");
        quietlySleep(5000L);
        getPool(ds).logPoolState();
        try (Connection conn = ds.getConnection()) {
            System.err.println("\nGot a connection, and released it.");
        }
    }
    System.err.println("\nPassed.");
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig)

Example 88 with HikariConfig

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

the class ShutdownTest method testThreadedShutdown.

@Test
public void testThreadedShutdown() throws Exception {
    final HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(5);
    config.setConnectionTimeout(1000);
    config.setValidationTimeout(1000);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    for (int i = 0; i < 4; i++) {
        try (final HikariDataSource ds = new HikariDataSource(config)) {
            Thread t = new Thread() {

                @Override
                public void run() {
                    try (Connection connection = ds.getConnection()) {
                        for (int i = 0; i < 10; i++) {
                            Connection connection2 = null;
                            try {
                                connection2 = ds.getConnection();
                                PreparedStatement stmt = connection2.prepareStatement("SOMETHING");
                                UtilityElf.quietlySleep(20);
                                stmt.getMaxFieldSize();
                            } catch (SQLException e) {
                                try {
                                    if (connection2 != null) {
                                        connection2.close();
                                    }
                                } catch (SQLException e2) {
                                    if (e2.getMessage().contains("shutdown") || e2.getMessage().contains("evicted")) {
                                        break;
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        Assert.fail(e.getMessage());
                    } finally {
                        ds.close();
                    }
                }
            };
            t.start();
            Thread t2 = new Thread() {

                @Override
                public void run() {
                    UtilityElf.quietlySleep(100);
                    try {
                        ds.close();
                    } catch (IllegalStateException e) {
                        Assert.fail(e.getMessage());
                    }
                }
            };
            t2.start();
            t.join();
            t2.join();
            ds.close();
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) PreparedStatement(java.sql.PreparedStatement) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 89 with HikariConfig

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

the class ShutdownTest method testShutdown5.

@Test
public void testShutdown5() throws SQLException {
    Assert.assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(5);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        HikariPool pool = getPool(ds);
        Connection[] connections = new Connection[5];
        for (int i = 0; i < 5; i++) {
            connections[i] = ds.getConnection();
        }
        Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
        ds.close();
        Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
        Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
        Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
    }
}
Also used : 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)

Example 90 with HikariConfig

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

the class ShutdownTest method testShutdownDuringInit.

@Test
public void testShutdownDuringInit() throws Exception {
    final HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(5);
    config.setConnectionTimeout(1000);
    config.setValidationTimeout(1000);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        StubConnection.slowCreate = true;
        UtilityElf.quietlySleep(3000L);
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

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