Java Reference
In-Depth Information
forwarded to the writer to be added to the output. If at any point in the three processors the Customer
fails a check, the executing ItemProcessor can simply return null and arrest processing.
9-7. Better Living through Transactions
You want your reads and writes to be robust. Ideally, they'll use transactions where appropriate and also
correctly react to exceptions.
Transaction capabilities are built on top of the first class support already provided by the core Spring
framework. Where relevant, Spring Batch surfaces the configuration so that you can control it. Within
the context of chunk-oriented processing, it also exposes a lot of control over the frequency of commits,
rollback semantics, and so on.
How It Works
Spring's core framework provides first class support for transactions. You simply wire up a
TransactionManager and give Spring Batch a reference, just as you would in any regular JdbcTemplate
or HibernateTemplate solution. As you build your Spring Batch solutions, you'll be given opportunities
to control how step s behave in a transaction. You've already seen some of the support for transactions
baked right in.
The batch.xml file, used in all these examples, established a DataSource and a
DataSourceTransactionManager bean. The TransactionManager and DataSource were then wired
to the JobRepository , which was in turn wired to the JobLauncher , which you used to launch all
jobs thus far. This enabled all the metadata your job s create to be written to the database in a
transactional way.
You might wonder why there is no explicit mention of the TransactionManager when you configured
the JdbcItemWriter with a reference to the DataSource . The TransactionManager can be specified,
but in your solutions it wasn't required because Spring Batch will, by default, try to pluck the
TransactionManager named transactionManager from the context and use it. If you want to explicitly
configure this, you can specify the transactionManager property on the tasklet element. A simple
TransactionManager for JDBC work might look like this:
<bean id="myCustomTransactionManager"
p:dataSource-ref="dataSource" />
<job job-repository="jobRepository" id="insertIntoDbFromCsvJob">
<step id="step1">
<tasklet transaction-manager="myCustomTransactionManager" >
<!-- ... -->
Search WWH ::

Custom Search