batch processes will need to be able to handle input from databases. In this section, you will look at
some of the facilities that Spring Batch provide out of the box to handle reading input data from a
database including JDBC, Hibernate, and JPA.
In the Java world, database connectivity begins with JDBC. We all go through the pain of writing the
JDBC connection code when we learn it, then quickly forget those lines when we realize that most
frameworks handle things like connections for us. One of the Spring framework's strengths is
encapsulating the pain points of things like JDBC in ways that allow developers to concentrate only on
the business-specific details.
In this tradition, the developers of the Spring Batch framework have extended the Spring
framework's JDBC functionality with the features that are needed in the batch world. But what are those
features and how has Spring Batch addressed them?
When working with batch processes, the need to process large amounts of data is common. If you
have a query that returns millions of records, you probably don't want all of that data loaded into
memory at once. However, if you use Spring's JdbcTemplate, that is exactly what you would get. The
JdbcTemplate loops through the entire ResultSet, mapping every row to the required domain object in
Instead, Spring Batch provides two different methods for loading records one at a time as they are
processed: a cursor and paging. A cursor is actually the default functionality of the JDBC ResultSet. When
a ResultSet is opened, every time the next() method is called a record from the database is returned.
This allows records to be streamed from the database on demand, which is the behavior that you need
for a cursor.
Paging, on the other hand, takes a bit more work. The concept of paging is that you retrieve records
from the database in chunks called pages. As you read through each page, a new page is read from the
database. Figure 7-5 shows the difference between the two approaches.
Figure 7-5. Cursor vs. paging
As you can see in Figure 7-5, the first read in the cursor returns a single record and advances the
record you point at to the next record, streaming a single record at a time, whereas in the pagination
approach, you receive 10 records from the database at a time. You will look at both approaches (cursor
implementations as well as paging) for each of the database technologies you will look at. Let's start with
JDBC Cursor Processing
For this example, you'll be using a Customer table. Using the same fields you have been working with up
to now, you will create a database table to hold the data. Figure 7-6 shows the database model for the
new Customer table.