Skip to content

Commit 92a304e

Browse files
quafffmbenhassine
authored andcommitted
Improve update sql for optimistic locking
The version to be updated could be computed at server side instead of client side, it will save one parameter of prepared statement. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 22cfc55 commit 92a304e

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ SELECT COUNT(*)
102102

103103
private static final String UPDATE_JOB_EXECUTION = """
104104
UPDATE %PREFIX%JOB_EXECUTION
105-
SET START_TIME = ?, END_TIME = ?, STATUS = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = ?, CREATE_TIME = ?, LAST_UPDATED = ?
105+
SET START_TIME = ?, END_TIME = ?, STATUS = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = VERSION + 1, CREATE_TIME = ?, LAST_UPDATED = ?
106106
WHERE JOB_EXECUTION_ID = ? AND VERSION = ?
107107
""";
108108

@@ -282,7 +282,6 @@ public void updateJobExecution(JobExecution jobExecution) {
282282

283283
this.lock.lock();
284284
try {
285-
Integer version = jobExecution.getVersion() + 1;
286285

287286
String exitDescription = jobExecution.getExitStatus().getExitDescription();
288287
if (exitDescription != null && exitDescription.length() > exitMessageLength) {
@@ -299,7 +298,7 @@ public void updateJobExecution(JobExecution jobExecution) {
299298
Timestamp lastUpdated = jobExecution.getLastUpdated() == null ? null
300299
: Timestamp.valueOf(jobExecution.getLastUpdated());
301300
Object[] parameters = new Object[] { startTime, endTime, jobExecution.getStatus().toString(),
302-
jobExecution.getExitStatus().getExitCode(), exitDescription, version, createTime, lastUpdated,
301+
jobExecution.getExitStatus().getExitCode(), exitDescription, createTime, lastUpdated,
303302
jobExecution.getId(), jobExecution.getVersion() };
304303

305304
// Check if given JobExecution's Id already exists, if none is found
@@ -313,7 +312,7 @@ public void updateJobExecution(JobExecution jobExecution) {
313312

314313
int count = getJdbcTemplate().update(getQuery(UPDATE_JOB_EXECUTION), parameters,
315314
new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
316-
Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
315+
Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
317316

318317
// Avoid concurrent modifications...
319318
if (count == 0) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
8282

8383
private static final String UPDATE_STEP_EXECUTION = """
8484
UPDATE %PREFIX%STEP_EXECUTION
85-
SET START_TIME = ?, END_TIME = ?, STATUS = ?, COMMIT_COUNT = ?, READ_COUNT = ?, FILTER_COUNT = ?, WRITE_COUNT = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = ?, READ_SKIP_COUNT = ?, PROCESS_SKIP_COUNT = ?, WRITE_SKIP_COUNT = ?, ROLLBACK_COUNT = ?, LAST_UPDATED = ?
85+
SET START_TIME = ?, END_TIME = ?, STATUS = ?, COMMIT_COUNT = ?, READ_COUNT = ?, FILTER_COUNT = ?, WRITE_COUNT = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = VERSION + 1, READ_SKIP_COUNT = ?, PROCESS_SKIP_COUNT = ?, WRITE_SKIP_COUNT = ?, ROLLBACK_COUNT = ?, LAST_UPDATED = ?
8686
WHERE STEP_EXECUTION_ID = ? AND VERSION = ?
8787
""";
8888

@@ -270,7 +270,6 @@ public void updateStepExecution(StepExecution stepExecution) {
270270
this.lock.lock();
271271
try {
272272

273-
Integer version = stepExecution.getVersion() + 1;
274273
Timestamp startTime = stepExecution.getStartTime() == null ? null
275274
: Timestamp.valueOf(stepExecution.getStartTime());
276275
Timestamp endTime = stepExecution.getEndTime() == null ? null
@@ -280,13 +279,13 @@ public void updateStepExecution(StepExecution stepExecution) {
280279
Object[] parameters = new Object[] { startTime, endTime, stepExecution.getStatus().toString(),
281280
stepExecution.getCommitCount(), stepExecution.getReadCount(), stepExecution.getFilterCount(),
282281
stepExecution.getWriteCount(), stepExecution.getExitStatus().getExitCode(), exitDescription,
283-
version, stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
282+
stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
284283
stepExecution.getWriteSkipCount(), stepExecution.getRollbackCount(), lastUpdated,
285284
stepExecution.getId(), stepExecution.getVersion() };
286285
int count = getJdbcTemplate().update(getQuery(UPDATE_STEP_EXECUTION), parameters,
287286
new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.BIGINT, Types.BIGINT,
288-
Types.BIGINT, Types.BIGINT, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.BIGINT,
289-
Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
287+
Types.BIGINT, Types.BIGINT, Types.VARCHAR, Types.VARCHAR, Types.BIGINT, Types.BIGINT,
288+
Types.BIGINT, Types.BIGINT, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
290289

291290
// Avoid concurrent modifications...
292291
if (count == 0) {

0 commit comments

Comments
 (0)