Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring batch trying to create persistant Batch Job Respository again

This seems to be a stupid issue. I am trying to configure an Oracle10g database for Spring Batch Job Repository (Spring Batch 2.1.7), I was able to have the tables created using the script available at org/springframework/batch/core/schema-oracle10g.sql in core. I also set the property batch.data.source.init to false.

On a clean database my batch program runs fine, successfully creating all the Batch tables/Sequences and populating them with the details of the batch results. However, when I run it again Spring Batch tries to create these tables again and throws "ORA-00955: name is already used by an existing object" exception. What am I doing wrong?

# For Oracle
batch.jdbc.driver=oracle.jdbc.driver.OracleDriver
batch.jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
batch.jdbc.user=****
batch.jdbc.password=****
batch.schema=****
batch.schema.script=classpath:/org/springframework/batch/core/schema-oracle10g.sql
batch.drop.script=classpath:/org/springframework/batch/core/schema-drop-oracle10g.sql
batch.jdbc.testWhileIdle=true
batch.data.source.init=false

Following is my context file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:property-placeholder location="classpath:batch.properties" />

    <context:component-scan base-package="com.myco.mypack" />

    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="${batch.schema.script}" />
    </jdbc:initialize-database>

    <import resource="classpath:/META-INF/spring/module-context.xml" />

    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository"/>
    </bean>

    <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
        <property name="dataSource" ref="dataSource" />   
    </bean>

    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
         <property name="dataSource" ref="dataSource" />
         <property name="transactionManager" ref="transactionManager"/>
         <property name="databaseType" value="oracle" />
         <property name="tablePrefix" value="BATCH_"/>
         <property name="isolationLevelForCreate" value="ISOLATION_DEFAULT"/>
    </bean>  

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${batch.jdbc.driver}" />
        <property name="url" value="${batch.jdbc.url}" />
        <property name="username" value="${batch.jdbc.user}" />
        <property name="password" value="${batch.jdbc.password}" />
    </bean>
</beans>
like image 232
Dchucks Avatar asked Apr 05 '12 08:04

Dchucks


People also ask

What is jobrepository in Spring Batch?

As stated earlier in this article, JobRepository in Spring batch takes care of all the CRUD (create, read, update, and delete) operations and ensures persistence. It does this for JobLauncher, Job, and Step.

How to run a Spring Batch job from the command line?

To run a Spring Batch Job, we require two things, the Job and a JobLauncher. It can contain the Job and JobLauncher in the same context or they can be part of different contexts. If we launch a Job from the command line, we will have a new JVM for each Job and each Job will have its own JobLauncher.

What is Spring Batch?

Introduction Spring Batch is a widely used open-source batch processing framework. It incorporates many of the properties that Spring offers. Besides, it exposes a lot of features like easy job configurations, transaction management, logging, job-scheduling to name a few.

What is @enablebatchprocessing in Spring Boot?

There are two components for the java based configuration: the @EnableBatchProcessing annotation and two builders. The @EnableBatchProcessing works similarly to the other @Enable* annotations in the Spring family. In this case, @EnableBatchProcessing provides a base configuration for building batch jobs.


2 Answers

i do not see the use of batch.data.source.init like this:

<jdbc:initialize-database data-source="dataSource" 
                          enabled="${batch.data.source.init}">
    <jdbc:script location="${batch.schema.script}" />
</jdbc:initialize-database>
like image 168
Michael Pralow Avatar answered Oct 13 '22 23:10

Michael Pralow


It's been a while but i think for people searching for this problem it will help to know that you can disable creating the databases by using

spring.batch.initializer.enabled=false

in application.properties as given in the documentation: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

like image 21
Xtroce Avatar answered Oct 14 '22 00:10

Xtroce