Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

java - Is it a bad idea to change the Spring Batch Meta-Data tables manually?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solution 2 is the accepted approach right now. The API does not provide a way to fix this scenario. There have been requests in the past for the framework to clean up automatically, but 99% of the time, a human decision is needed to determine if cleanup is truly required.

My only note for option 2 would be to check the BATCH_STEP_EXECUTION table as well to see what state the last executed step was left in.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...