spring-data JPA: manual commit transaction and restart new one
spring-data, transactions
Solution
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
Problem
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).