When INSERT ... ON DUPLICATE KEY UPDATE statement updated a row, the ResultSet returned from getGeneratedKeys() contains two rows.
Here is a failing test case (I wrote it in MultiTest).
@Test
|
public void shouldDuplicateKeyUpdateNotReturnExtraRows() throws Throwable {
|
try (Connection con = openNewConnection(connUri, new Properties())) {
|
try (Statement stmt = con.createStatement()) {
|
stmt.execute("truncate table testMultiGeneratedKey");
|
}
|
try (PreparedStatement pstmt = con.prepareStatement(
|
"INSERT INTO testMultiGeneratedKey (id, text) VALUES (?, ?) "
|
+ "ON DUPLICATE KEY UPDATE text = VALUES(text)",
|
Statement.RETURN_GENERATED_KEYS)) {
|
// Insert a row.
|
pstmt.setInt(1, 1);
|
pstmt.setString(2, "initial");
|
assertEquals(1, pstmt.executeUpdate());
|
try (ResultSet rs = pstmt.getGeneratedKeys()) {
|
assertTrue(rs.next());
|
assertEquals(1, rs.getInt(1));
|
assertFalse(rs.next());
|
}
|
// Update the row.
|
pstmt.setInt(1, 1);
|
pstmt.setString(2, "updated");
|
assertEquals(2, pstmt.executeUpdate());
|
try (ResultSet rs = pstmt.getGeneratedKeys()) {
|
assertTrue(rs.next());
|
assertEquals(1, rs.getInt(1));
|
assertFalse(rs.next());
|
}
|
}
|
}
|
}
|
getGeneratedKeys() ultimately calls org.mariadb.jdbc.internal.com.read.dao.CmdInformationSingle.getGeneratedKeys(Protocol).
In this method, updateCount is used as the size of returned result set, however, updateCount is 2 when INSERT ... ON DUPLICATE KEY UPDATE statement updated a row according to the doc.