Skip to content

Commit 3394bf6

Browse files
committed
Fix for Bug#113130 (Bug#36043125), getGeneratedKeys() returns a zero resultset with non-key-generating statements.
Change-Id: I386fdd321902bc8c7f7d39d50fe50b6b6d6d56db
1 parent 3b9c627 commit 3394bf6

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
Version 9.6.0
55

6+
- Fix for Bug#113130 (Bug#36043125), getGeneratedKeys() returns a zero resultset with non-key-generating statements.
7+
68
- Fix for Bug#113336 (Bug#36080226), Inconsistent getUpdateCount() Behavior with allowMultiQueries.
79

810
- Fix for Bug#118234 (Bug#37975837), A potential bugs in Mysql Connector/J.

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,10 +1152,11 @@ protected int processMultiCountsAndKeys(StatementImpl batchedStatement, int upda
11521152

11531153
if (doGenKeys) {
11541154
long generatedKey = batchedStatement.getLastInsertID();
1155-
1156-
row = new byte[1][];
1157-
row[0] = StringUtils.getBytes(Long.toString(generatedKey));
1158-
this.batchedGeneratedKeys.add(new ByteArrayRow(row, getExceptionInterceptor()));
1155+
if (generatedKey != 0) {
1156+
row = new byte[1][];
1157+
row[0] = StringUtils.getBytes(Long.toString(generatedKey));
1158+
this.batchedGeneratedKeys.add(new ByteArrayRow(row, getExceptionInterceptor()));
1159+
}
11591160
}
11601161

11611162
while (batchedStatement.getMoreResults() || batchedStatement.getLargeUpdateCount() != -1) {
@@ -1164,10 +1165,11 @@ protected int processMultiCountsAndKeys(StatementImpl batchedStatement, int upda
11641165

11651166
if (doGenKeys) {
11661167
long generatedKey = batchedStatement.getLastInsertID();
1167-
1168-
row = new byte[1][];
1169-
row[0] = StringUtils.getBytes(Long.toString(generatedKey));
1170-
this.batchedGeneratedKeys.add(new ByteArrayRow(row, getExceptionInterceptor()));
1168+
if (generatedKey != 0) {
1169+
row = new byte[1][];
1170+
row[0] = StringUtils.getBytes(Long.toString(generatedKey));
1171+
this.batchedGeneratedKeys.add(new ByteArrayRow(row, getExceptionInterceptor()));
1172+
}
11711173
}
11721174
}
11731175

@@ -1632,7 +1634,7 @@ protected ResultSetInternalMethods getGeneratedKeysInternal(long numKeys) throws
16321634

16331635
/**
16341636
* getLastInsertID returns the value of the auto_incremented key after an
1635-
* executeQuery() or excute() call.
1637+
* executeQuery() or execute() call.
16361638
*
16371639
* <p>
16381640
* This gets around the un-threadsafe behavior of "select LAST_INSERT_ID()" which is tied to the Connection that created this Statement, and therefore could

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14748,4 +14748,61 @@ public void testBug113336() throws Exception {
1474814748
} while (rwBS = !rwBS);
1474914749
}
1475014750

14751+
/**
14752+
* Tests fix for Bug#113130 (Bug#36043125), getGeneratedKeys() returns a zero resultset with non-key-generating statements.
14753+
*
14754+
* @throws SQLException
14755+
*/
14756+
@Test
14757+
public void testBug113130() throws SQLException {
14758+
createTable("testBug113130NoAI", "(id INT PRIMARY KEY, data VARCHAR(100))");
14759+
createTable("testBug113130AI", "(id INT AUTO_INCREMENT PRIMARY KEY, data VARCHAR(100))");
14760+
14761+
boolean allowMQ = false;
14762+
boolean rwBS = false;
14763+
do {
14764+
Properties props = new Properties();
14765+
props.setProperty(PropertyKey.allowMultiQueries.getKeyName(), Boolean.toString(allowMQ));
14766+
props.setProperty(PropertyKey.rewriteBatchedStatements.getKeyName(), Boolean.toString(rwBS));
14767+
14768+
for (int q = 1; q <= 5; q++) {
14769+
try (Connection testConn = getConnectionWithProps(props)) {
14770+
Statement testStmt = testConn.createStatement();
14771+
for (int i = 1; i <= q; i++) {
14772+
testStmt.addBatch(String.format("INSERT INTO testBug113130NoAI VALUES (%d, 'Data%d')", i, i));
14773+
}
14774+
int updCounts[] = testStmt.executeBatch();
14775+
assertEquals(q, updCounts.length);
14776+
for (int i = 0; i < q; i++) {
14777+
assertEquals(1, updCounts[i]);
14778+
}
14779+
this.rs = testStmt.getGeneratedKeys();
14780+
assertFalse(this.rs.next());
14781+
}
14782+
14783+
try (Connection testConn = getConnectionWithProps(props)) {
14784+
Statement testStmt = testConn.createStatement();
14785+
for (int i = 1; i <= q; i++) {
14786+
testStmt.addBatch(String.format("INSERT INTO testBug113130AI (data) VALUES ('Data%d')", i));
14787+
}
14788+
int updCounts[] = testStmt.executeBatch();
14789+
assertEquals(q, updCounts.length);
14790+
for (int i = 0; i < q; i++) {
14791+
assertEquals(1, updCounts[i]);
14792+
}
14793+
this.rs = testStmt.getGeneratedKeys();
14794+
for (int i = 1; i <= q; i++) {
14795+
assertTrue(this.rs.next());
14796+
assertEquals(i, this.rs.getInt(1));
14797+
}
14798+
assertFalse(this.rs.next());
14799+
}
14800+
14801+
this.stmt.execute("TRUNCATE TABLE testBug113130NoAI");
14802+
this.stmt.execute("TRUNCATE TABLE testBug113130AI");
14803+
this.stmt.execute("ALTER TABLE testBug113130AI AUTO_INCREMENT = 1");
14804+
}
14805+
} while ((allowMQ = !allowMQ) || (rwBS = !rwBS));
14806+
}
14807+
1475114808
}

0 commit comments

Comments
 (0)