use of com.zaxxer.hikari.pool.HikariPool in project HikariCP by brettwooldridge.
the class HikariDataSource method getConnection.
// ***********************************************************************
// DataSource methods
// ***********************************************************************
/**
* {@inheritDoc}
*/
@Override
public Connection getConnection() throws SQLException {
if (isClosed()) {
throw new SQLException("HikariDataSource " + this + " has been closed.");
}
if (fastPathPool != null) {
return fastPathPool.getConnection();
}
// See http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
HikariPool result = pool;
if (result == null) {
synchronized (this) {
result = pool;
if (result == null) {
validate();
LOGGER.info("{} - Starting...", getPoolName());
try {
pool = result = new HikariPool(this);
this.seal();
} catch (PoolInitializationException pie) {
if (pie.getCause() instanceof SQLException) {
throw (SQLException) pie.getCause();
} else {
throw pie;
}
}
LOGGER.info("{} - Start completed.", getPoolName());
}
}
}
return result.getConnection();
}
use of com.zaxxer.hikari.pool.HikariPool in project HikariCP by brettwooldridge.
the class HikariDataSource method setMetricsTrackerFactory.
/**
* {@inheritDoc}
*/
@Override
public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory) {
boolean isAlreadySet = getMetricsTrackerFactory() != null;
super.setMetricsTrackerFactory(metricsTrackerFactory);
HikariPool p = pool;
if (p != null) {
if (isAlreadySet) {
throw new IllegalStateException("MetricsTrackerFactory can only be set one time");
} else {
p.setMetricsTrackerFactory(super.getMetricsTrackerFactory());
}
}
}
use of com.zaxxer.hikari.pool.HikariPool in project HikariCP by brettwooldridge.
the class BasicPoolTest method testIdleTimeout2.
@Test
public void testIdleTimeout2() throws InterruptedException, SQLException {
HikariConfig config = newHikariConfig();
config.setMaximumPoolSize(50);
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", 50, pool.getTotalConnections());
assertEquals("Idle connections not as expected", 50, pool.getIdleConnections());
try (Connection connection = ds.getConnection()) {
assertNotNull(connection);
MILLISECONDS.sleep(1500);
assertEquals("Second total connections not as expected", 50, pool.getTotalConnections());
assertEquals("Second idle connections not as expected", 49, pool.getIdleConnections());
}
assertEquals("Idle connections not as expected", 50, pool.getIdleConnections());
SECONDS.sleep(3);
assertEquals("Third total connections not as expected", 50, pool.getTotalConnections());
assertEquals("Third idle connections not as expected", 50, pool.getIdleConnections());
}
}
use of com.zaxxer.hikari.pool.HikariPool in project bootique-jdbc by bootique.
the class HikariCPDerbyIT method testDerbyDataSource.
@Test
public void testDerbyDataSource() throws SQLException {
BQRuntime runtime = testFactory.app("-c", "classpath:HikariCPDerbyIT.yml").autoLoadModules().createRuntime();
DataSource ds4 = runtime.getInstance(DataSourceFactory.class).forName("derby4");
assertNotNull(ds4);
assertTrue(ds4 instanceof HikariDataSource);
HikariDataSource hikariDS = (HikariDataSource) ds4;
assertEquals("org.apache.derby.jdbc.EmbeddedDataSource", hikariDS.getDataSourceClassName());
HikariPool pool = (HikariPool) hikariDS.getHikariPoolMXBean();
assertTrue(pool.getUnwrappedDataSource().getClass().isAssignableFrom(EmbeddedDataSource.class));
try (Connection c = hikariDS.getConnection()) {
assertEquals("jdbc:derby:target/derby4", c.getMetaData().getURL());
}
}
use of com.zaxxer.hikari.pool.HikariPool in project HikariCP by brettwooldridge.
the class HikariDataSource method close.
/**
* Shutdown the DataSource and its associated pool.
*/
@Override
public void close() {
if (isShutdown.getAndSet(true)) {
return;
}
HikariPool p = pool;
if (p != null) {
try {
LOGGER.info("{} - Shutdown initiated...", getPoolName());
p.shutdown();
LOGGER.info("{} - Shutdown completed.", getPoolName());
} catch (InterruptedException e) {
LOGGER.warn("{} - Interrupted during closing", getPoolName(), e);
Thread.currentThread().interrupt();
}
}
}
Aggregations