Search in sources :

Example 91 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project jDialects by drinkjava2.

the class DialectTest method testGuessDialectsByConnection.

@Test
public void testGuessDialectsByConnection() {
    HikariDataSource ds = buildH2Datasource();
    String dialectName = null;
    Connection con = null;
    try {
        con = ds.getConnection();
        dialectName = Dialect.guessDialect(con).toString();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    Assert.assertEquals("H2Dialect", dialectName);
    ds.close();
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Test(org.junit.Test)

Example 92 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project jDialects by drinkjava2.

the class DialectTest method testGuessDialectsByDatasource.

@Test
public void testGuessDialectsByDatasource() {
    HikariDataSource ds = buildH2Datasource();
    String dialectName = Dialect.guessDialect(ds).toString();
    Assert.assertEquals("H2Dialect", dialectName);
    ds.close();
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Test(org.junit.Test)

Example 93 with HikariDataSource

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

the class TestSealedConfig method testSealed1.

@Test(expected = IllegalStateException.class)
public void testSealed1() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        fail("Exception should have been thrown");
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 94 with HikariDataSource

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

the class BasicPoolTest method testIdleTimeout.

@Test
public void testIdleTimeout() throws InterruptedException, SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(10);
    config.setConnectionTestQuery("SELECT 1");
    config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
    config.addDataSourceProperty("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "1000");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
        SECONDS.sleep(1);
        HikariPool pool = getPool(ds);
        getUnsealedConfig(ds).setIdleTimeout(3000);
        assertEquals("Total connections not as expected", 5, pool.getTotalConnections());
        assertEquals("Idle connections not as expected", 5, pool.getIdleConnections());
        try (Connection connection = ds.getConnection()) {
            Assert.assertNotNull(connection);
            MILLISECONDS.sleep(1500);
            assertEquals("Second total connections not as expected", 6, pool.getTotalConnections());
            assertEquals("Second idle connections not as expected", 5, pool.getIdleConnections());
        }
        assertEquals("Idle connections not as expected", 6, pool.getIdleConnections());
        SECONDS.sleep(2);
        assertEquals("Third total connections not as expected", 5, pool.getTotalConnections());
        assertEquals("Third idle connections not as expected", 5, pool.getIdleConnections());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariPool(com.zaxxer.hikari.pool.HikariPool) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 95 with HikariDataSource

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

the class HikariCPCollectorTest method connectionClosed.

@Test
public void connectionClosed() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    config.setMaximumPoolSize(1);
    StubConnection.slowCreate = true;
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection connection1 = ds.getConnection()) {
        // close immediately
        }
        assertThat(getValue("hikaricp_active_connections", "connectionClosed"), is(0.0));
        assertThat(getValue("hikaricp_idle_connections", "connectionClosed"), is(1.0));
        assertThat(getValue("hikaricp_pending_threads", "connectionClosed"), is(0.0));
        assertThat(getValue("hikaricp_connections", "connectionClosed"), is(1.0));
        assertThat(getValue("hikaricp_max_connections", "connectionClosed"), is(1.0));
        assertThat(getValue("hikaricp_min_connections", "connectionClosed"), is(1.0));
    } finally {
        StubConnection.slowCreate = false;
    }
}
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)

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