17
©Manning Publications Co. Please post comments or corrections to the Author Online forum:
http://www.manning-sandbox.com/forum.jspa?forumID=679
Spring Batch provides strong foundations for handling these specificities, especially in the way it runs
batch processes. This is what we can call the runtime side of Spring Batch features. To show you in
which situations Spring Batch can be very useful for you batch applications, we’re going to see different
runtime scenarios (transaction management, error handling and making batch application scale) and
how Spring Batch handles them.
1.3.1 Handling transactions
In read/write scenarios, Spring Batch is able to manage transactions for you. It means that any
operation on a database will be run inside a transaction. Spring Batch handles the transaction creation
and whether it should be committed (in case of success) or rolled back (in case of failure). This is a very
interesting feature because your code won’t be cluttered with transaction management. Moreover, as
Spring Batch’s transaction management builds on top Spring’s one, it supports native database
transaction, JTA, Hibernate and so on. It means you can switch from transaction management
technology to another without impact on your code.
Another benefit of letting Spring Batch drive transactions for you is that it can do so in a batch-
oriented way, which helps you handle large volumes of data. When doing a bulk of inserts in a database,
you usually don’t want to have only one transaction spanning all of them: in case of error, all the inserts
will be lost, and it forces the database to maintain a large rollback segment. You don’t want either to
have on transaction for each insert: transactions aren’t cheap and doing so can have dramatic impacts
on performances. The best strategy is usually to handle records in… batch! It means that you want to
handle 10 or 100 or 1000 records in one transaction, the number of records being called the batch size.
Doing so is not difficult, but doing it for all your read/write operations becomes quickly cumbersome,
especially when it comes to handling errors (more on this later!). For read/write scenarios, Spring Batch
allows to set a batch size, as shown in the following snippet:
<batch:chunk reader="reader" writer="writer" commit-interval="100" />
By setting the
commit-interval attribute to 100, we tell Spring Batch to ask 100 records to the
reader, open a transaction, send the records to the writer and commit the transaction. Externalizing the
batch size is very interesting as there’s no “best” value for this setting: it depends on many factors like
the writing instructions, the data, or the database. Being able to set the batch size without impact on
the code simplifies tweaking the batch processes, when doing performance tests.
Committing transaction happens on sunny days, but how Spring Batch handles errors? Errors can
have impacts on transaction but also on the whole batch process.
1.3.2 Handling errors
Batch processes handle a lot of data automatically and many things can go wrong: incorrect format in
input files, violation of database constraints, bugs and so forth. Usually, a batch process is not all-or-
nothing operation: you don’t want to stop the whole process because of a tiny error. This is one of the
specificities that can make batch applications really tricky to write: foresee errors and handle them.
Imagining reading records from a flat file before inserting them into a database: if a line doesn’t
respect the format, what should we do? By default, Spring Batch will launch an exception and stop the
process. The following snippet shows the configuration for this default behavior:
<chunk reader="reader" writer="writer" commit-interval="100" />
Licensed to Pedro Rodriguez <prodriguez@opnworks.com>