use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class PostgresTest method testCase3.
//@Test
public void testCase3() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(3);
config.setMaximumPoolSize(10);
config.setConnectionTimeout(1000);
config.setIdleTimeout(TimeUnit.SECONDS.toMillis(60));
config.setJdbcUrl("jdbc:pgsql://localhost:5432/test");
config.setUsername("brettw");
try (final HikariDataSource ds = new HikariDataSource(config)) {
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
try (Connection conn = ds.getConnection()) {
System.err.println("ERROR: should not have acquired connection.");
} catch (SQLException e) {
// expected
}
}
}.start();
}
quietlySleep(5000L);
System.err.println("Now, bring the DB online. Checking pool in 15 seconds.");
quietlySleep(15000L);
getPool(ds).logPoolState();
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class PostgresTest method testCase2.
//@Test
public void testCase2() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(3);
config.setMaximumPoolSize(10);
config.setConnectionTimeout(1000);
config.setIdleTimeout(TimeUnit.SECONDS.toMillis(60));
config.setJdbcUrl("jdbc:pgsql://localhost:5432/test");
config.setUsername("brettw");
try (HikariDataSource ds = new HikariDataSource(config)) {
try (Connection conn = ds.getConnection()) {
System.err.println("\nGot a connection, and released it. Now, enable the firewall.");
}
getPool(ds).logPoolState();
quietlySleep(5000L);
System.err.println("\nNow attempting another getConnection(), expecting a timeout...");
long start = currentTime();
try (Connection conn = ds.getConnection()) {
System.err.println("\nOpps, got a connection. Did you enable the firewall? " + conn);
fail("Opps, got a connection. Did you enable the firewall?");
} catch (SQLException e) {
assertTrue("Timeout less than expected " + elapsedMillis(start) + "ms", elapsedMillis(start) > 5000);
}
System.err.println("\nOk, so far so good. Now, disable the firewall again. Attempting connection in 5 seconds...");
quietlySleep(5000L);
getPool(ds).logPoolState();
try (Connection conn = ds.getConnection()) {
System.err.println("\nGot a connection, and released it.");
}
}
System.err.println("\nPassed.");
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class ShutdownTest method testThreadedShutdown.
@Test
public void testThreadedShutdown() throws Exception {
final HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(5);
config.setConnectionTimeout(1000);
config.setValidationTimeout(1000);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
for (int i = 0; i < 4; i++) {
try (final HikariDataSource ds = new HikariDataSource(config)) {
Thread t = new Thread() {
@Override
public void run() {
try (Connection connection = ds.getConnection()) {
for (int i = 0; i < 10; i++) {
Connection connection2 = null;
try {
connection2 = ds.getConnection();
PreparedStatement stmt = connection2.prepareStatement("SOMETHING");
UtilityElf.quietlySleep(20);
stmt.getMaxFieldSize();
} catch (SQLException e) {
try {
if (connection2 != null) {
connection2.close();
}
} catch (SQLException e2) {
if (e2.getMessage().contains("shutdown") || e2.getMessage().contains("evicted")) {
break;
}
}
}
}
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
ds.close();
}
}
};
t.start();
Thread t2 = new Thread() {
@Override
public void run() {
UtilityElf.quietlySleep(100);
try {
ds.close();
} catch (IllegalStateException e) {
Assert.fail(e.getMessage());
}
}
};
t2.start();
t.join();
t2.join();
ds.close();
}
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class ShutdownTest method testShutdown5.
@Test
public void testShutdown5() throws SQLException {
Assert.assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(5);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = getPool(ds);
Connection[] connections = new Connection[5];
for (int i = 0; i < 5; i++) {
connections[i] = ds.getConnection();
}
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
ds.close();
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
}
}
use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.
the class ShutdownTest method testShutdownDuringInit.
@Test
public void testShutdownDuringInit() throws Exception {
final HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(5);
config.setConnectionTimeout(1000);
config.setValidationTimeout(1000);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
StubConnection.slowCreate = true;
UtilityElf.quietlySleep(3000L);
}
}
Aggregations