Skip to content

Commit

Permalink
Cleanup. Former code would remove N elements if PoolEntries were unre…
Browse files Browse the repository at this point in the history
…servable. Additionally, I'd like to see stats every 30 seconds, as long as idleTimeout is enabled, regardless of idle removable (because connection attrition occurs by other means as well.
  • Loading branch information
brettwooldridge committed Sep 25, 2015
1 parent d954f46 commit e125d50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/main/java/com/zaxxer/hikari/pool/HikariPool.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLTransientConnectionException; import java.sql.SQLTransientConnectionException;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
Expand Down Expand Up @@ -608,29 +608,26 @@ public void run()


previous = now; previous = now;


logPoolState("Before cleanup\t");

if (idleTimeout > 0L) { if (idleTimeout > 0L) {
final List<PoolEntry> notInUseList = connectionBag.values(STATE_NOT_IN_USE); final List<PoolEntry> notInUseList = connectionBag.values(STATE_NOT_IN_USE);
final int removable = notInUseList.size() - config.getMinimumIdle(); int removable = notInUseList.size() - config.getMinimumIdle();
if (removable > 0) { if (removable > 0) {
logPoolState("Before cleanup\t"); // Sort pool entries on lastAccessed
//sort pool entries on lastAccessed Collections.sort(notInUseList, PoolEntry.LASTACCESS_COMPARABLE);
Collections.sort(notInUseList, new Comparator<PoolEntry>() { // Iterate the first N removable elements
@Override for (final Iterator<PoolEntry> iter = notInUseList.iterator(); removable > 0 && iter.hasNext(); ) {

This comment has been minimized.

Copy link
@nitincchauhan

nitincchauhan Sep 25, 2015

Contributor

It is safe to use alternative to iterator here.

public int compare(final PoolEntry entryOne, final PoolEntry entryTwo) { final PoolEntry poolEntry = iter.next();
return Long.compare(entryOne.lastAccessed, entryTwo.lastAccessed); if (clockSource.elapsedMillis(poolEntry.lastAccessed, now) > idleTimeout && connectionBag.reserve(poolEntry)) {
} closeConnection(poolEntry, "(connection passed idleTimeout)");
}); removable--;
for (int i = 0; i < removable; i++) {
final PoolEntry poolEntry = notInUseList.get(i);
if (clockSource.elapsedMillis(poolEntry.lastAccessed, now) > idleTimeout) {
if (connectionBag.reserve(poolEntry)) {
closeConnection(poolEntry, "(connection passed idleTimeout)");
}
} }
} }
logPoolState("After cleanup\t");
} }
} }

logPoolState("After cleanup\t");


fillPool(); // Try to maintain minimum connections fillPool(); // Try to maintain minimum connections
} }
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/zaxxer/hikari/pool/PoolEntry.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Comparator;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;


Expand All @@ -37,6 +38,7 @@ final class PoolEntry implements IConcurrentBagEntry
{ {
private static final Logger LOGGER = LoggerFactory.getLogger(PoolEntry.class); private static final Logger LOGGER = LoggerFactory.getLogger(PoolEntry.class);


static final Comparator<PoolEntry> LASTACCESS_COMPARABLE;
Connection connection; Connection connection;
long lastAccessed; long lastAccessed;
long lastBorrowed; long lastBorrowed;
Expand All @@ -48,6 +50,16 @@ final class PoolEntry implements IConcurrentBagEntry


private volatile ScheduledFuture<?> endOfLife; private volatile ScheduledFuture<?> endOfLife;


static
{
LASTACCESS_COMPARABLE = new Comparator<PoolEntry>() {
@Override
public int compare(final PoolEntry entryOne, final PoolEntry entryTwo) {
return Long.compare(entryOne.lastAccessed, entryTwo.lastAccessed);
}
};
}

PoolEntry(final Connection connection, final PoolBase pool) PoolEntry(final Connection connection, final PoolBase pool)
{ {
this.connection = connection; this.connection = connection;
Expand Down

0 comments on commit e125d50

Please sign in to comment.