Take the 2-minute tour ×

I have a method for importing data. In case the import is large it can not run within a single transaction without potentially causing an OutOfMemoryError due to huge transaction statement cache.

What I want is to manually commit after n records in statement cache.

How can I achieve this? (preferably within an @Transactional method).

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Use EntityManager.flush() and EntityManager.clear() on every Nth iteration, so that the session gets synchronized to the database, and the chache cleared to prevent the OOM.

If you use Hibernate, you could also set the hibernate.jdbc.batch_size to an appropriate value to have batching on JDBC level.

If you also want to commit after a batch and insist on a @Transactional method, then refactor your code so that the @Transactional method gets a batch of N records from the input source, and call that method from an external loop. Otherwise you can use Spring's TransactionTemplate to programmatically controll transactions.

This might be useful: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch15.html

share|improve this answer
thanks, great. flush() and clear() + batch_size did the trick. Massive performance increase. canceled previous tries after 10 min while less than half of the work was done. now it completed in 2.5 min. –  beginner_ Feb 18 '13 at 13:08

Your Answer


