use of java.sql.Connection in project HikariCP by brettwooldridge.
the class SaturatedPoolTest830 method saturatedPoolTest.
@Test
public void saturatedPoolTest() throws Exception {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(5);
config.setMaximumPoolSize(MAX_POOL_SIZE);
config.setInitializationFailTimeout(Long.MAX_VALUE);
config.setConnectionTimeout(1000);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
StubConnection.slowCreate = true;
StubStatement.setSimulatedQueryTime(1000);
setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "5000");
final long start = currentTime();
try (final HikariDataSource ds = new HikariDataSource(config)) {
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(50, /*core*/
50, /*max*/
2, /*keepalive*/
SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
threadPool.allowCoreThreadTimeOut(true);
AtomicInteger windowIndex = new AtomicInteger();
boolean[] failureWindow = new boolean[100];
Arrays.fill(failureWindow, true);
// Initial saturation
for (int i = 0; i < 50; i++) {
threadPool.execute(() -> {
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()) {
stmt.execute("SELECT bogus FROM imaginary");
} catch (SQLException e) {
LOGGER.info(e.getMessage());
}
});
}
long sleep = 80;
outer: while (true) {
quietlySleep(sleep);
if (elapsedMillis(start) > SECONDS.toMillis(12) && sleep < 100) {
sleep = 100;
LOGGER.warn("Switching to 100ms sleep");
} else if (elapsedMillis(start) > SECONDS.toMillis(6) && sleep < 90) {
sleep = 90;
LOGGER.warn("Switching to 90ms sleep");
}
threadPool.execute(() -> {
int ndx = windowIndex.incrementAndGet() % failureWindow.length;
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()) {
stmt.execute("SELECT bogus FROM imaginary");
failureWindow[ndx] = false;
} catch (SQLException e) {
LOGGER.info(e.getMessage());
failureWindow[ndx] = true;
}
});
for (int i = 0; i < failureWindow.length; i++) {
if (failureWindow[i]) {
if (elapsedMillis(start) % (SECONDS.toMillis(1) - sleep) < sleep) {
LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
}
continue outer;
}
}
LOGGER.info("Timeouts have subsided.");
LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
break;
}
LOGGER.info("Waiting for completion of {} active tasks.", threadPool.getActiveCount());
while (getPool(ds).getActiveConnections() > 0) {
quietlySleep(50);
}
assertEquals("Rate not in balance at 10req/s", SECONDS.toMillis(1) / sleep, 10L);
} finally {
StubStatement.setSimulatedQueryTime(0);
StubConnection.slowCreate = false;
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
setSlf4jLogLevel(HikariPool.class, Level.INFO);
}
}
use of java.sql.Connection in project HikariCP by brettwooldridge.
the class StatementTest method testStatementClose.
@Test
public void testStatementClose() throws SQLException {
ds.getConnection().close();
HikariPool pool = getPool(ds);
assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 1);
try (Connection connection = ds.getConnection()) {
assertNotNull(connection);
assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 0);
Statement statement = connection.createStatement();
assertNotNull(statement);
connection.close();
assertTrue(statement.isClosed());
}
}
use of java.sql.Connection in project HikariCP by brettwooldridge.
the class UnwrapTest method testUnwrapConnection.
@Test
public void testUnwrapConnection() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
ds.getConnection().close();
assertSame("Idle connections not as expected", 1, getPool(ds).getIdleConnections());
Connection connection = ds.getConnection();
assertNotNull(connection);
StubConnection unwrapped = connection.unwrap(StubConnection.class);
assertTrue("unwrapped connection is not instance of StubConnection: " + unwrapped, (unwrapped != null && unwrapped instanceof StubConnection));
}
}
use of java.sql.Connection in project HikariCP by brettwooldridge.
the class TestProxies method testOtherExceptions.
@Test
public void testOtherExceptions() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
try (Connection conn = ds.getConnection()) {
StubConnection stubConnection = conn.unwrap(StubConnection.class);
stubConnection.throwException = true;
try {
conn.setTransactionIsolation(Connection.TRANSACTION_NONE);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.isReadOnly();
fail();
} catch (SQLException e) {
// pass
}
try {
conn.setReadOnly(false);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.setCatalog("");
fail();
} catch (SQLException e) {
// pass
}
try {
conn.setAutoCommit(false);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.clearWarnings();
fail();
} catch (SQLException e) {
// pass
}
try {
conn.isValid(0);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.isWrapperFor(getClass());
fail();
} catch (SQLException e) {
// pass
}
try {
conn.unwrap(getClass());
fail();
} catch (SQLException e) {
// pass
}
try {
conn.close();
fail();
} catch (SQLException e) {
// pass
}
try {
assertFalse(conn.isValid(0));
} catch (SQLException e) {
fail();
}
}
}
}
use of java.sql.Connection in project HikariCP by brettwooldridge.
the class TestProxies method testStatementExceptions.
@Test
public void testStatementExceptions() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(1));
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
Connection conn = ds.getConnection();
StubConnection stubConnection = conn.unwrap(StubConnection.class);
stubConnection.throwException = true;
try {
conn.createStatement();
fail();
} catch (SQLException e) {
// pass
}
try {
conn.createStatement(0, 0);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.createStatement(0, 0, 0);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareCall("");
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareCall("", 0, 0);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareCall("", 0, 0, 0);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareStatement("");
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareStatement("", 0);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareStatement("", new int[0]);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareStatement("", new String[0]);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareStatement("", 0, 0);
fail();
} catch (SQLException e) {
// pass
}
try {
conn.prepareStatement("", 0, 0, 0);
fail();
} catch (SQLException e) {
// pass
}
}
}
Aggregations