Skip to content

Commit eb6c9a2

Browse files
fix: incorrect generated keys after batch and individual statement executions (#490)
1 parent 7591def commit eb6c9a2

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/main/user-impl/java/com/mysql/cj/jdbc/StatementImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,7 @@ protected long executeUpdateInternal(String sql, boolean isBatch, boolean return
13231323

13241324
if (!isBatch) {
13251325
this.query.getStatementExecuting().set(false);
1326+
this.batchedGeneratedKeys = null;
13261327
}
13271328
}
13281329

src/test/java/testsuite/regression/StatementRegressionTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12724,7 +12724,6 @@ public void testBugIssue464() throws Exception {
1272412724

1272512725
String tableName = "testBugIssue464";
1272612726

12727-
final Statement statement = this.conn.createStatement();
1272812727
createTable(tableName, "(c0 INT)");
1272912728

1273012729
Statement bstmt = conn.createStatement();
@@ -12749,4 +12748,48 @@ public void testBugIssue464() throws Exception {
1274912748
}
1275012749
assertEquals(3, counter);
1275112750
}
12751+
12752+
/**
12753+
* Test fix for Issue #484. Verifies that getGeneratedKeys() will work after executing both batch and individual
12754+
* statements.
12755+
*
12756+
* @throws Exception
12757+
*/
12758+
@Test
12759+
public void testBugIssue484() throws Exception {
12760+
Properties props = new Properties();
12761+
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
12762+
props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
12763+
this.conn = getConnectionWithProps(props);
12764+
12765+
String tableName = "testBugIssue484";
12766+
12767+
createTable(tableName, "(c0 INT AUTO_INCREMENT PRIMARY KEY)");
12768+
12769+
Statement stmt = conn.createStatement();
12770+
stmt.addBatch("INSERT INTO " + tableName + " VALUES(1)");
12771+
stmt.addBatch("INSERT INTO " + tableName + " VALUES(2)");
12772+
stmt.executeBatch();
12773+
12774+
ResultSet rs1 = stmt.getGeneratedKeys();
12775+
int counter = 0;
12776+
while (rs1.next()) {
12777+
counter++;
12778+
}
12779+
rs1.close();
12780+
assertEquals(2, counter);
12781+
12782+
stmt.executeUpdate("INSERT INTO " + tableName + " VALUES (DEFAULT)", Statement.RETURN_GENERATED_KEYS);
12783+
12784+
ResultSet rs2 = stmt.getGeneratedKeys();
12785+
counter = 0;
12786+
int key = 0;
12787+
while (rs2.next()) {
12788+
counter++;
12789+
key = rs2.getInt(1);
12790+
}
12791+
rs2.close();
12792+
assertEquals(1, counter);
12793+
assertEquals(3, key);
12794+
}
1275212795
}

0 commit comments

Comments
 (0)