BATCH-2462 Allow JpaItemWriter to support persist rather than merge for improved performance.
This commit is contained in:
committed by
Mahmoud Ben Hassine
parent
4d3090ac02
commit
229fed4e24
@@ -50,6 +50,7 @@ public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
protected static final Log logger = LogFactory.getLog(JpaItemWriter.class);
|
||||
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
private boolean usePersist = false;
|
||||
|
||||
/**
|
||||
* Set the EntityManager to be used internally.
|
||||
@@ -59,6 +60,15 @@ public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
|
||||
this.entityManagerFactory = entityManagerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether the EntityManager should perform a persist instead of a merge.
|
||||
*
|
||||
* @param usePersist whether to use persist instead of merge.
|
||||
*/
|
||||
public void setUsePersist(boolean usePersist) {
|
||||
this.usePersist = usePersist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check mandatory properties - there must be an entityManagerFactory.
|
||||
@@ -98,16 +108,21 @@ public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
}
|
||||
|
||||
if (!items.isEmpty()) {
|
||||
long mergeCount = 0;
|
||||
long addedToContextCount = 0;
|
||||
for (T item : items) {
|
||||
if (!entityManager.contains(item)) {
|
||||
entityManager.merge(item);
|
||||
mergeCount++;
|
||||
if(usePersist) {
|
||||
entityManager.persist(item);
|
||||
}
|
||||
else {
|
||||
entityManager.merge(item);
|
||||
}
|
||||
addedToContextCount++;
|
||||
}
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(mergeCount + " entities merged.");
|
||||
logger.debug((items.size() - mergeCount) + " entities found in persistence context.");
|
||||
logger.debug(addedToContextCount + " entities " + (usePersist ? " persisted." : "merged."));
|
||||
logger.debug((items.size() - addedToContextCount) + " entities found in persistence context.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2006-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.orm.jpa.EntityManagerHolder;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
/**
|
||||
* @author Chris Cranford
|
||||
*
|
||||
*/
|
||||
public class JpaItemWriterPersistTests {
|
||||
|
||||
EntityManagerFactory emf;
|
||||
|
||||
JpaItemWriter<Object> writer;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
writer = new JpaItemWriter<Object>();
|
||||
writer.setUsePersist(true);
|
||||
emf = mock(EntityManagerFactory.class,"emf");
|
||||
writer.setEntityManagerFactory(emf);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
writer = new JpaItemWriter<Object>();
|
||||
try {
|
||||
writer.afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
assertTrue("Wrong message for exception: " + e.getMessage(),
|
||||
e.getMessage().indexOf("EntityManagerFactory") >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersist() throws Exception {
|
||||
EntityManager em = mock(EntityManager.class, "em");
|
||||
TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
|
||||
List<String> items = Arrays.asList(new String[] { "persist1", "persist2" });
|
||||
writer.write(items);
|
||||
TransactionSynchronizationManager.unbindResource(emf);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user