Skip to content

Commit 259cc71

Browse files
committed
Eliminate unnecessary query for current version
The version from query result is nondeterministic, it's not definitely same to the moment update executed because it may be altered right after that moment, it's not definitely same to the latest version in database because it may be altered right after the query executed. Given that, the current version is not so useful for troubleshooting in practice. Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 08c4cb1 commit 259cc71

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* @author Dimitrios Liapis
7878
* @author Philippe Marschall
7979
* @author Jinwoo Bae
80+
* @author Yanming Zhou
8081
*/
8182
public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean {
8283

@@ -322,11 +323,8 @@ public void updateJobExecution(JobExecution jobExecution) {
322323

323324
// Avoid concurrent modifications...
324325
if (count == 0) {
325-
int currentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_JOB_EXECUTION),
326-
Integer.class, new Object[] { jobExecution.getId() });
327-
throw new OptimisticLockingFailureException(
328-
"Attempt to update job execution id=" + jobExecution.getId() + " with wrong version ("
329-
+ jobExecution.getVersion() + "), where current version is " + currentVersion);
326+
throw new OptimisticLockingFailureException("Attempt to update job execution id=" + jobExecution.getId()
327+
+ " with wrong version (" + jobExecution.getVersion() + ")");
330328
}
331329

332330
jobExecution.incrementVersion();

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
* @author Mahmoud Ben Hassine
6969
* @author Baris Cubukcuoglu
7070
* @author Minsoo Kim
71+
* @author Yanming Zhou
7172
* @see StepExecutionDao
7273
*/
7374
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -102,11 +103,6 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
102103
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
103104
""";
104105

105-
private static final String CURRENT_VERSION_STEP_EXECUTION = """
106-
SELECT VERSION FROM %PREFIX%STEP_EXECUTION
107-
WHERE STEP_EXECUTION_ID=?
108-
""";
109-
110106
private static final String COUNT_STEP_EXECUTIONS = """
111107
SELECT COUNT(*)
112108
FROM %PREFIX%JOB_EXECUTION JE
@@ -289,11 +285,8 @@ public void updateStepExecution(StepExecution stepExecution) {
289285

290286
// Avoid concurrent modifications...
291287
if (count == 0) {
292-
int currentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_STEP_EXECUTION),
293-
Integer.class, stepExecution.getId());
294-
throw new OptimisticLockingFailureException(
295-
"Attempt to update step execution id=" + stepExecution.getId() + " with wrong version ("
296-
+ stepExecution.getVersion() + "), where current version is " + currentVersion);
288+
throw new OptimisticLockingFailureException("Attempt to update step execution id="
289+
+ stepExecution.getId() + " with wrong version (" + stepExecution.getVersion() + ")");
297290
}
298291

299292
stepExecution.incrementVersion();

0 commit comments

Comments
 (0)