Spring Book – Chapter 21 – Spring Batch


Due to various factors like scalability, transaction handling capability, performance etc. using database in an enterprise application has become de-facto standard for any modern day enterprise application. Spring Batch provide out of the box implementations to handle reading input data from a database including JDBC, Hibernate, and JPA. We will now see each of them in the following sections.

A common use case in any enterprise application is to process large amount of data read from a database. Typically if you execute a query which fetches millions of record, it won’t be a nice idea to load all these records in memory and then handle it according to business requirement. Spring Batch provides two different methods for loading records one at a time as they are processed, pictorially represented as shown in Figure 23-13 below:

  • Cursor – After the ResultSet is opened, every time the next() method is called a record from the database is returned. This makes streaming of one record at a time from the database to the batch processor. The various cursor based item readers provided by Spring Batch are:
    • JdbcCursorItemReader
    • HibernateCursorItemReader
    • StoredProcedureItemReader
    • Paging – In the concept of paging, you retrieve a set of records from the database called pages. This makes streaming of a set of records of page from the database to the batch processor. The various paging based item readers provided by Spring Batch are:
      • JdbcPagingItemReader
      • JpaPagingItemReader
      • HibernatePagingItemReader
      • IbatisPagingItemReader

 Figure 23-13. Working of a Paging and Cursor

Figure 23-13. Working of a Paging and Cursor


Spring Batch provides two JDBC item reader classes namely:

  • org.springframework.batch.item.database.JdbcCursorItemReader – It’s a simple item reader implementation that opens a JDBC cursor and continually retrieves the next row in the ResultSet. By default the cursor will be opened using a separate connection which means that it will not participate in any transactions created as part of the step processing.
  • org.springframework.batch.item.database.JdbcPagingItemReader – It’s an  for reading database records using JDBC in a paging fashion. The performance of the paging depends on the database specific features available to limit the number of returned rows. Setting a fairly large page size and using a commit interval that matches the page size should provide better performance.

To configure the JdbcCursorItemReader, you provide a minimum of three dependencies: a datasource, the query you want to run, and your RowMapper implementation as shown in Listing 23-11 below.

Listing 23-11. Configuring a sample JdbcCursorItemReader

To configure the JdbcPagingItemReader, you need to have four dependencies: a datasource, the PagingQueryProvider implementation, your RowMapper implementation, and the size of your page as shown in Listing 23-12 below.

Listing 23-12. Configuring a sample JdbcPagingItemReader

The class org.springframework.batch.item.database.JdbcBatchItemWriter is an ItemWriter that uses the batching features from SimpleJdbcTemplate to execute a batch of statements for all items provided. The user must provide an SQL query and a special callback. SQL query can use either named parameters or the traditional ‘?’ placeholders. Listing 23-13 below shows configuring JdbcBatchItemWriter in the Spring configuration file.

Listing 23-13. Configuring a sample JdbcBatchItemWriter

Page Visitors: 25402

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 21 – Spring Batch

  1. Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?

  2. Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.


Leave a Reply

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