use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class PrometheusMetricsTrackerTest method testMultiplePoolName.
@Test
public void testMultiplePoolName() throws Exception {
String[] labelNames = { POOL_LABEL_NAME };
HikariConfig config = newHikariConfig();
config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
config.setPoolName("first");
config.setJdbcUrl("jdbc:h2:mem:");
config.setMaximumPoolSize(2);
config.setConnectionTimeout(250);
String[] labelValues1 = { config.getPoolName() };
try (HikariDataSource ignored = new HikariDataSource(config)) {
assertThat(collectorRegistry.getSampleValue("hikaricp_connection_timeout_total", labelNames, labelValues1), is(0.0));
HikariConfig config2 = newHikariConfig();
config2.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
config2.setPoolName("second");
config2.setJdbcUrl("jdbc:h2:mem:");
config2.setMaximumPoolSize(4);
config2.setConnectionTimeout(250);
String[] labelValues2 = { config2.getPoolName() };
try (HikariDataSource ignored2 = new HikariDataSource(config2)) {
assertThat(collectorRegistry.getSampleValue("hikaricp_connection_timeout_total", labelNames, labelValues2), is(0.0));
}
}
}
use of com.zaxxer.hikari.HikariConfig 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.HikariConfig 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.HikariConfig 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.HikariConfig 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;
}
}
Aggregations