Skip to content

Commit ba827dd

Browse files
committed
test erweitert + fix
1 parent 1d957d8 commit ba827dd

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourcePoolListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ default void onBeforeReturnConnection(Connection connection) {}
4444
*/
4545
default void onAfterReturnConnection() {}
4646

47+
default void onBeforeCloseConnection(Connection connection) {}
48+
49+
default void onAfterCloseConnection() {}
4750
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ void pstmtCacheMetrics(PstmtCache pstmtCache) {
157157
pscRem.add(pstmtCache.removeCount());
158158
}
159159

160+
161+
160162
final class HeartBeatRunnable extends TimerTask {
161163
@Override
162164
public void run() {
@@ -454,8 +456,7 @@ private Connection createConnection() throws SQLException {
454456
if (poolListener != null) {
455457
poolListener.onAfterCreateConnection(connection);
456458
}
457-
458-
return initConnection(source.getConnection());
459+
return connection;
459460
}
460461

461462
@Override
@@ -572,6 +573,9 @@ private void returnTheConnection(PooledConnection pooledConnection, boolean forc
572573
poolListener.onBeforeReturnConnection(pooledConnection);
573574
}
574575
queue.returnPooledConnection(pooledConnection, forceClose);
576+
if (poolListener != null && !forceClose) {
577+
poolListener.onAfterReturnConnection();
578+
}
575579
if (forceClose) {
576580
// Got a bad connection so check the pool
577581
testConnection();
@@ -584,6 +588,17 @@ void returnConnectionReset(PooledConnection pooledConnection) {
584588
reset();
585589
}
586590

591+
void onBeforeCloseConnection(PooledConnection pooledConnection) {
592+
if (poolListener != null) {
593+
poolListener.onBeforeCloseConnection(pooledConnection);
594+
}
595+
}
596+
597+
void onAfterCloseConnection() {
598+
if (poolListener != null) {
599+
poolListener.onAfterCloseConnection();
600+
}
601+
}
587602
/**
588603
* Grow the pool by creating a new connection. The connection can either be
589604
* added to the available list, or returned.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ void closeConnectionFully(boolean logErrors) {
242242
}
243243
if (pool != null) {
244244
pool.pstmtCacheMetrics(pstmtCache);
245+
pool.onBeforeCloseConnection(this);
245246
}
246247
try {
247248
if (connection.isClosed()) {
@@ -281,6 +282,7 @@ void closeConnectionFully(boolean logErrors) {
281282
try {
282283
connection.close();
283284
pool.dec();
285+
pool.onAfterCloseConnection();
284286
} catch (SQLException ex) {
285287
if (logErrors || Log.isLoggable(System.Logger.Level.DEBUG)) {
286288
Log.error("Error when fully closing connection [" + fullDescription() + "]", ex);

ebean-datasource/src/test/java/io/ebean/datasource/test/MultipoolTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
import java.sql.Connection;
1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.Random;
1516
import java.util.concurrent.ExecutorService;
1617
import java.util.concurrent.Executors;
1718
import java.util.concurrent.Future;
19+
import java.util.concurrent.Semaphore;
20+
import java.util.concurrent.TimeUnit;
1821

1922
@Disabled("run manually")
2023
class MultipoolTest {
@@ -43,6 +46,26 @@ static void after() {
4346
}
4447

4548
static class PoolManager implements DataSourcePoolListener {
49+
List<DataSourcePool> pools = new ArrayList<>();
50+
Semaphore semaphore = new Semaphore(120);
51+
Random random = new Random();
52+
53+
@Override
54+
public void onBeforeCreateConnection() {
55+
try {
56+
while (!semaphore.tryAcquire(50, TimeUnit.MILLISECONDS)) {
57+
System.out.println("trim required");
58+
pools.get(random.nextInt(pools.size())).forceTrim(25);
59+
}
60+
} catch (InterruptedException e) {
61+
throw new RuntimeException(e);
62+
}
63+
}
64+
65+
public void onAfterCloseConnection() {
66+
semaphore.release();
67+
}
68+
4669

4770
}
4871

@@ -56,6 +79,7 @@ void testFalseFriendRollback() throws Exception {
5679

5780
try {
5881
consumeConnections(pool1, 100);
82+
//pool1.forceTrim(70);
5983
consumeConnections(pool2, 100);
6084
} finally {
6185
pool1.shutdown();
@@ -84,7 +108,7 @@ void consumeConnections(DataSourcePool pool, int connectionsCount) throws Except
84108
}
85109

86110
private static DataSourcePool getPool() {
87-
return DataSourceBuilder.create()
111+
DataSourcePool pool = DataSourceBuilder.create()
88112
.url(container.jdbcUrl())
89113
.username("unit")
90114
.password("unit")
@@ -93,5 +117,7 @@ private static DataSourcePool getPool() {
93117
.maxConnections(100)
94118
.listener(poolManager)
95119
.build();
120+
poolManager.pools.add(pool);
121+
return pool;
96122
}
97123
}

0 commit comments

Comments
 (0)