Skip to content

Commit 1f9f68f

Browse files
committed
pool as parameter
1 parent 0965902 commit 1f9f68f

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,50 @@ public interface DataSourcePoolListener {
1717
/**
1818
* Called before a connection has been created
1919
*/
20-
default void onBeforeCreateConnection() {}
20+
default void onBeforeCreateConnection(DataSourcePool pool) {}
2121

2222
/**
2323
* Called after a connection has been created
2424
*/
25-
default void onAfterCreateConnection(Connection connection) {}
25+
default void onAfterCreateConnection(DataSourcePool pool, Connection connection) {}
2626

2727
/**
2828
* Called before a connection has been retrieved from the connection pool
2929
*/
30-
default void onBeforeBorrowConnection() {}
30+
default void onBeforeBorrowConnection(DataSourcePool pool) {}
3131

3232
/**
3333
* Called after a connection has been retrieved from the connection pool
3434
*/
35+
default void onAfterBorrowConnection(DataSourcePool pool, Connection connection) {onAfterBorrowConnection(connection);}
36+
37+
/**
38+
* Called after a connection has been retrieved from the connection pool.
39+
*
40+
* Migrate to <code>onAfterBorrowConnection(DataSourcePool pool, Connection connection)</code>
41+
*/
42+
@Deprecated
3543
default void onAfterBorrowConnection(Connection connection) {}
3644

3745
/**
3846
* Called before a connection will be put back to the connection pool
3947
*/
48+
default void onBeforeReturnConnection(DataSourcePool pool, Connection connection) {onBeforeReturnConnection(connection);}
49+
50+
/**
51+
* Called before a connection will be put back to the connection pool.
52+
*
53+
* Migrate to <code>onBeforeReturnConnection(DataSourcePool pool, Connection connection)</code>
54+
*/
55+
@Deprecated
4056
default void onBeforeReturnConnection(Connection connection) {}
4157

4258
/**
4359
* Called after a connection will be put back to the connection pool
4460
*/
45-
default void onAfterReturnConnection() {}
61+
default void onAfterReturnConnection(DataSourcePool pool) {}
4662

47-
default void onBeforeCloseConnection(Connection connection) {}
63+
default void onBeforeCloseConnection(DataSourcePool pool, Connection connection) {}
4864

49-
default void onAfterCloseConnection() {}
65+
default void onAfterCloseConnection(DataSourcePool pool) {}
5066
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ private Connection initConnection(Connection conn) throws SQLException {
450450
private Connection createConnection() throws SQLException {
451451

452452
if (poolListener != null) {
453-
poolListener.onBeforeCreateConnection();
453+
poolListener.onBeforeCreateConnection(this);
454454
}
455455
Connection connection = initConnection(source.getConnection());
456456
if (poolListener != null) {
457-
poolListener.onAfterCreateConnection(connection);
457+
poolListener.onAfterCreateConnection(this, connection);
458458
}
459459
return connection;
460460
}
@@ -574,11 +574,11 @@ void removeClosedConnection(PooledConnection pooledConnection) {
574574
*/
575575
private void returnTheConnection(PooledConnection pooledConnection, boolean forceClose) {
576576
if (poolListener != null && !forceClose) {
577-
poolListener.onBeforeReturnConnection(pooledConnection);
577+
poolListener.onBeforeReturnConnection(this, pooledConnection);
578578
}
579579
queue.returnPooledConnection(pooledConnection, forceClose);
580580
if (poolListener != null && !forceClose) {
581-
poolListener.onAfterReturnConnection();
581+
poolListener.onAfterReturnConnection(this);
582582
}
583583
}
584584

@@ -590,13 +590,13 @@ void returnConnectionReset(PooledConnection pooledConnection) {
590590

591591
void onBeforeCloseConnection(PooledConnection pooledConnection) {
592592
if (poolListener != null) {
593-
poolListener.onBeforeCloseConnection(pooledConnection);
593+
poolListener.onBeforeCloseConnection(this, pooledConnection);
594594
}
595595
}
596596

597597
void onAfterCloseConnection() {
598598
if (poolListener != null) {
599-
poolListener.onAfterCloseConnection();
599+
poolListener.onAfterCloseConnection(this);
600600
}
601601
}
602602
/**
@@ -638,11 +638,11 @@ private void reset() {
638638
@Override
639639
public Connection getConnection(String username, String password) throws SQLException {
640640
if (poolListener != null) {
641-
poolListener.onBeforeCreateConnection();
641+
poolListener.onBeforeCreateConnection(this);
642642
}
643643
Connection connection = initConnection(source.getConnection(username, password));
644644
if (poolListener != null) {
645-
poolListener.onAfterCreateConnection(connection);
645+
poolListener.onAfterCreateConnection(this, connection);
646646
}
647647
return connection;
648648
}
@@ -663,14 +663,14 @@ public Connection getConnection() throws SQLException {
663663
*/
664664
private PooledConnection getPooledConnection() throws SQLException {
665665
if (poolListener != null) {
666-
poolListener.onBeforeBorrowConnection();
666+
poolListener.onBeforeBorrowConnection(this);
667667
}
668668
PooledConnection c = queue.obtainConnection();
669669
if (captureStackTrace) {
670670
c.setStackTrace(Thread.currentThread().getStackTrace());
671671
}
672672
if (poolListener != null) {
673-
poolListener.onAfterBorrowConnection(c);
673+
poolListener.onAfterBorrowConnection(this, c);
674674
}
675675
return c;
676676
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ static class PoolManager implements DataSourcePoolListener {
5050
Semaphore semaphore = new Semaphore(120);
5151
Random random = new Random();
5252

53-
@Override
54-
public void onBeforeCreateConnection() {
53+
@Override
54+
public void onBeforeCreateConnection(DataSourcePool pool) {
5555
try {
5656
while (!semaphore.tryAcquire(50, TimeUnit.MILLISECONDS)) {
5757
System.out.println("trim required");
@@ -66,7 +66,6 @@ public void onAfterCloseConnection() {
6666
semaphore.release();
6767
}
6868

69-
7069
}
7170

7271
private static PoolManager poolManager = new PoolManager();

0 commit comments

Comments
 (0)