use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testDoubleClose.
@Test
public void testDoubleClose() 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");
try (HikariDataSource ds = new HikariDataSource(config);
Connection connection = ds.getConnection()) {
connection.close();
// should no-op
connection.abort(null);
assertTrue("Connection should have closed", connection.isClosed());
assertFalse("Connection should have closed", connection.isValid(5));
assertTrue("Expected to contain ClosedConnection, but was " + connection, connection.toString().contains("ClosedConnection"));
}
}
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 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() {
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 ignored = 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 ignored = new HikariDataSource(config)) {
fail("Initialization should have failed");
} catch (PoolInitializationException e) {
// passed
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testMaximumPoolLimit.
@Test
public void testMaximumPoolLimit() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(4);
config.setConnectionTimeout(20000);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
final AtomicReference<Exception> ref = new AtomicReference<>();
// reset counter
StubConnection.count.set(0);
try (final HikariDataSource ds = new HikariDataSource(config)) {
final HikariPool pool = getPool(ds);
Thread[] threads = new Thread[20];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(() -> {
try {
pool.logPoolState("Before acquire ");
try (Connection ignored = ds.getConnection()) {
pool.logPoolState("After acquire ");
quietlySleep(500);
}
} catch (Exception e) {
ref.set(e);
}
});
}
for (Thread thread : threads) {
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
pool.logPoolState("before check ");
assertNull((ref.get() != null ? ref.get().toString() : ""), ref.get());
assertSame("StubConnection count not as expected", 4, StubConnection.count.get());
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testMinimumIdleZero.
@Test
@SuppressWarnings("EmptyTryBlock")
public void testMinimumIdleZero() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(5);
config.setConnectionTimeout(1000L);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config);
Connection ignored = ds.getConnection()) {
// passed
} catch (SQLTransientConnectionException sqle) {
fail("Failed to obtain connection");
}
}
Aggregations