Skip to content

Commit

Permalink
Fix for Bug#96900 (30355150), STATEMENT.CANCEL()CREATE A DATABASE
Browse files Browse the repository at this point in the history
CONNECTION BUT DOES NOT CLOSE THE CONNECTION.
  • Loading branch information
soklakov committed Nov 23, 2021
1 parent 4d19ea1 commit ad46620
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Version 8.0.28

- Fix for Bug#96900 (30355150), STATEMENT.CANCEL()CREATE A DATABASE CONNECTION BUT DOES NOT CLOSE THE CONNECTION.

- Fix for Bug#104067 (33054827), No reset autoCommit after unknown issue occurs.
Thanks to Tingyu Wei for his contribution.

Expand Down
13 changes: 4 additions & 9 deletions src/main/user-impl/java/com/mysql/cj/jdbc/StatementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,14 @@ public void cancel() throws SQLException {
}

if (!this.isClosed && this.connection != null) {
JdbcConnection cancelConn = null;
java.sql.Statement cancelStmt = null;
NativeSession newSession = null;

try {
HostInfo hostInfo = this.session.getHostInfo();
String database = hostInfo.getDatabase();
String user = hostInfo.getUser();
String password = hostInfo.getPassword();
NativeSession newSession = new NativeSession(this.session.getHostInfo(), this.session.getPropertySet());
newSession = new NativeSession(this.session.getHostInfo(), this.session.getPropertySet());
newSession.connect(hostInfo, user, password, database, 30000, new TransactionEventHandler() {
@Override
public void transactionCompleted() {
Expand All @@ -313,12 +312,8 @@ public void transactionBegun() {
} catch (IOException e) {
throw SQLExceptionsMapping.translateException(e, this.exceptionInterceptor);
} finally {
if (cancelStmt != null) {
cancelStmt.close();
}

if (cancelConn != null) {
cancelConn.close();
if (newSession != null) {
newSession.forceClose();
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/testsuite/regression/StatementRegressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11898,4 +11898,46 @@ public void testBug85223() throws Exception {
String value = this.rs.getString(1);
assertEquals("LName", value);
}

/**
* Test fix for Bug#96900 (30355150), STATEMENT.CANCEL()CREATE A DATABASE CONNECTION BUT DOES NOT CLOSE THE CONNECTION.
*
* @throws Exception
*/
@Test
public void testBug96900() throws Exception {
assumeTrue(versionMeetsMinimum(5, 6), "MySQL 5.6+ is required to run this test.");

Properties props = new Properties();
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
props.setProperty(PropertyKey.connectionAttributes.getKeyName(), "testBug96900:1");

Connection con = getConnectionWithProps(props);
Statement st = con.createStatement();
new Thread(() -> {
try {
st.executeQuery("SELECT SLEEP(600)");
} catch (Throwable e) {
e.printStackTrace();
}
}).start();

String attCountQuery = "SELECT COUNT(*) FROM performance_schema.session_connect_attrs WHERE attr_name = 'testBug96900'";

this.rs = this.stmt.executeQuery(attCountQuery);
this.rs.next();
assertEquals(1, this.rs.getInt(1));

st.cancel();
this.rs = this.stmt.executeQuery(attCountQuery);
this.rs.next();
assertEquals(1, this.rs.getInt(1));

con.close();
this.rs = this.stmt.executeQuery(attCountQuery);
this.rs.next();
assertEquals(0, this.rs.getInt(1));
}

}

0 comments on commit ad46620

Please sign in to comment.