Background
I'm using Spring Batch 2.1.8, and run jobs by CommandLineJobRunner
. Such as:
java org.springframework.batch.core.launch.support.CommandLineJobRunner classpath:launchContext.xml theJobId
Problem
At some condition such as a server crash, running job could be interrupted.
But the interrupted job left a STARTED
status in the Spring Batch Meta-Data tables, and can't be run again.
org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running
I can think of two solutions:
Solution1
Add a new job parameter and change it everytime to make it a "new" job for Spring Batch. Such as:
java org.springframework.batch.core.launch.support.CommandLineJobRunner classpath:launchContext.xml theJobId times=0
And when need to rerun it, do clear all corresponding output data, count up times
once, and then rerun the job.
Solution2
Change the Spring Batch Meta-Data tables manually.
To update the status to make the job restartable. Such as:
UPDATE BATCH_JOB_EXECUTION SET END_TIME = SYSTIMESTAMP, STATUS = 'FAILED', EXIT_CODE = 'FAILOVER' WHERE JOB_EXECUTION_ID =
(SELECT MAX(JOB_EXECUTION_ID) FROM BATCH_JOB_EXECUTION WHERE JOB_INSTANCE_ID =
(SELECT MAX(JOB_INSTANCE_ID) FROM BATCH_JOB_INSTANCE WHERE JOB_NAME = 'XXX'));
I've tried it and it seems works well.
Question
Is Solution2 a bad idea? Are there any traps?
Thanks in advance. And any other solutions are appreciated.