Skip to content

Commit 6008be0

Browse files
committed
Allow connection pool to grow
1 parent e0d868d commit 6008be0

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionBuffer.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120
*/
121121
final class ConnectionBuffer {
122122

123+
static final Object POP_LAST = new Object();
124+
123125
private final Node free = Node.init();
124126
private final Node freeEnd = free.next;
125127
private final Node busy = Node.init();
@@ -213,18 +215,27 @@ boolean moveToFreeList(PooledConnection c) {
213215
* <p>
214216
* Connections that are returend from this method must be either added to busyList with
215217
* addBusy or closed fully.
218+
*
219+
* @param affinityId the preferred affinity-id.
220+
* If <code>null</code> is provided, the first element in the list is
221+
* returned.
222+
* If the affinity-id is not present in the list, <code>null</code>
223+
* is returned. The caller can decide to create a new connection or
224+
* ask again with <code>POP_LAST</code>, which returns the last
225+
* (=oldest) connection if affinity is enabled.
216226
*/
217227
PooledConnection popFree(Object affinityId) {
218228
Node node;
219229
if (affinityId == null || affinityNodes == null) {
220230
node = free.next;
231+
} else if (affinityId == POP_LAST) {
232+
node = freeEnd.prev;
221233
} else {
222234
node = affinityNodes[affinityId.hashCode() % hashSize].find(affinityId);
223235
if (node == null) {
224-
// when we did not find a node with that affinity, we take the last (oldest one)
225-
// and reuse this with the new affinity. This avoids to "steal" the affinity
226-
// from the newest one.
227-
node = freeEnd.prev;
236+
// when we did not find a node with that affinity, we return null
237+
// this allows the pool to grow to its maximum size
238+
return null;
228239
}
229240
}
230241
if (node.isBoundaryNode()) {

ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnectionQueue.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,19 @@ private PooledConnection _obtainConnection(Object affinitiyId) throws Interrupte
231231
if (connection != null) {
232232
return connection;
233233
}
234+
if (affinitiyId != null) {
235+
connection = extractFromFreeList(ConnectionBuffer.POP_LAST);
236+
if (connection != null) {
237+
return connection;
238+
}
239+
}
234240
}
235241
try {
236242
// The pool is at maximum size. We are going to go into
237243
// a wait loop until connections are returned into the pool.
238244
waitCount++;
239245
waitingThreads++;
240-
return _obtainConnectionWaitLoop(affinitiyId);
246+
return _obtainConnectionWaitLoop(null);
241247
} finally {
242248
waitingThreads--;
243249
}

ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionBufferTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ public void test_Affinity() {
184184

185185
private static PooledConnection getConnection(ConnectionBuffer b, Object affinity) {
186186
PooledConnection c1 = b.popFree(affinity);
187+
if (c1 == null) {
188+
c1 = b.popFree(ConnectionBuffer.POP_LAST);
189+
}
187190
c1.setAffinityId(affinity);
188191
b.addBusy(c1);
189192
return c1;

0 commit comments

Comments
 (0)