use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class IsolationTest method testNonIsolation.
@Test
public void testNonIsolation() throws SQLException {
try (HikariDataSource ds = newHikariDataSource()) {
ds.setMinimumIdle(1);
ds.setMaximumPoolSize(1);
ds.setIsolateInternalQueries(false);
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (Connection connection = ds.getConnection()) {
connection.close();
try (Connection connection2 = ds.getConnection()) {
connection2.close();
assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
}
}
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class IsolationTest method testIsolation.
@Test
public void testIsolation() throws SQLException {
try (HikariDataSource ds = newHikariDataSource()) {
ds.setMinimumIdle(1);
ds.setMaximumPoolSize(1);
ds.setIsolateInternalQueries(true);
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (Connection connection = ds.getConnection()) {
connection.close();
try (Connection connection2 = ds.getConnection()) {
connection2.close();
assertNotSame(connection, connection2);
assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
}
}
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class JdbcDriverTest method driverTest1.
@Test
public void driverTest1() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
config.setJdbcUrl("jdbc:stub");
config.addDataSourceProperty("user", "bart");
config.addDataSourceProperty("password", "simpson");
ds = new HikariDataSource(config);
assertTrue(ds.isWrapperFor(DriverDataSource.class));
DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
assertNotNull(unwrap);
try (Connection connection = ds.getConnection()) {
// test that getConnection() succeeds
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class MetricsTrackerTest method connectionTimeoutIsRecorded.
@Test(expected = SQLTransientConnectionException.class)
public void connectionTimeoutIsRecorded() throws Exception {
int timeoutMillis = 1000;
int timeToCreateNewConnectionMillis = timeoutMillis * 2;
StubDataSource stubDataSource = new StubDataSource();
stubDataSource.setConnectionAcquistionTime(timeToCreateNewConnectionMillis);
StubMetricsTracker metricsTracker = new StubMetricsTracker();
try (HikariDataSource ds = newHikariDataSource()) {
ds.setMinimumIdle(0);
ds.setMaximumPoolSize(1);
ds.setConnectionTimeout(timeoutMillis);
ds.setDataSource(stubDataSource);
ds.setMetricsTrackerFactory((poolName, poolStats) -> metricsTracker);
try (Connection c = ds.getConnection()) {
fail("Connection shouldn't have been successfully created due to configured connection timeout");
} finally {
// assert that connection timeout was measured
assertThat(metricsTracker.connectionTimeoutRecorded, is(true));
// assert that measured time to acquire connection should be roughly equal or greater than the configured connection timeout time
assertTrue(metricsTracker.connectionAcquiredNanos >= TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS));
}
}
}
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);
}
}
}
Aggregations