Using Spring Batch in Grails applications

In one of my grails projects we are using Spring Batch for processing huge amounts of data. We are processing 100000 database entries in one run. When using HibernatePagingItemReader or HibernateCursorItemReader we ran into OutOfMemory errors.

Using the Eclipse Memory Analyzer I quickly found the reason for this behavior. When starting the Spring Batch Job via the JobLauncher from a Grails Quartz Plugin Job it creates a HibernateSession per default. Even if the Spring Batch jobs handle the HibernateSession correct by fetching only a few items and committing small blocks of elements per transaction all the read items are still held by outer HibernateSession of the Quartz Job. This is how the nested session structure looks like while the job is running:

The solution was to set the flag

def sessionRequired = false

in the Quartz Job, so it does not require Grails to provide a session. This allows the Spring Batch jobs to handle their sessions without a surrounding session and resolves the memory issue.