Fix shutdown race with connections added to the pool.

pull/192/head
Brett Wooldridge 11 years ago
parent 7e2ab6de3e
commit e6fb2a4e72

@ -250,6 +250,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
isShutdown = true;
LOGGER.info("HikariCP pool {} is shutting down.", configuration.getPoolName());
connectionBag.close();
logPoolState("Before shutdown ");
houseKeepingExecutorService.shutdownNow();
addConnectionExecutor.shutdownNow();

@ -81,6 +81,7 @@ public final class ConcurrentBag<T extends BagEntry>
private final Synchronizer synchronizer;
private final AtomicLong sequence;
private final IBagStateListener listener;
private volatile boolean closed;
/**
* Construct a ConcurrentBag with the specified listener.
@ -179,6 +180,10 @@ public final class ConcurrentBag<T extends BagEntry>
*/
public void add(final T bagEntry)
{
if (closed) {
throw new IllegalStateException("ConcurrentBag has been closed");
}
sharedList.add(bagEntry);
synchronizer.releaseShared(sequence.incrementAndGet());
}
@ -203,6 +208,14 @@ public final class ConcurrentBag<T extends BagEntry>
}
}
/**
* Close the bag to further adds.
*/
public void close()
{
closed = true;
}
/**
* This method provides a "snaphot" in time of the BagEntry
* items in the bag in the specified state. It does not "lock"

@ -167,7 +167,7 @@ public class ShutdownTest
PoolUtilities.quietlySleep(250);
}
Assert.assertSame("Thread was leaked", 0, threadCount());
Assert.assertSame("Unreleased connections after shutdown", 0, TestElf.getPool(ds).getTotalConnections());
}
@Test

@ -253,7 +253,7 @@ public class TestConnectionTimeoutRetry
Connection connection6 = ds.getConnection();
Connection connection7 = ds.getConnection();
Thread.sleep(1250);
Thread.sleep(1350);
Assert.assertSame("Totals connections not as expected", 10, TestElf.getPool(ds).getTotalConnections());
Assert.assertSame("Idle connections not as expected", 3, TestElf.getPool(ds).getIdleConnections());

@ -247,6 +247,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener
isShutdown = true;
LOGGER.info("HikariCP pool {} is shutting down.", configuration.getPoolName());
connectionBag.close();
logPoolState("Before shutdown ");
houseKeepingExecutorService.shutdownNow();
addConnectionExecutor.shutdownNow();

@ -80,6 +80,7 @@ public final class ConcurrentBag<T extends BagEntry>
private final Synchronizer synchronizer;
private final AtomicLong sequence;
private final IBagStateListener listener;
private volatile boolean closed;
/**
* Construct a ConcurrentBag with the specified listener.
@ -178,6 +179,10 @@ public final class ConcurrentBag<T extends BagEntry>
*/
public void add(final T bagEntry)
{
if (closed) {
throw new IllegalStateException("ConcurrentBag has been closed");
}
sharedList.add(bagEntry);
synchronizer.releaseShared(sequence.incrementAndGet());
}
@ -202,6 +207,14 @@ public final class ConcurrentBag<T extends BagEntry>
}
}
/**
* Close the bag to further adds.
*/
public void close()
{
closed = true;
}
/**
* This method provides a "snaphot" in time of the BagEntry
* items in the bag in the specified state. It does not "lock"

@ -167,7 +167,7 @@ public class ShutdownTest
PoolUtilities.quietlySleep(250);
}
Assert.assertSame("Thread was leaked", 0, threadCount());
Assert.assertSame("Unreleased connections after shutdown", 0, TestElf.getPool(ds).getTotalConnections());
}
@Test

@ -253,7 +253,7 @@ public class TestConnectionTimeoutRetry
Connection connection6 = ds.getConnection();
Connection connection7 = ds.getConnection();
Thread.sleep(1250);
Thread.sleep(1350);
Assert.assertSame("Totals connections not as expected", 10, TestElf.getPool(ds).getTotalConnections());
Assert.assertSame("Idle connections not as expected", 3, TestElf.getPool(ds).getIdleConnections());

Loading…
Cancel
Save