Skip to content

Commit 1e6d86e

Browse files
committed
Add test that shows datasource-down on high pool usage
1 parent e8c042e commit 1e6d86e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.ebean.datasource.pool;
2+
3+
import io.ebean.datasource.DataSourceAlert;
4+
import io.ebean.datasource.DataSourceBuilder;
5+
import io.ebean.datasource.DataSourcePool;
6+
import org.junit.jupiter.api.Test;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import javax.sql.DataSource;
11+
import java.sql.Connection;
12+
import java.sql.SQLException;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
public class ConnectionPoolFullTest implements DataSourceAlert, WaitFor {
17+
18+
private static final Logger log = LoggerFactory.getLogger(ConnectionPoolFullTest.class);
19+
20+
private int up;
21+
private int down;
22+
23+
24+
@Test
25+
void testPoolFullWithHeartbeat() throws Exception {
26+
27+
DataSourcePool pool = DataSourceBuilder.create()
28+
.url("jdbc:h2:mem:testConnectionPoolFull")
29+
.username("sa")
30+
.password("sa")
31+
.heartbeatFreqSecs(1)
32+
.minConnections(1)
33+
.maxConnections(1)
34+
.trimPoolFreqSecs(1)
35+
.alert(this)
36+
.failOnStart(false)
37+
.build();
38+
39+
assertThat(up).isEqualTo(1);
40+
assertThat(down).isEqualTo(0);
41+
42+
try {
43+
try (Connection connection = pool.getConnection()) {
44+
// we do a rollback here
45+
System.out.println("So we wait");
46+
Thread.sleep(2000);
47+
connection.rollback();
48+
}
49+
} finally {
50+
pool.shutdown();
51+
}
52+
assertThat(up).isEqualTo(1);
53+
assertThat(down).isEqualTo(0);
54+
55+
}
56+
57+
58+
@Override
59+
public void dataSourceUp(DataSource dataSource) {
60+
up++;
61+
}
62+
63+
@Override
64+
public void dataSourceDown(DataSource dataSource, SQLException reason) {
65+
down++;
66+
}
67+
}

0 commit comments

Comments
 (0)