로컬에서 H2 환경으로 테스트후 개발 환경을 구성해 MySql로 변경하였는데 Job 실행 과정에서
Unabled to commit new sequence value change for BATCH_JOB_SEQ 에러가 발생했다.
현재 JTA를 위해 사용하고있는 Atomikos 라이브러리에서는 Global transaction이 진행중인 경우 commit, rollback을 명시적으로 호출 할수 없도록 되어있는데 Spring Batch에서 기본적으로 사용하고 있는 MySQLMaxValueIncrementer에서 sequence를 체번 하는 과정에서 문제가 발생한다.
해당 Class의 javadoc에서 나온것 처럼 sequence채번시 기본적으로 별도 connection을 생성하기 때문이다.
MySQLMaxValueIncrementer javadoc
공식 문서에서 나온것 처럼 해결을 위해 useNewConnection을 수정하고, SEQ테이블의 타입을 변경하여 정상 작동 확인 하였다.
@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends DefaultBatchConfigurer {
@Override
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource);
factory.setTransactionManager(getTransactionManager());
factory.setIncrementerFactory(new DefaultDataFieldMaxValueIncrementerFactory(dataSource){
@Override
public DataFieldMaxValueIncrementer getIncrementer(String incrementerType, String incrementerName) {
DataFieldMaxValueIncrementer inc = super.getIncrementer(incrementerType, incrementerName);
if(inc instanceof MySQLMaxValueIncrementer) {
((MySQLMaxValueIncrementer) inc).setUseNewConnection(false);
}
return inc;
}
});
factory.afterPropertiesSet();
return factory.getObject();
}
}