1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.datasource.pooled;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import java.sql.Connection;
21 import java.sql.PreparedStatement;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import java.util.concurrent.TimeUnit;
26
27 import org.apache.ibatis.testcontainers.MysqlContainer;
28 import org.junit.jupiter.api.Tag;
29 import org.junit.jupiter.api.Test;
30
31 @Tag("TestcontainersTests")
32 public class MysqlTimeoutTest {
33
34 @Test
35 void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
36
37 PooledDataSource ds = MysqlContainer.getPooledDataSource();
38 ds.setPoolMaximumActiveConnections(1);
39 ds.setPoolMaximumIdleConnections(1);
40 ds.setPoolTimeToWait(1000);
41 ds.setPoolMaximumCheckoutTime(2000);
42 ds.setPoolPingEnabled(true);
43 ds.setPoolPingQuery("select 1");
44 ds.setDefaultAutoCommit(true);
45
46 ds.setPoolPingConnectionsNotUsedFor(1000);
47
48 Connection con1 = ds.getConnection();
49
50 Statement stmt = con1.createStatement();
51 stmt.execute("set session wait_timeout = 3");
52
53 executeQuery(con1);
54
55
56
57
58 Thread.sleep(TimeUnit.SECONDS.toMillis(3));
59
60
61 Connection con2 = ds.getConnection();
62 executeQuery(con2);
63
64 con1.close();
65 con2.close();
66 }
67
68 private void executeQuery(Connection con) throws SQLException {
69 try (PreparedStatement st = con.prepareStatement("select 1"); ResultSet rs = st.executeQuery()) {
70 while (rs.next()) {
71 assertEquals(1, rs.getInt(1));
72 }
73 }
74 }
75
76 }