View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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      // See #748
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      // MySQL wait_timeout * 1000 or less. (unit:ms)
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      // Simulate connection leak by not closing.
55      // con1.close();
56  
57      // Wait for disconnected from mysql...
58      Thread.sleep(TimeUnit.SECONDS.toMillis(3));
59  
60      // Should return usable connection.
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  }