addition of transacted value to JpaPagingItemReader
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
@@ -79,6 +80,7 @@ import org.springframework.util.ClassUtils;
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Dave Syer
|
||||
* @author Will Schipp
|
||||
* @since 2.0
|
||||
*/
|
||||
public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
|
||||
@@ -94,6 +96,8 @@ public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
|
||||
private JpaQueryProvider queryProvider;
|
||||
|
||||
private Map<String, Object> parameterValues;
|
||||
|
||||
private boolean transacted = true;//default value
|
||||
|
||||
public JpaPagingItemReader() {
|
||||
setName(ClassUtils.getShortName(JpaPagingItemReader.class));
|
||||
@@ -125,6 +129,18 @@ public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
|
||||
public void setParameterValues(Map<String, Object> parameterValues) {
|
||||
this.parameterValues = parameterValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* By default (true) the EntityTransaction will be started and committed around the read.
|
||||
* Can be overridden (false) in cases where the JPA implementation doesn't support a
|
||||
* particular transaction. (e.g. Hibernate with a JTA transaction). NOTE: may cause
|
||||
* problems in guaranteeing the object consistency in the EntityManagerFactory.
|
||||
*
|
||||
* @param transacted
|
||||
*/
|
||||
public void setTransacted(boolean transacted) {
|
||||
this.transacted = transacted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
@@ -174,11 +190,15 @@ public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void doReadPage() {
|
||||
|
||||
EntityTransaction tx = entityManager.getTransaction();
|
||||
tx.begin();
|
||||
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
EntityTransaction tx = null;
|
||||
|
||||
if (transacted) {
|
||||
tx = entityManager.getTransaction();
|
||||
tx.begin();
|
||||
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
}//end if
|
||||
|
||||
Query query = createQuery().setFirstResult(getPage() * getPageSize()).setMaxResults(getPageSize());
|
||||
|
||||
@@ -194,9 +214,17 @@ public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
|
||||
else {
|
||||
results.clear();
|
||||
}
|
||||
results.addAll(query.getResultList());
|
||||
|
||||
tx.commit();
|
||||
|
||||
if (!transacted) {
|
||||
List<T> queryResult = query.getResultList();
|
||||
for (T entity : queryResult) {
|
||||
entityManager.detach(entity);
|
||||
results.add(entity);
|
||||
}//end if
|
||||
} else {
|
||||
results.addAll(query.getResultList());
|
||||
tx.commit();
|
||||
}//end if
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user