Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch Framework - Auto create Batch Table

I just created a batch job using Spring Batch framework, but I don't have Database privileges to run CREATE SQL. When I try to run the batch job I hit the error while the framework tried to create TABLE_BATCH_INSTANCE. I try to disable the

<jdbc:initialize-database data-source="dataSource" enabled="false">      ... </jdbc:initialize-database> 

But after I tried I still hit the error

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist 

Anyway can disable the SQL, I just want to test my reader writer and processor work properly.

like image 282
Einn Hann Avatar asked Oct 21 '15 02:10

Einn Hann


People also ask

What is Spring Batch metadata tables?

Overview. The Spring Batch Metadata tables closely match the Domain objects that represent them in Java. For example, JobInstance , JobExecution , JobParameters , and StepExecution map to BATCH_JOB_INSTANCE , BATCH_JOB_EXECUTION , BATCH_JOB_EXECUTION_PARAMS , and BATCH_STEP_EXECUTION , respectively.

Does Spring Batch need a database?

Spring Batch by default uses a database to store metadata on the configured batch jobs. In this example, we will run Spring Batch without a database. Instead, an in-memory Map based repository is used.

How do I use multiple datasource in Spring Batch?

With the xml one you have to be explicit in which datasource Spring Batch uses. If you don't declare it explicitly with Java based configuration it will try to detect the datasource to work, which will only work in case a single datasource is detected. YOu could try annotating the one to use for Batch with @Primary .


2 Answers

With Spring Boot 2.0 you probably need this: https://docs.spring.io/spring-boot/docs/2.0.0.M7/reference/htmlsingle/#howto-initialize-a-spring-batch-database

spring.batch.initialize-schema=always 

By default it will only create the tables if you are using an embedded database.

Or

 spring.batch.initialize-schema=never 

To permanently disable it.

like image 151
BitfullByte Avatar answered Sep 22 '22 05:09

BitfullByte


Spring Batch uses the database to save metadata for its recover/retry functionality.

If you can't create tables in the database then you have to disable this behaviour

If you can create the batch metadata tables but not in runtime then you might create them manually

like image 39
Sergio Avatar answered Sep 21 '22 05:09

Sergio