Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch - Can two different batches share the same Meta-Data datasource?

I have two different Spring Batch projects, both are configured to have the same datasource (same DB schema) for the Meta-Data tables:

  1. application.properties (Spring Batch A)

    spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/my_batch
    
  2. 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?

like image 267
wltheng Avatar asked Jun 23 '17 17:06

wltheng


3 Answers

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.

like image 120
Michael Minella Avatar answered Nov 16 '22 04:11

Michael Minella


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();
like image 38
wltheng Avatar answered Nov 16 '22 04:11

wltheng


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.

like image 1
gati sahu Avatar answered Nov 16 '22 02:11

gati sahu