7

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
wltheng
  • 750
  • 1
  • 11
  • 26

3 Answers3

5

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.

Michael Minella
  • 20,843
  • 4
  • 55
  • 67
4

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();
wltheng
  • 750
  • 1
  • 11
  • 26
  • On the side note, as I am not using MVC, in order to add `JobParameters`, I have to manually launch the job from my Application class, and set `spring.batch.job.enabled=false` to avoid the job running twice. Reference: https://stackoverflow.com/a/25185150/5366727 – wltheng Jun 27 '17 at 08:51
1

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.

gati sahu
  • 2,576
  • 2
  • 10
  • 16