Skip to content

Commit 727ffc4

Browse files
committed
Add PoolStatus.size() helper as convenience for free + busy
1 parent 1668dd5 commit 727ffc4

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public interface PoolStatus {
2525
*/
2626
int busy();
2727

28+
/**
29+
* Return total number of free + busy connections in the pool.
30+
*/
31+
int size();
32+
2833
/**
2934
* Return the number of threads waiting for connections.
3035
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,11 @@ public int busy() {
934934
return busy;
935935
}
936936

937+
@Override
938+
public int size() {
939+
return free + busy;
940+
}
941+
937942
@Override
938943
public int waiting() {
939944
return waiting;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ void getConnection_expect_poolGrowsAboveMin() throws SQLException {
7878
assertThat(status.maxSize()).isEqualTo(4);
7979
}
8080

81+
@Test
82+
void status_size_isBusyPlusFree() throws SQLException {
83+
PoolStatus initial = pool.status(false);
84+
assertThat(initial.size()).isEqualTo(initial.busy() + initial.free());
85+
86+
Connection con1 = pool.getConnection();
87+
Connection con2 = pool.getConnection();
88+
Connection con3 = pool.getConnection();
89+
90+
PoolStatus allBusy = pool.status(false);
91+
assertThat(allBusy.busy()).isEqualTo(3);
92+
assertThat(allBusy.free()).isEqualTo(0);
93+
assertThat(allBusy.size()).isEqualTo(3);
94+
assertThat(allBusy.size()).isEqualTo(allBusy.busy() + allBusy.free());
95+
96+
con2.rollback();
97+
con2.close();
98+
99+
PoolStatus mixed = pool.status(false);
100+
assertThat(mixed.busy()).isEqualTo(2);
101+
assertThat(mixed.free()).isEqualTo(1);
102+
assertThat(mixed.size()).isEqualTo(3);
103+
assertThat(mixed.size()).isEqualTo(mixed.busy() + mixed.free());
104+
105+
con1.rollback();
106+
con1.close();
107+
con3.rollback();
108+
con3.close();
109+
110+
PoolStatus allFree = pool.status(false);
111+
assertThat(allFree.busy()).isEqualTo(0);
112+
assertThat(allFree.free()).isEqualTo(3);
113+
assertThat(allFree.size()).isEqualTo(3);
114+
assertThat(allFree.size()).isEqualTo(allFree.busy() + allFree.free());
115+
}
116+
81117
@Test
82118
void getConnection_explicitUserPassword() throws SQLException {
83119
Connection connection = pool.getConnection("sa", "");

0 commit comments

Comments
 (0)