Skip to content

Commit 10598bf

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 08c4cb1 commit 10598bf

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/jdbc/JdbcJobExecutionDao.java

Lines changed: 4 additions & 4 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

@@ -101,7 +102,7 @@ SELECT COUNT(*)
101102

102103
private static final String UPDATE_JOB_EXECUTION = """
103104
UPDATE %PREFIX%JOB_EXECUTION
104-
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 = ?
105106
WHERE JOB_EXECUTION_ID = ? AND VERSION = ?
106107
""";
107108

@@ -287,7 +288,6 @@ public void updateJobExecution(JobExecution jobExecution) {
287288

288289
this.lock.lock();
289290
try {
290-
Integer version = jobExecution.getVersion() + 1;
291291

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

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

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

323323
// Avoid concurrent modifications...
324324
if (count == 0) {

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

Lines changed: 5 additions & 5 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 {
@@ -81,7 +82,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
8182

8283
private static final String UPDATE_STEP_EXECUTION = """
8384
UPDATE %PREFIX%STEP_EXECUTION
84-
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 = ?
8586
WHERE STEP_EXECUTION_ID = ? AND VERSION = ?
8687
""";
8788

@@ -269,7 +270,6 @@ public void updateStepExecution(StepExecution stepExecution) {
269270
this.lock.lock();
270271
try {
271272

272-
Integer version = stepExecution.getVersion() + 1;
273273
Timestamp startTime = stepExecution.getStartTime() == null ? null
274274
: Timestamp.valueOf(stepExecution.getStartTime());
275275
Timestamp endTime = stepExecution.getEndTime() == null ? null
@@ -279,13 +279,13 @@ public void updateStepExecution(StepExecution stepExecution) {
279279
Object[] parameters = new Object[] { startTime, endTime, stepExecution.getStatus().toString(),
280280
stepExecution.getCommitCount(), stepExecution.getReadCount(), stepExecution.getFilterCount(),
281281
stepExecution.getWriteCount(), stepExecution.getExitStatus().getExitCode(), exitDescription,
282-
version, stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
282+
stepExecution.getReadSkipCount(), stepExecution.getProcessSkipCount(),
283283
stepExecution.getWriteSkipCount(), stepExecution.getRollbackCount(), lastUpdated,
284284
stepExecution.getId(), stepExecution.getVersion() };
285285
int count = getJdbcTemplate().update(getQuery(UPDATE_STEP_EXECUTION), parameters,
286286
new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.BIGINT, Types.BIGINT,
287-
Types.BIGINT, Types.BIGINT, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.BIGINT,
288-
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 });
289289

290290
// Avoid concurrent modifications...
291291
if (count == 0) {

0 commit comments

Comments
 (0)