use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class TestConnectionTimeoutRetry method testConnectionRetries.
@Test
public void testConnectionRetries() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setConnectionTimeout(2800);
config.setValidationTimeout(2800);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused"));
long start = currentTime();
try (Connection connection = ds.getConnection()) {
connection.close();
fail("Should not have been able to get a connection.");
} catch (SQLException e) {
long elapsed = elapsedMillis(start);
long timeout = config.getConnectionTimeout();
assertTrue("Didn't wait long enough for timeout", (elapsed >= timeout));
}
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class TestConnectionTimeoutRetry method testConnectionRetries3.
@Test
public void testConnectionRetries3() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(2);
config.setConnectionTimeout(2800);
config.setValidationTimeout(2800);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
final Connection connection1 = ds.getConnection();
final Connection connection2 = ds.getConnection();
assertNotNull(connection1);
assertNotNull(connection2);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
scheduler.schedule(new Runnable() {
@Override
public void run() {
try {
connection1.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}, 800, MILLISECONDS);
long start = currentTime();
try {
try (Connection connection3 = ds.getConnection()) {
// close immediately
}
long elapsed = elapsedMillis(start);
assertTrue("Waited too long to get a connection.", (elapsed >= 700) && (elapsed < 950));
} catch (SQLException e) {
fail("Should not have timed out.");
} finally {
scheduler.shutdownNow();
}
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class TestConnections method testInitializationFailure1.
@Test
public void testInitializationFailure1() throws SQLException {
StubDataSource stubDataSource = new StubDataSource();
stubDataSource.setThrowException(new SQLException("Connection refused"));
try (HikariDataSource ds = newHikariDataSource()) {
ds.setMinimumIdle(1);
ds.setMaximumPoolSize(1);
ds.setConnectionTimeout(2500);
ds.setConnectionTestQuery("VALUES 1");
ds.setDataSource(stubDataSource);
try (Connection c = ds.getConnection()) {
fail("Initialization should have failed");
} catch (SQLException e) {
// passed
}
}
}
use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.
the class TestConnections method testMaxLifetime.
@Test
public void testMaxLifetime() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setConnectionTimeout(2500);
config.setConnectionTestQuery("VALUES 1");
config.setInitializationFailTimeout(Long.MAX_VALUE);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100");
setConfigUnitTest(true);
try (HikariDataSource ds = new HikariDataSource(config)) {
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
ds.setMaxLifetime(700);
HikariPool pool = getPool(ds);
assertSame("Total connections not as expected", 0, pool.getTotalConnections());
assertSame("Idle connections not as expected", 0, pool.getIdleConnections());
Connection unwrap;
Connection unwrap2;
try (Connection connection = ds.getConnection()) {
unwrap = connection.unwrap(Connection.class);
assertNotNull(connection);
assertSame("Second total connections not as expected", 1, pool.getTotalConnections());
assertSame("Second idle connections not as expected", 0, pool.getIdleConnections());
}
assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
try (Connection connection = ds.getConnection()) {
unwrap2 = connection.unwrap(Connection.class);
assertSame(unwrap, unwrap2);
assertSame("Second total connections not as expected", 1, pool.getTotalConnections());
assertSame("Second idle connections not as expected", 0, pool.getIdleConnections());
}
quietlySleep(TimeUnit.SECONDS.toMillis(2));
try (Connection connection = ds.getConnection()) {
unwrap2 = connection.unwrap(Connection.class);
assertNotSame("Expected a different connection", unwrap, unwrap2);
}
assertSame("Post total connections not as expected", 1, pool.getTotalConnections());
assertSame("Post idle connections not as expected", 1, pool.getIdleConnections());
} finally {
setConfigUnitTest(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