I have two different Spring Batch projects, both are configured to have the same datasource (same DB schema) for the Meta-Data tables:
application.properties (Spring Batch A)
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/my_batch
application.properties (Spring Batch B)
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/my_batch
After successfully running Spring Batch A for several times, I ran Spring Batch B and it threw a JobExecutionAlreadyRunningException
.
Example:
org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=2, JobParameters=[{}], Job=[MyBatchName]
During that time, Spring Batch A is no longer running. The exception seems to indicate that the Job Instance ID is already taken by Spring Batch A and cannot be used by Spring Batch B.
Question:
Can a Spring Batch Meta-Data Schema support multiple Spring Batch projects?
They can but you need to make sure things are unique across jobs. Specifically the job name and identifying parameters must be unique. So if JobA and JobB both have the same name, you'll run into collisions.
As suggested by Micheal Minella, the root cause of the collision was due to the combination of JOB_NAME
and JOB_KEY
was not unique.
Definition of JOB_NAME
and JOB_KEY
:
JOB_NAME: Name of the job obtained from the
Job
object. Because it is required to identify the instance, it must not be null.JOB_KEY: A serialization of the
JobParameters
that uniquely identifies separate instances of the same job from one another.
BATCH_JOB_INSTANCE
table creation SQL
CREATE TABLE BATCH_JOB_INSTANCE (
JOB_INSTANCE_ID BIGINT NOT NULL PRIMARY KEY ,
VERSION BIGINT ,
JOB_NAME VARCHAR(100) NOT NULL,
JOB_KEY VARCHAR(32) NOT NULL,
constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY)
) ENGINE=InnoDB;
Since my job name is a constant, I need to make sure the JobParameters
are unique every time I run the batch. As suggested by Mkyong, we can add System.currentTimeMillis()
as a parameter.
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time",System.currentTimeMillis()).toJobParameters();
if (status.isRunning() || status == BatchStatus.STOPPING)
for your job then batch will throw JobExecutionAlreadyRunningException
.Can you please check in meta data table what is the status of the job or job step.
Once if possible you can re-create meta data table and check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With