Fixes #1060 (Really) support experimental throw-if-suspended functionality.

pull/1074/head
Brett Wooldridge 7 years ago
parent bef929cc97
commit 64d91e8ea3

@ -64,10 +64,11 @@ public class SuspendResumeLock
if (acquisitionSemaphore.tryAcquire()) { if (acquisitionSemaphore.tryAcquire()) {
return; return;
} }
else if (Boolean.getBoolean("com.zaxxer.hikari.throwIfSuspended")) {
if (Boolean.getBoolean("com.zaxxer.hikari.throwIfSuspended")) {
throw new SQLTransientException("The pool is currently suspended and configured to throw exceptions upon acquisition"); throw new SQLTransientException("The pool is currently suspended and configured to throw exceptions upon acquisition");
} }
acquisitionSemaphore.acquireUninterruptibly();
} }
public void release() public void release()

@ -39,6 +39,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLTransientConnectionException; import java.sql.SQLTransientConnectionException;
import java.sql.SQLTransientException;
import java.sql.Statement; import java.sql.Statement;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -459,6 +460,58 @@ public class TestConnections
} }
} }
@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");
}
}
@Test @Test
public void testInitializationFailure1() public void testInitializationFailure1()
{ {

Loading…
Cancel
Save