2

My spring batch working fine with in-memory data base hsqldb by using delow configuration.

@Bean
public ResourcelessTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public JobRepository jobRepository(ResourcelessTransactionManager transactionManager) throws Exception {
    MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean = new MapJobRepositoryFactoryBean(transactionManager);
    mapJobRepositoryFactoryBean.setTransactionManager(transactionManager);
    return mapJobRepositoryFactoryBean.getObject();
}

@Bean
public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
    SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
    simpleJobLauncher.setJobRepository(jobRepository);
    return simpleJobLauncher;
}

But when I connecting with DB2 data base batch it giving me below error.

    class path resource [org/springframework/batch/core/schema-db2zos.sql] cannot be opened because it does not exist 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchDatabaseInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [org/springframework/batch/core/schema-db2zos.sql]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/batch/core/schema-db2zos.sql] cannot be opened because it does not exist 

After adding properties (spring.batch.schema=classpath:/org/springframework/batch/core/schema-db2.sql) mention by frenzykryger getting below exception.

Failed to execute SQL script statement #1 of resource class path resource [org/springframework/batch/core/schema-db2.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) ) 
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-551, SQLSTATE=42501, SQLERRMC=AK51602;CREATE TABLE;DSNDB04, DRIVER=4.19.26
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
xception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.deere.sample.main.Application.main(Application.java:22)
Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? order by JOB_INSTANCE_ID desc]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)

After adding properties (spring.batch.initializer.enabled=false) getting below exception.

Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.deere.sample.main.Application.main(Application.java:22)
Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? order by JOB_INSTANCE_ID desc]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)

1 Answers1

0

There is no db2zos schema in spring-batch-core, but there is schema for db2. Try to explicitly specify in properties:

spring.batch.schema=classpath:/org/springframework/batch/core/schema-db2.sql

and see if it helps.

Update

To completely disable schema initialization just use following property:

spring.batch.initializer.enabled=false

For more reference just see org.springframework.boot.autoconfigure.batch.BatchProperties class. It's fields are mapped to spring.batch.* properties.

In such case schema should be created by some external means or you need to use org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean to not persist any spring-batch state in database.

featuredpeow
  • 2,061
  • 1
  • 19
  • 18
  • Even after specifying properties still getting same issue. – Abdul Gaffar Khan Aug 30 '16 at 12:27
  • Sorry, I gave you incorrect properties (they don't work with spring boot starter batch). I updated the answer, this property should work. – featuredpeow Aug 30 '16 at 14:16
  • Thanks for the reply by adding above properties batch try to create Meta-data tables due to which it failing giving failed to execute SQL Script error.My requirement is not to create data Meta data tables of batch that's why using ResourcelessTransactionManager. I have updated my question with error which I am getting after putting above property file. – Abdul Gaffar Khan Aug 30 '16 at 15:26
  • Added info on how disable schema initialization – featuredpeow Aug 30 '16 at 15:36
  • Thanks Frenzy for quick response,I am using MapJobRepositoryFactoryBean but then also it persisting in DB.When I use spring.batch.initializer.enabled=false, then I am getting PreparedStatementCallback; bad SQL grammar because batchjob firing select statement on Metadata table which is not created.Updated Exception in Question. – Abdul Gaffar Khan Aug 30 '16 at 16:16