Skip to content

Commit d2a3e1b

Browse files
committed
Add version to delete sql for optimistic locking
Signed-off-by: Yanming Zhou <[email protected]>
1 parent 2bd5b84 commit d2a3e1b

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* @author Dimitrios Liapis
7575
* @author Philippe Marschall
7676
* @author Jinwoo Bae
77+
* @author Yanming Zhou
7778
*/
7879
public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean {
7980

@@ -146,7 +147,7 @@ SELECT COUNT(*)
146147

147148
private static final String DELETE_JOB_EXECUTION = """
148149
DELETE FROM %PREFIX%JOB_EXECUTION
149-
WHERE JOB_EXECUTION_ID = ?
150+
WHERE JOB_EXECUTION_ID = ? AND VERSION = ?
150151
""";
151152

152153
private static final String DELETE_JOB_EXECUTION_PARAMETERS = """
@@ -395,7 +396,13 @@ public void synchronizeStatus(JobExecution jobExecution) {
395396
*/
396397
@Override
397398
public void deleteJobExecution(JobExecution jobExecution) {
398-
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION), jobExecution.getId());
399+
int count = getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION), jobExecution.getId(),
400+
jobExecution.getVersion());
401+
402+
if (count == 0) {
403+
throw new OptimisticLockingFailureException("Attempt to delete job execution id=" + jobExecution.getId()
404+
+ " with wrong version (" + jobExecution.getVersion() + ")");
405+
}
399406
}
400407

401408
/**

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobInstanceDao.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.beans.factory.InitializingBean;
3232
import org.springframework.dao.DataAccessException;
3333
import org.springframework.dao.EmptyResultDataAccessException;
34+
import org.springframework.dao.OptimisticLockingFailureException;
3435
import org.springframework.jdbc.core.ResultSetExtractor;
3536
import org.springframework.jdbc.core.RowMapper;
3637
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
@@ -53,6 +54,7 @@
5354
* @author Will Schipp
5455
* @author Mahmoud Ben Hassine
5556
* @author Parikshit Dutta
57+
* @author Yanming Zhou
5658
*/
5759
public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements JobInstanceDao, InitializingBean {
5860

@@ -124,7 +126,7 @@ SELECT COUNT(*)
124126

125127
private static final String DELETE_JOB_INSTANCE = """
126128
DELETE FROM %PREFIX%JOB_INSTANCE
127-
WHERE JOB_INSTANCE_ID = ?
129+
WHERE JOB_INSTANCE_ID = ? AND VERSION = ?
128130
""";
129131

130132
private DataFieldMaxValueIncrementer jobInstanceIncrementer;
@@ -281,7 +283,13 @@ public long getJobInstanceCount(@Nullable String jobName) throws NoSuchJobExcept
281283
*/
282284
@Override
283285
public void deleteJobInstance(JobInstance jobInstance) {
284-
getJdbcTemplate().update(getQuery(DELETE_JOB_INSTANCE), jobInstance.getId());
286+
int count = getJdbcTemplate().update(getQuery(DELETE_JOB_INSTANCE), jobInstance.getId(),
287+
jobInstance.getVersion());
288+
289+
if (count == 0) {
290+
throw new OptimisticLockingFailureException("Attempt to delete job instance id=" + jobInstance.getId()
291+
+ " with wrong version (" + jobInstance.getVersion() + ")");
292+
}
285293
}
286294

287295
/**

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* @author Mahmoud Ben Hassine
6767
* @author Baris Cubukcuoglu
6868
* @author Minsoo Kim
69+
* @author Yanming Zhou
6970
* @see StepExecutionDao
7071
*/
7172
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -114,7 +115,7 @@ SELECT COUNT(*)
114115

115116
private static final String DELETE_STEP_EXECUTION = """
116117
DELETE FROM %PREFIX%STEP_EXECUTION
117-
WHERE STEP_EXECUTION_ID = ?
118+
WHERE STEP_EXECUTION_ID = ? and VERSION = ?
118119
""";
119120

120121
private static final Comparator<StepExecution> BY_CREATE_TIME_DESC_ID_DESC = Comparator
@@ -378,7 +379,13 @@ public long countStepExecutions(JobInstance jobInstance, String stepName) {
378379
*/
379380
@Override
380381
public void deleteStepExecution(StepExecution stepExecution) {
381-
getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION), stepExecution.getId());
382+
int count = getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION), stepExecution.getId(),
383+
stepExecution.getVersion());
384+
385+
if (count == 0) {
386+
throw new OptimisticLockingFailureException("Attempt to delete step execution id=" + stepExecution.getId()
387+
+ " with wrong version (" + stepExecution.getVersion() + ")");
388+
}
382389
}
383390

384391
private static class StepExecutionRowMapper implements RowMapper<StepExecution> {

0 commit comments

Comments
 (0)