Java Reference
In-Depth Information
Transaction Configuration
The use of the job repository is based on transactions. The repository is updated when each chunk of
processing completes, which triggers the end of a transaction. You've seen that there are two ways to
configure the job repository, one with the regular Spring bean tag and one using the Spring Batch
namespace's job-repository tag. How transactions are configured depends on which of these options
you choose.
When you use the job-repository tag from the Spring Batch namespace, Spring Batch uses Spring's
AOP features to wrap the repository with a transaction. The only thing to configure when using this
approach is the transaction isolation level for the createJobExecution method of the job repository
interface. The intent of this configuration is to prevent multiple executions of a JobInstance at the same
time. To address this issue, Spring Batch sets the transaction's isolation level at its most aggressive value,
SERIALIZABLE , by default. However, your environment may not require a level this aggressive, so Spring
Batch lets you configure the transaction level for the createJobExecution method with the isolation-
level-for-create attribute of the job-repository tag. Listing 5-5 shows how to lower the isolation level
when using the job-repository tag.
Listing 5-5. Setting the Create Transaction Level
<job-repository id="jobRepository" transaction-manager="transactionManager"
data-source="dataSource" isolation-level-for-create="READ_COMMITTED"/>
If you configure your job repository using the Spring bean tag as you did with the in-memory option,
the framework doesn't handle any of the transactions for you. In this case, you need to configure
transactional advice by hand, as shown in Listing 5-6. Here you use Spring's AOP namespace to
configure the transaction advice and apply it to all the methods in the job repository interface.
Listing 5-6. Configuring Job Repository Transactions by Hand
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<beans:property name="driverClassName" value="${batch.jdbc.driver}" />
<beans:property name="url" value="${batch.jdbc.url}" />
<beans:property name="username" value="${batch.jdbc.user}" />
<beans:property name="password" value="${batch.jdbc.password}" />
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"/>
<aop:config>
<aop:advisor
pointcut="execution(*org.springframework.batch.core.repository..*Repository+.*(..))"/>
<advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
 
Search WWH ::




Custom Search