Fix cached ID reuse after commit failure in MySQLMaxValueIncrementer

Invalidate the cached ID range when commit or auto-commit restoration
fails so subsequent calls obtain a new range from the database.

Closes gh-37322

Signed-off-by: cookie-meringue <daehyeon3351@gmail.com>
This commit is contained in:
머랭
2026-10-04 13:09:52 +02:00
committed by GitHub
parent eb9928f993
commit f96403da2a
2 changed files with 29 additions and 0 deletions
@@ -179,6 +179,7 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer
}
}
catch (SQLException ignore) {
this.nextId = this.maxId;
throw new DataAccessResourceFailureException(
"Unable to commit new sequence value changes for " + getIncrementerName());
}
@@ -237,6 +237,34 @@ class DataFieldMaxValueIncrementerTests {
verify(connection).close();
}
@Test
void mySQLMaxValueIncrementerWithCommitFailure() throws SQLException {
given(dataSource.getConnection()).willReturn(connection);
given(connection.createStatement()).willReturn(statement);
given(statement.executeQuery("select last_insert_id()")).willReturn(resultSet);
given(resultSet.next()).willReturn(true);
given(resultSet.getLong(1)).willReturn(2L, 2L, 4L);
willThrow(new SQLException("Cannot commit")).willDoNothing().given(connection).commit();
MySQLMaxValueIncrementer incrementer = new MySQLMaxValueIncrementer();
incrementer.setDataSource(dataSource);
incrementer.setIncrementerName("myseq");
incrementer.setColumnName("seq");
incrementer.setCacheSize(2);
incrementer.afterPropertiesSet();
assertThatExceptionOfType(DataAccessResourceFailureException.class)
.isThrownBy(incrementer::nextLongValue);
assertThat(incrementer.nextLongValue()).isEqualTo(1);
assertThat(incrementer.nextLongValue()).isEqualTo(2);
assertThat(incrementer.nextLongValue()).isEqualTo(3);
assertThat(incrementer.nextLongValue()).isEqualTo(4);
verify(dataSource, times(3)).getConnection();
verify(statement, times(3)).executeUpdate("update myseq set seq = last_insert_id(seq + 2) limit 1");
verify(connection, times(3)).commit();
}
@Test
void mariaDBSequenceMaxValueIncrementer() throws SQLException {
given(dataSource.getConnection()).willReturn(connection);