mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-10-05 22:49:02 +00:00
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:
+1
@@ -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());
|
||||
}
|
||||
|
||||
+28
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user