|
| 1 | +package org.example.tests; |
| 2 | + |
| 3 | +import io.ebean.datasource.DataSourceBuilder; |
| 4 | +import io.ebean.datasource.DataSourcePool; |
| 5 | +import io.ebean.test.containers.PostgresContainer; |
| 6 | +import org.junit.jupiter.api.BeforeAll; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Properties; |
| 16 | + |
| 17 | +class Java21TrimAndShutdownTest { |
| 18 | + |
| 19 | + private static Logger log = LoggerFactory.getLogger(Java21TrimAndShutdownTest.class); |
| 20 | + |
| 21 | + @BeforeAll |
| 22 | + static void before() { |
| 23 | + PostgresContainer.builder("15") |
| 24 | + .port(9999) |
| 25 | + .containerName("pool_test") |
| 26 | + .dbName("app") |
| 27 | + .user("db_owner") |
| 28 | + .build() |
| 29 | + .startWithDropCreate(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void test() throws InterruptedException, SQLException { |
| 34 | + Properties clientInfo = new Properties(); |
| 35 | + clientInfo.setProperty("ApplicationName", "my-test"); |
| 36 | + |
| 37 | + DataSourcePool pool = DataSourceBuilder.create() |
| 38 | + .url("jdbc:postgresql://127.0.0.1:9999/app") |
| 39 | + .username("db_owner") |
| 40 | + .password("test") |
| 41 | + .clientInfo(clientInfo) |
| 42 | + .maxInactiveTimeSecs(2) |
| 43 | + .heartbeatFreqSecs(1) |
| 44 | + .trimPoolFreqSecs(1) |
| 45 | + .build(); |
| 46 | + |
| 47 | + List<Connection> connectionList = new ArrayList<>(); |
| 48 | + for (int i = 0; i < 50; i++) { |
| 49 | + connectionList.add(pool.getConnection()); |
| 50 | + } |
| 51 | + |
| 52 | + // close them slowly to allow multiple trims |
| 53 | + for (Connection connection : connectionList) { |
| 54 | + connection.rollback(); |
| 55 | + connection.close(); |
| 56 | + Thread.sleep(200); |
| 57 | + } |
| 58 | + |
| 59 | + log.info("----------- Sleep allowing trim -------------"); |
| 60 | + Thread.sleep(9_000); |
| 61 | + log.info("----------- Shutdown pool -------------"); |
| 62 | + pool.shutdown(); |
| 63 | + } |
| 64 | +} |
0 commit comments