Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ void closeAll(boolean logErrors) {
/**
* Trim any inactive connections that have not been used since usedSince.
*/
int trim(int minSize, long usedSince, long createdSince) {
int trimCount = 0;
List<PooledConnection> trim(int minSize, long usedSince, long createdSince) {
var trimmed = new ArrayList<PooledConnection>();
ListIterator<PooledConnection> iterator = freeBuffer.listIterator(minSize);
while (iterator.hasNext()) {
PooledConnection pooledConnection = iterator.next();
if (pooledConnection.shouldTrim(usedSince, createdSince)) {
iterator.remove();
pooledConnection.closeConnectionFully(true);
trimCount++;
trimmed.add(pooledConnection);
}
}
return trimCount;
return trimmed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.ebean.datasource.pool.ConnectionPool.Status;

import java.sql.SQLException;
import java.util.List;
import java.util.function.LongSupplier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
Expand Down Expand Up @@ -458,9 +459,10 @@ void trim(long maxInactiveMillis, long maxAgeMillis) {
int firstConnectionId = -1;
int add;
long generation = 0;
List<PooledConnection> trimmedConnections;
lock.lock();
try {
trimInactiveConnections(maxInactiveMillis, maxAgeMillis);
trimmedConnections = trimInactiveConnections(maxInactiveMillis, maxAgeMillis);
int freeDeficit = minSize - freeList.size();
int capacity = maxSize - totalConnections() - creatingConnections;
add = Math.min(freeDeficit, capacity);
Expand All @@ -473,6 +475,9 @@ void trim(long maxInactiveMillis, long maxAgeMillis) {
} finally {
lock.unlock();
}
for (var connection : trimmedConnections) {
connection.closeConnectionFully(true);
}
if (add > 0) {
createReservedConnections(firstConnectionId, add, generation);
}
Expand Down Expand Up @@ -515,22 +520,24 @@ private void createReservedConnections(int firstConnectionId, int numberToAdd, l
/**
* Trim connections that have been not used for some time.
*/
private void trimInactiveConnections(long maxInactiveMillis, long maxAgeMillis) {
private List<PooledConnection> trimInactiveConnections(long maxInactiveMillis, long maxAgeMillis) {
final long createdSince = (maxAgeMillis == 0) ? 0 : System.currentTimeMillis() - maxAgeMillis;
final int trimmedCount;
final List<PooledConnection> trimmedConnections;
if (freeList.size() > minSize) {
// trim on maxInactive and maxAge
long usedSince = System.currentTimeMillis() - maxInactiveMillis;
trimmedCount = freeList.trim(minSize, usedSince, createdSince);
trimmedConnections = freeList.trim(minSize, usedSince, createdSince);
} else if (createdSince > 0) {
// trim only on maxAge
trimmedCount = freeList.trim(0, createdSince, createdSince);
trimmedConnections = freeList.trim(0, createdSince, createdSince);
} else {
trimmedCount = 0;
trimmedConnections = List.of();
}
if (trimmedCount > 0 && Log.isLoggable(DEBUG)) {
Log.debug("DataSource [{0}] trimmed [{1}] inactive connections. free[{2}] busy[{3}]", name, trimmedCount, freeList.size(), busyList.size());
if (!trimmedConnections.isEmpty() && Log.isLoggable(DEBUG)) {
Log.debug("DataSource [{0}] trim [{1}] inactive connections. free[{2}] busy[{3}]",
name, trimmedConnections.size(), freeList.size(), busyList.size());
}
return trimmedConnections;
}

/**
Expand Down
Loading