Search in sources :

Example 61 with HikariDataSource

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

the class TestMBean method testMBeanReporting.

@Test
public void testMBeanReporting() throws SQLException, InterruptedException, MalformedObjectNameException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(3);
    config.setMaximumPoolSize(5);
    config.setRegisterMbeans(true);
    config.setConnectionTimeout(2800);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        getUnsealedConfig(ds).setIdleTimeout(3000);
        TimeUnit.SECONDS.sleep(1);
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (testMBeanReporting)");
        HikariPoolMXBean hikariPoolMXBean = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
        assertEquals(0, hikariPoolMXBean.getActiveConnections());
        assertEquals(3, hikariPoolMXBean.getIdleConnections());
        try (Connection ignored = ds.getConnection()) {
            assertEquals(1, hikariPoolMXBean.getActiveConnections());
            TimeUnit.SECONDS.sleep(1);
            assertEquals(3, hikariPoolMXBean.getIdleConnections());
            assertEquals(4, hikariPoolMXBean.getTotalConnections());
        }
        TimeUnit.SECONDS.sleep(2);
        assertEquals(0, hikariPoolMXBean.getActiveConnections());
        assertEquals(3, hikariPoolMXBean.getIdleConnections());
        assertEquals(3, hikariPoolMXBean.getTotalConnections());
    } finally {
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariPoolMXBean(com.zaxxer.hikari.HikariPoolMXBean) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 62 with HikariDataSource

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

the class TestMetrics method testMetricRegistrySubclassIsAllowed.

@Test
public void testMetricRegistrySubclassIsAllowed() {
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMaximumPoolSize(1);
        ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        ds.setMetricRegistry(new MetricRegistry() {

            @Override
            public Timer timer(String name) {
                return super.timer(name);
            }
        });
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Timer(com.codahale.metrics.Timer) MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.Test)

Example 63 with HikariDataSource

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

the class TestMetrics method testMetricUsage.

@Test
public void testMetricUsage() throws SQLException {
    assumeFalse(System.getProperty("os.name").contains("Windows"));
    MetricRegistry metricRegistry = new MetricRegistry();
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setMetricRegistry(metricRegistry);
    config.setInitializationFailTimeout(0);
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection connection = ds.getConnection()) {
            UtilityElf.quietlySleep(250L);
        }
        Histogram histo = metricRegistry.getHistograms(new MetricFilter() {

            /**
             * {@inheritDoc}
             */
            @Override
            public boolean matches(String name, Metric metric) {
                return name.equals(MetricRegistry.name("testMetricUsage", "pool", "Usage"));
            }
        }).values().iterator().next();
        assertEquals(1, histo.getCount());
        double seventyFifth = histo.getSnapshot().get75thPercentile();
        assertTrue("Seventy-fith percentile less than 250ms: " + seventyFifth, seventyFifth >= 250.0);
    }
}
Also used : Histogram(com.codahale.metrics.Histogram) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) MetricFilter(com.codahale.metrics.MetricFilter) MetricRegistry(com.codahale.metrics.MetricRegistry) Connection(java.sql.Connection) Metric(com.codahale.metrics.Metric) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 64 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project waltz by khartec.

the class DIConfiguration method dataSource.

@Bean
public DataSource dataSource() {
    HikariConfig dsConfig = new HikariConfig();
    dsConfig.setJdbcUrl(dbUrl);
    dsConfig.setUsername(dbUser);
    dsConfig.setPassword(dbPassword);
    dsConfig.setDriverClassName(dbDriver);
    dsConfig.setMaximumPoolSize(dbPoolMax);
    dsConfig.setMinimumIdle(dbPoolMin);
    return new HikariDataSource(dsConfig);
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig)

Example 65 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project hive by apache.

the class TestDataSourceProviderFactory method testSetHikariCpStringProperty.

@Test
public void testSetHikariCpStringProperty() throws SQLException {
    MetastoreConf.setVar(conf, ConfVars.CONNECTION_POOLING_TYPE, HikariCPDataSourceProvider.HIKARI);
    conf.set(HikariCPDataSourceProvider.HIKARI + ".connectionInitSql", "select 1 from dual");
    conf.set(HikariCPDataSourceProvider.HIKARI + ".initializationFailTimeout", "-1");
    DataSourceProvider dsp = DataSourceProviderFactory.getDataSourceProvider(conf);
    Assert.assertNotNull(dsp);
    DataSource ds = dsp.create(conf);
    Assert.assertTrue(ds instanceof HikariDataSource);
    Assert.assertEquals("select 1 from dual", ((HikariDataSource) ds).getConnectionInitSql());
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) BoneCPDataSource(com.jolbox.bonecp.BoneCPDataSource) Test(org.junit.Test) MetastoreUnitTest(org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest)

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