use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnectionTimeoutRetry method testConnectionRetries5.
@Test
public void testConnectionRetries5() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(2);
config.setConnectionTimeout(1000);
config.setValidationTimeout(1000);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
final Connection connection1 = ds.getConnection();
long start = currentTime();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
scheduler.schedule(new Runnable() {
@Override
public void run() {
try {
connection1.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}, 250, MILLISECONDS);
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused"));
try {
try (Connection connection2 = ds.getConnection()) {
// close immediately
}
long elapsed = elapsedMillis(start);
assertTrue("Waited too long to get a connection.", (elapsed >= 250) && (elapsed < config.getConnectionTimeout()));
} catch (SQLException e) {
fail("Should not have timed out.");
} finally {
scheduler.shutdownNow();
}
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testEviction.
@Test
public void testEviction() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(5);
config.setConnectionTimeout(2500);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
Connection connection = ds.getConnection();
HikariPool pool = getPool(ds);
assertEquals(1, pool.getTotalConnections());
ds.evictConnection(connection);
assertEquals(0, pool.getTotalConnections());
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testOldDriver.
@Test
public void testOldDriver() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTimeout(2500);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
StubConnection.oldDriver = true;
StubStatement.oldDriver = true;
try (HikariDataSource ds = new HikariDataSource(config)) {
quietlySleep(500);
try (Connection connection = ds.getConnection()) {
// close
}
quietlySleep(500);
try (Connection connection = ds.getConnection()) {
// close
}
} finally {
StubConnection.oldDriver = false;
StubStatement.oldDriver = false;
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testMaxLifetime2.
@Test
public void testMaxLifetime2() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setConnectionTimeout(2500);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100");
setConfigUnitTest(true);
try (HikariDataSource ds = new HikariDataSource(config)) {
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(800);
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.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testInvalidConnectionTestQuery.
@Test
public void testInvalidConnectionTestQuery() {
class BadConnection extends StubConnection {
/** {@inheritDoc} */
@Override
public Statement createStatement() throws SQLException {
throw new SQLException("Simulated exception in createStatement()");
}
}
StubDataSource stubDataSource = new StubDataSource() {
/** {@inheritDoc} */
@Override
public Connection getConnection() throws SQLException {
return new BadConnection();
}
};
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(2);
config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(3));
config.setConnectionTestQuery("VALUES 1");
config.setInitializationFailTimeout(TimeUnit.SECONDS.toMillis(2));
config.setDataSource(stubDataSource);
try (HikariDataSource ds = new HikariDataSource(config)) {
try (Connection c = ds.getConnection()) {
fail("getConnection() should have failed");
} catch (SQLException e) {
assertSame("Simulated exception in createStatement()", e.getNextException().getMessage());
}
} catch (PoolInitializationException e) {
assertSame("Simulated exception in createStatement()", e.getCause().getMessage());
}
config.setInitializationFailTimeout(0);
try (HikariDataSource ds = new HikariDataSource(config)) {
fail("Initialization should have failed");
} catch (PoolInitializationException e) {
// passed
}
}
Aggregations