use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class TestConnections method testMinimumIdleZero.
@Test
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 connection = ds.getConnection()) {
// passed
} catch (SQLTransientConnectionException sqle) {
fail("Failed to obtain connection");
}
}
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 testSuspendResume.
@Test
public void testSuspendResume() 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");
try (final HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = getPool(ds);
while (pool.getTotalConnections() < 3) {
quietlySleep(50);
}
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
ds.getConnection();
ds.getConnection();
} catch (Exception e) {
fail();
}
}
});
try (Connection c3 = ds.getConnection()) {
assertEquals(2, pool.getIdleConnections());
pool.suspendPool();
t.start();
quietlySleep(500);
assertEquals(2, pool.getIdleConnections());
}
assertEquals(3, pool.getIdleConnections());
pool.resumePool();
quietlySleep(500);
assertEquals(1, pool.getIdleConnections());
}
}
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(new Runnable() {
@Override
public void run() {
try {
pool.logPoolState("Before acquire ");
try (Connection connection = ds.getConnection()) {
pool.logPoolState("After acquire ");
quietlySleep(500);
}
} catch (Exception e) {
ref.set(e);
}
}
});
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].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 TestElf method newHikariConfig.
public static HikariConfig newHikariConfig() {
final StackTraceElement callerStackTrace = Thread.currentThread().getStackTrace()[2];
String poolName = callerStackTrace.getMethodName();
if ("setup".equals(poolName)) {
poolName = callerStackTrace.getClassName();
}
final HikariConfig config = new HikariConfig();
config.setPoolName(poolName);
return config;
}
Aggregations