use of com.zaxxer.hikari.HikariConfig 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;
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testCreate.
@Test
public void testCreate() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setConnectionInitSql("SELECT 1");
config.setReadOnly(true);
config.setConnectionTimeout(2500);
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(30));
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
ds.setLoginTimeout(10);
assertSame(10, ds.getLoginTimeout());
HikariPool pool = getPool(ds);
ds.getConnection().close();
assertSame("Total connections not as expected", 1, pool.getTotalConnections());
assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
try (Connection connection = ds.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM device WHERE device_id=?")) {
assertNotNull(connection);
assertNotNull(statement);
assertSame("Total connections not as expected", 1, pool.getTotalConnections());
assertSame("Idle connections not as expected", 0, pool.getIdleConnections());
statement.setInt(1, 0);
try (ResultSet resultSet = statement.executeQuery()) {
assertNotNull(resultSet);
assertFalse(resultSet.next());
}
}
assertSame("Total connections not as expected", 1, pool.getTotalConnections());
assertSame("Idle connections not as expected", 1, pool.getIdleConnections());
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testInitializationFailure2.
@Test
public void testInitializationFailure2() throws SQLException {
StubDataSource stubDataSource = new StubDataSource();
stubDataSource.setThrowException(new SQLException("Connection refused"));
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setConnectionTestQuery("VALUES 1");
config.setDataSource(stubDataSource);
try (HikariDataSource ds = new HikariDataSource(config);
Connection ignored = ds.getConnection()) {
fail("Initialization should have failed");
} catch (PoolInitializationException e) {
// passed
}
}
use of com.zaxxer.hikari.HikariConfig 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");
getUnsealedConfig(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.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testSuspendResumeWithThrow.
@Test
public void testSuspendResumeWithThrow() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(3);
config.setMaximumPoolSize(3);
config.setConnectionTimeout(2500);
config.setAllowPoolSuspension(true);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
System.setProperty("com.zaxxer.hikari.throwIfSuspended", "true");
try (final HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = getPool(ds);
while (pool.getTotalConnections() < 3) {
quietlySleep(50);
}
AtomicReference<Exception> exception = new AtomicReference<>();
Thread t = new Thread(() -> {
try {
ds.getConnection();
ds.getConnection();
} catch (Exception e) {
exception.set(e);
}
});
try (Connection ignored = ds.getConnection()) {
assertEquals(2, pool.getIdleConnections());
pool.suspendPool();
t.start();
quietlySleep(500);
assertEquals(SQLTransientException.class, exception.get().getClass());
assertEquals(2, pool.getIdleConnections());
}
assertEquals(3, pool.getIdleConnections());
pool.resumePool();
try (Connection ignored = ds.getConnection()) {
assertEquals(2, pool.getIdleConnections());
}
} finally {
System.getProperties().remove("com.zaxxer.hikari.throwIfSuspended");
}
}
Aggregations