Skip to content

Commit 230422f

Browse files
committed
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 2bd5b84 commit 230422f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

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

+4-4
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

@@ -98,7 +99,7 @@ SELECT COUNT(*)
9899

99100
private static final String UPDATE_JOB_EXECUTION = """
100101
UPDATE %PREFIX%JOB_EXECUTION
101-
SET START_TIME = ?, END_TIME = ?, STATUS = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = ?, CREATE_TIME = ?, LAST_UPDATED = ?
102+
SET START_TIME = ?, END_TIME = ?, STATUS = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = VERSION + 1, CREATE_TIME = ?, LAST_UPDATED = ?
102103
WHERE JOB_EXECUTION_ID = ? AND VERSION = ?
103104
""";
104105

@@ -284,7 +285,6 @@ public void updateJobExecution(JobExecution jobExecution) {
284285

285286
this.lock.lock();
286287
try {
287-
Integer version = jobExecution.getVersion() + 1;
288288

289289
String exitDescription = jobExecution.getExitStatus().getExitDescription();
290290
if (exitDescription != null && exitDescription.length() > exitMessageLength) {
@@ -301,7 +301,7 @@ public void updateJobExecution(JobExecution jobExecution) {
301301
Timestamp lastUpdated = jobExecution.getLastUpdated() == null ? null
302302
: Timestamp.valueOf(jobExecution.getLastUpdated());
303303
Object[] parameters = new Object[] { startTime, endTime, jobExecution.getStatus().toString(),
304-
jobExecution.getExitStatus().getExitCode(), exitDescription, version, createTime, lastUpdated,
304+
jobExecution.getExitStatus().getExitCode(), exitDescription, createTime, lastUpdated,
305305
jobExecution.getId(), jobExecution.getVersion() };
306306

307307
// Check if given JobExecution's Id already exists, if none is found
@@ -315,7 +315,7 @@ public void updateJobExecution(JobExecution jobExecution) {
315315

316316
int count = getJdbcTemplate().update(getQuery(UPDATE_JOB_EXECUTION), parameters,
317317
new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
318-
Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
318+
Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
319319

320320
// Avoid concurrent modifications...
321321
if (count == 0) {

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

+5-5
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 {
@@ -79,7 +80,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
7980

8081
private static final String UPDATE_STEP_EXECUTION = """
8182
UPDATE %PREFIX%STEP_EXECUTION
82-
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 = ?
83+
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 = ?
8384
WHERE STEP_EXECUTION_ID = ? AND VERSION = ?
8485
""";
8586

@@ -267,7 +268,6 @@ public void updateStepExecution(StepExecution stepExecution) {
267268
this.lock.lock();
268269
try {
269270

270-
Integer version = stepExecution.getVersion() + 1;
271271
Timestamp startTime = stepExecution.getStartTime() == null ? null
272272
: Timestamp.valueOf(stepExecution.getStartTime());
273273
Timestamp endTime = stepExecution.getEndTime() == null ? null
@@ -277,13 +277,13 @@ public void updateStepExecution(StepExecution stepExecution) {
277277
Object[] parameters = new Object[] { startTime, endTime, stepExecution.getStatus().toString(),
278278
stepExecution.getCommitCount(), stepExecution.getReadCount(), stepExecution.getFilterCount(),
279279
stepExecution.getWriteCount(), stepExecution.getExitStatus().getExitCode(), exitDescription,
280-
version, stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
280+
stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
281281
stepExecution.getWriteSkipCount(), stepExecution.getRollbackCount(), lastUpdated,
282282
stepExecution.getId(), stepExecution.getVersion() };
283283
int count = getJdbcTemplate().update(getQuery(UPDATE_STEP_EXECUTION), parameters,
284284
new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.BIGINT, Types.BIGINT,
285-
Types.BIGINT, Types.BIGINT, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.BIGINT,
286-
Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
285+
Types.BIGINT, Types.BIGINT, Types.VARCHAR, Types.VARCHAR, Types.BIGINT, Types.BIGINT,
286+
Types.BIGINT, Types.BIGINT, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });
287287

288288
// Avoid concurrent modifications...
289289
if (count == 0) {

0 commit comments

Comments
 (0)