Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

In Spring-Batch – Reading and Writing XML there was a note on the JobRepository in the src/main/resources/applicationContext.xml. The note clearly states that the Class that was used isn’t ready for prime time. The problem is that this job repository is stored in memory only. This is a problem if a job has to be restarted. As a reference, here is the snippet around the JobRepository

    <!--This repository is only really intended for use in testing and rapid prototyping-->
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"
          p:transactionManager-ref="transactionManager"/>

If that isn’t the real-deal, then what is?

Spring-Batch JobRepositoryFactoryBean

The MapJobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean. The only other class that extends it is JobRepositoryFactoryBean. This uses a database to store the job related information. This is how it is declared.

<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="mysql" />
</bean>
  • dataSource is a normal database connection.
  • transactionManager is also a standard database transaction manager.
  • databaseType indicates the type of database. See org.springframework.batch.support.DatabaseType to determine the correct type. The values can be entered in lower-case.

Spring Batch Schema tables

Another thing you will need to do is add the database-related tables to store the information.

<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
    <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
  </jdbc:initialize-database>

The scripts follow the pattern of schema-drop-{databaseType}.sql and schema-{databaseType}.sql. There are scripts for lots and lots of different databases. The scripts are located in spring-batch-core jar in org/springframework/batch/core.

The following database types are supported and have scripts:

  • db2 – IBM DB2
  • derby – Apache Derby
  • h2 – H2
  • hsqldb – HSQL Database Engine
  • mysql – MySQL
  • oracle – Oracle (10g)
  • postgresql – PostgreSQL
  • sqlf – Pivotal SQLFire
  • sqlite – SQLite
  • sqlserver – Microsoft SQL Server
  • sybase – Sybase

There is your answer.

February 18th, 2016

Posted In: Java, java ninja, Javaninja, Spring Batch

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com