Skip to content

Commit ca897f8

Browse files
committed
Enabled a disabled test
Moved the test to a new file to add `@Tag`.
1 parent 335a72c commit ca897f8

File tree

3 files changed

+83
-59
lines changed

3 files changed

+83
-59
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2009-2021 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+
* http://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.*;
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");
70+
ResultSet rs = st.executeQuery()) {
71+
while (rs.next()) {
72+
assertEquals(1, rs.getInt(1));
73+
}
74+
}
75+
}
76+
77+
}

src/test/java/org/apache/ibatis/jdbc/PooledDataSourceTest.java

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@
1818
import static org.junit.jupiter.api.Assertions.*;
1919

2020
import java.sql.Connection;
21-
import java.sql.PreparedStatement;
22-
import java.sql.ResultSet;
23-
import java.sql.SQLException;
2421
import java.util.ArrayList;
2522
import java.util.List;
2623
import java.util.Properties;
27-
import java.util.concurrent.TimeUnit;
2824

2925
import org.apache.ibatis.BaseDataTest;
3026
import org.apache.ibatis.datasource.pooled.PooledDataSource;
3127
import org.hsqldb.jdbc.JDBCConnection;
32-
import org.junit.jupiter.api.Disabled;
3328
import org.junit.jupiter.api.Test;
3429

3530
class PooledDataSourceTest extends BaseDataTest {
@@ -90,58 +85,4 @@ void ShouldReturnRealConnection() throws Exception {
9085
JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
9186
c.close();
9287
}
93-
94-
@Disabled("See the comments")
95-
@Test
96-
void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
97-
// See #748
98-
// Requirements:
99-
// 1. MySQL JDBC driver dependency.
100-
// 2. MySQL server instance with the following.
101-
// - CREATE DATABASE `test`;
102-
// - SET GLOBAL wait_timeout=3;
103-
// 3. Tweak the connection info below.
104-
final String URL = "jdbc:mysql://localhost:3306/test";
105-
final String USERNAME = "admin";
106-
final String PASSWORD = "";
107-
108-
PooledDataSource ds = new PooledDataSource();
109-
ds.setDriver("com.mysql.jdbc.Driver");
110-
ds.setUrl(URL);
111-
ds.setUsername(USERNAME);
112-
ds.setPassword(PASSWORD);
113-
ds.setPoolMaximumActiveConnections(1);
114-
ds.setPoolMaximumIdleConnections(1);
115-
ds.setPoolTimeToWait(1000);
116-
ds.setPoolMaximumCheckoutTime(2000);
117-
ds.setPoolPingEnabled(true);
118-
ds.setPoolPingQuery("select 1");
119-
ds.setDefaultAutoCommit(true);
120-
// MySQL wait_timeout * 1000 or less. (unit:ms)
121-
ds.setPoolPingConnectionsNotUsedFor(1000);
122-
123-
Connection con = ds.getConnection();
124-
executeQuery(con);
125-
// Simulate connection leak by not closing.
126-
// con.close();
127-
128-
// Wait for disconnected from mysql...
129-
Thread.sleep(TimeUnit.SECONDS.toMillis(3));
130-
131-
con.close();
132-
133-
// Should return usable connection.
134-
con = ds.getConnection();
135-
executeQuery(con);
136-
con.close();
137-
}
138-
139-
private void executeQuery(Connection con) throws SQLException {
140-
try (PreparedStatement st = con.prepareStatement("select 1");
141-
ResultSet rs = st.executeQuery()) {
142-
while (rs.next()) {
143-
assertEquals(1, rs.getInt(1));
144-
}
145-
}
146-
}
14788
}

src/test/java/org/apache/ibatis/testcontainers/MysqlContainer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import javax.sql.DataSource;
1919

20+
import org.apache.ibatis.datasource.pooled.PooledDataSource;
2021
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2122
import org.testcontainers.containers.MySQLContainer;
2223

@@ -42,6 +43,11 @@ public static DataSource getUnpooledDataSource() {
4243
MysqlContainer.PASSWORD);
4344
}
4445

46+
public static PooledDataSource getPooledDataSource() {
47+
return new PooledDataSource(MysqlContainer.DRIVER, INSTANCE.getJdbcUrl(), MysqlContainer.USERNAME,
48+
MysqlContainer.PASSWORD);
49+
}
50+
4551
private MysqlContainer() {
4652
super();
4753
}

0 commit comments

Comments
 (0)