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));
}
}
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);
}
}
}
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) {} });
}
}
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;
}
}
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;
}
}
Aggregations