diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateItemWriter.java index 94515cc98..97af9773b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateItemWriter.java @@ -29,7 +29,8 @@ import org.springframework.util.Assert; /** * {@link ItemWriter} that uses a Hibernate session to save or update entities * that are not part of the current Hibernate session. It will also flush the - * session at chunk boundaries.
+ * session at chunk boundaries. It will also flush and clear the session at + * chunk boundaries by default (@see ).
*
* * The writer is thread safe after its properties are set (normal singleton @@ -45,6 +46,18 @@ public class HibernateItemWriter implements ItemWriter, InitializingBean { private HibernateOperations hibernateTemplate; + private boolean clearSession = true; + + /** + * Flag to indicate that the session should be cleared and flushed at the + * end of the write (default true). + * + * @param clearSession the flag value to set + */ + public void setClearSession(boolean clearSession) { + this.clearSession = clearSession; + } + /** * Public setter for the {@link HibernateOperations} property. * @@ -80,6 +93,9 @@ public class HibernateItemWriter implements ItemWriter, InitializingBean { public final void write(List items) { doWrite(hibernateTemplate, items); hibernateTemplate.flush(); + if (clearSession) { + hibernateTemplate.clear(); + } } /** diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateItemWriterTests.java index 5d2dc6b94..dff648d84 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateItemWriterTests.java @@ -81,6 +81,7 @@ public class HibernateItemWriterTests { expectLastCall().andReturn(false); ht.saveOrUpdate("bar"); ht.flush(); + ht.clear(); replay(ht); List items = Arrays.asList(new String[] { "foo", "bar" });