Duplicate Spring Batch Job Instance

后端 未结 1 1182
北恋
北恋 2021-01-12 12:37

I have a small sample Spring Batch application that when started for the first time will work fine but whenever I shut the application down & restart the jar I always ge

相关标签:
1条回答
  • 2021-01-12 12:50

    Found the issue. When using the @EnableAutoConfiguration annotation from Spring Boot it will invoke the org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration class which will initialize the database from the 'schema-mysql.sql' file.

    Inside the schema-mysql.sql file is some code to reset the sequence id's for the batch meta tables which is why I was getting a duplicate key error:

    INSERT INTO BATCH_STEP_EXECUTION_SEQ values(0);
    INSERT INTO BATCH_JOB_EXECUTION_SEQ values(0);
    INSERT INTO BATCH_JOB_SEQ values(0);
    

    The fix was to build the Spring Batch tables separately & then change the @EnableAutoConfiguration annotation to:

    @EnableAutoConfiguration(exclude={BatchAutoConfiguration.class})
    

    so that when the application starts up it will not try to initialize the Spring Batch tables.

    To exclude or customize Spring Boot's auto-configuration for other components you can find some docs here: http://projects.spring.io/spring-boot/docs/spring-boot-autoconfigure/README.html

    The BatchAutoConfiguration code: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

    0 讨论(0)
提交回复
热议问题