From 1e1edb8ff907d3f934dea4340bf3ca9fef3d57ba Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 3 Feb 2011 17:43:08 +0000 Subject: [PATCH] BATCH-1635: remobe clear() from Hibernate/JPA writers --- .../item/database/HibernateItemWriter.java | 23 ++++++++----------- .../batch/item/database/JpaItemWriter.java | 16 +++++-------- .../database/HibernateItemWriterTests.java | 1 - .../item/database/JpaItemWriterTests.java | 1 - 4 files changed, 15 insertions(+), 26 deletions(-) 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 9465117f8..0186237ee 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 @@ -28,8 +28,9 @@ 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 - * and clear the session at chunk boundaries.

+ * that are not part of the current Hibernate session. It will also flush and + * clear the session at chunk boundaries.
+ *
* * The writer is thread safe after its properties are set (normal singleton * behavior), so it can be used to write in multiple concurrent transactions. @@ -71,25 +72,19 @@ public class HibernateItemWriter implements ItemWriter, InitializingBean { } /** - * Save or update any entities not in the current hibernate session and then flush and - * clear the hibernate session. + * Save or update any entities not in the current hibernate session and then + * flush the hibernate session. * * @see org.springframework.batch.item.ItemWriter#write(java.util.List) */ public final void write(List items) { doWrite(hibernateTemplate, items); - try { - hibernateTemplate.flush(); - } - finally { - // This should happen when the transaction commits anyway, but to be - // sure... - hibernateTemplate.clear(); - } + hibernateTemplate.flush(); } - + /** - * Do perform the actual write operation. This can be overridden in a subclass if necessary. + * Do perform the actual write operation. This can be overridden in a + * subclass if necessary. * * @param hibernateTemplate the HibernateTemplate to use for the operation * @param items the list of items to use for the write diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java index 949056b48..e3e72afeb 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java @@ -70,7 +70,7 @@ public class JpaItemWriter implements ItemWriter, InitializingBean { /** * Merge all provided items that aren't already in the persistence context - * and then flush and clear the entity manager. + * and then flush the entity manager. * * @see org.springframework.batch.item.ItemWriter#write(java.util.List) */ @@ -80,16 +80,12 @@ public class JpaItemWriter implements ItemWriter, InitializingBean { throw new DataAccessResourceFailureException("Unable to obtain a transactional EntityManager"); } doWrite(entityManager, items); - try { - entityManager.flush(); - } - finally { - entityManager.clear(); - } + entityManager.flush(); } - + /** - * Do perform the actual write operation. This can be overridden in a subclass if necessary. + * Do perform the actual write operation. This can be overridden in a + * subclass if necessary. * * @param entityManager the EntityManager to use for the operation * @param items the list of items to use for the write @@ -113,7 +109,7 @@ public class JpaItemWriter implements ItemWriter, InitializingBean { logger.debug((items.size() - mergeCount) + " entities found in persistence context."); } } - + } } 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 dff648d84..5d2dc6b94 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,7 +81,6 @@ public class HibernateItemWriterTests { expectLastCall().andReturn(false); ht.saveOrUpdate("bar"); ht.flush(); - ht.clear(); replay(ht); List items = Arrays.asList(new String[] { "foo", "bar" }); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java index c70cbbbfc..8ea3ef7c5 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java @@ -79,7 +79,6 @@ public class JpaItemWriterTests { em.merge("bar"); expectLastCall().andReturn("bar"); em.flush(); - em.clear(); replay(em); replay(emf); TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));