BATCH-1635: remobe clear() from Hibernate/JPA writers

This commit is contained in:
Dave Syer
2011-02-03 17:43:08 +00:00
parent 0093836593
commit 1e1edb8ff9
4 changed files with 15 additions and 26 deletions

View File

@@ -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.<br/><br/>
* that are not part of the current Hibernate session. It will also flush and
* clear the session at chunk boundaries.<br/>
* <br/>
*
* 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<T> implements ItemWriter<T>, 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<? extends T> 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

View File

@@ -70,7 +70,7 @@ public class JpaItemWriter<T> implements ItemWriter<T>, 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<T> implements ItemWriter<T>, 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<T> implements ItemWriter<T>, InitializingBean {
logger.debug((items.size() - mergeCount) + " entities found in persistence context.");
}
}
}
}