From 7576a77c5303262283e256dfd5a280ef109fe70b Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Thu, 27 Mar 2025 12:57:50 +0800 Subject: [PATCH] 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 --- .../core/repository/dao/JdbcJobExecutionDao.java | 8 +++----- .../core/repository/dao/JdbcStepExecutionDao.java | 13 +++---------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java index 9ec0a9e2d8..041a3c46c0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java @@ -74,6 +74,7 @@ * @author Dimitrios Liapis * @author Philippe Marschall * @author Jinwoo Bae + * @author Yanming Zhou */ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean { @@ -319,11 +320,8 @@ public void updateJobExecution(JobExecution jobExecution) { // Avoid concurrent modifications... if (count == 0) { - int currentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_JOB_EXECUTION), - Integer.class, new Object[] { jobExecution.getId() }); - throw new OptimisticLockingFailureException( - "Attempt to update job execution id=" + jobExecution.getId() + " with wrong version (" - + jobExecution.getVersion() + "), where current version is " + currentVersion); + throw new OptimisticLockingFailureException("Attempt to update job execution id=" + jobExecution.getId() + + " with wrong version (" + jobExecution.getVersion() + ")"); } jobExecution.incrementVersion(); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java index b1e46e0c23..1a614d0927 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java @@ -66,6 +66,7 @@ * @author Mahmoud Ben Hassine * @author Baris Cubukcuoglu * @author Minsoo Kim + * @author Yanming Zhou * @see StepExecutionDao */ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean { @@ -100,11 +101,6 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ? """; - private static final String CURRENT_VERSION_STEP_EXECUTION = """ - SELECT VERSION FROM %PREFIX%STEP_EXECUTION - WHERE STEP_EXECUTION_ID=? - """; - private static final String COUNT_STEP_EXECUTIONS = """ SELECT COUNT(*) FROM %PREFIX%JOB_EXECUTION JE @@ -287,11 +283,8 @@ public void updateStepExecution(StepExecution stepExecution) { // Avoid concurrent modifications... if (count == 0) { - int currentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_STEP_EXECUTION), - Integer.class, stepExecution.getId()); - throw new OptimisticLockingFailureException( - "Attempt to update step execution id=" + stepExecution.getId() + " with wrong version (" - + stepExecution.getVersion() + "), where current version is " + currentVersion); + throw new OptimisticLockingFailureException("Attempt to update step execution id=" + + stepExecution.getId() + " with wrong version (" + stepExecution.getVersion() + ")"); } stepExecution.incrementVersion();