From 5de373148cfd297e18df8bd8cb23661ecf2dff91 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Tue, 23 Dec 2014 00:54:24 +0900 Subject: [PATCH] Don't throw exceptions that might disrupt executor threads, but do log. Plus, don't trust nanoTime() implementations on all platforms not to go backwards due to NTP adjustments. --- .../com/zaxxer/hikari/util/ConcurrentBag.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/hikaricp-common/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java b/hikaricp-common/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java index 618361cf..211aee7b 100644 --- a/hikaricp-common/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java +++ b/hikaricp-common/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java @@ -129,7 +129,8 @@ public class ConcurrentBag return null; } - timeout = originTimeout - (System.nanoTime() - startScan); + final long elapsed = (System.nanoTime() - startScan); + timeout = originTimeout - Math.max(elapsed, 100L); // don't trust the nanoTime() impl. not to go backwards due to NTP adjustments } while (timeout > 1000L); // 1000ns is the minimum resolution on many systems @@ -155,7 +156,7 @@ public class ConcurrentBag synchronizer.releaseShared(sequence.incrementAndGet()); } else { - throw new IllegalStateException("Value was returned to the bag that was not borrowed: " + bagEntry); + LOGGER.warn("Attempt to remove an object from the bag that does not exist: {}", bagEntry.toString()); } } @@ -167,6 +168,7 @@ public class ConcurrentBag public void add(final T bagEntry) { if (closed) { + LOGGER.info("ConcurrentBag has been closed, ignoring add()"); throw new IllegalStateException("ConcurrentBag has been closed, ignoring add()"); } @@ -186,11 +188,13 @@ public class ConcurrentBag public boolean remove(final T bagEntry) { if (!bagEntry.state().compareAndSet(STATE_IN_USE, STATE_REMOVED) && !bagEntry.state().compareAndSet(STATE_RESERVED, STATE_REMOVED) && !closed) { - throw new IllegalStateException("Attempt to remove an object from the bag that was not borrowed or reserved"); + LOGGER.warn("Attempt to remove an object from the bag that was not borrowed or reserved: {}", bagEntry.toString()); + return false; } + final boolean removed = sharedList.remove(bagEntry); if (!removed && !closed) { - throw new IllegalStateException("Attempt to remove an object from the bag that does not exist"); + LOGGER.warn("Attempt to remove an object from the bag that does not exist: {}", bagEntry.toString()); } return removed; } @@ -251,11 +255,12 @@ public class ConcurrentBag public void unreserve(final T bagEntry) { final long checkInSeq = sequence.incrementAndGet(); - if (!bagEntry.state().compareAndSet(STATE_RESERVED, STATE_NOT_IN_USE)) { - throw new IllegalStateException("Attempt to relinquish an object to the bag that was not reserved"); + if (bagEntry.state().compareAndSet(STATE_RESERVED, STATE_NOT_IN_USE)) { + synchronizer.releaseShared(checkInSeq); + } + else { + LOGGER.warn("Attempt to relinquish an object to the bag that was not reserved: {}", bagEntry.toString()); } - - synchronizer.releaseShared(checkInSeq); } /**