use of java.sql.Connection 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 java.sql.Connection 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 java.sql.Connection 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 java.sql.Connection 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 java.sql.Connection in project HikariCP by brettwooldridge.
the class RampUpDown method rampUpDownTest.
@Test
public void rampUpDownTest() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(60);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "250");
try (HikariDataSource ds = new HikariDataSource(config)) {
ds.setIdleTimeout(1000);
HikariPool pool = getPool(ds);
// wait two housekeeping periods so we don't fail if this part of test runs too quickly
quietlySleep(500);
Assert.assertSame("Total connections not as expected", 5, pool.getTotalConnections());
Connection[] connections = new Connection[ds.getMaximumPoolSize()];
for (int i = 0; i < connections.length; i++) {
connections[i] = ds.getConnection();
}
assertSame("Total connections not as expected", 60, pool.getTotalConnections());
for (Connection connection : connections) {
connection.close();
}
quietlySleep(500);
assertSame("Total connections not as expected", 5, pool.getTotalConnections());
}
}
Aggregations