From abd29a0e8ef81bdbdeaf9e0c841e7df4426fe50a Mon Sep 17 00:00:00 2001 From: robokaso Date: Mon, 10 Nov 2008 08:48:32 +0000 Subject: [PATCH] REOPENED - BATCH-893: Remove the HibernateAwareItemWriter? removed the JpaAwareItemWriter as well --- .../item/database/JpaAwareItemWriter.java | 108 -------------- .../database/JpaAwareItemWriterTests.java | 135 ------------------ 2 files changed, 243 deletions(-) delete mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaAwareItemWriter.java delete mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaAwareItemWriterTests.java diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaAwareItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaAwareItemWriter.java deleted file mode 100644 index 7bf50ad1e..000000000 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaAwareItemWriter.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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 java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -import org.springframework.batch.item.ItemWriter; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.dao.DataAccessResourceFailureException; -import org.springframework.orm.jpa.EntityManagerFactoryUtils; -import org.springframework.util.Assert; - -/** - * {@link org.springframework.batch.item.ItemWriter} that is aware of the JPA - * EntityManagerFactory and can take some responsibilities to do with chunk - * boundaries away from a less smart - * {@link org.springframework.batch.item.ItemWriter} (the delegate). A delegate - * is required, and will be used to do the actual writing of the item.
- * - * It is required that {@link #write(List)} is called inside a transaction.
- * - * The reader must be configured with an - * {@link javax.persistence.EntityManagerFactory} that is capable of - * participating in Spring managed transactions. - * - * The writer is thread safe after its properties are set (normal singleton - * behaviour), so it can be used to write in multiple concurrent transactions. - * Note, however, that the set of failed items is stored in a collection - * internally, and this collection is never cleared, so it is not a great idea - * to go on using the writer indefinitely. Normally it would be used for the - * duration of a batch job and then discarded. - * - * @author Dave Syer - * @author Thomas Risberg - * - */ -public class JpaAwareItemWriter implements ItemWriter, InitializingBean { - - private ItemWriter delegate; - - private EntityManagerFactory entityManagerFactory; - - /** - * Public setter for the {@link org.springframework.batch.item.ItemWriter} - * property. - * - * @param delegate the delegate to set - */ - public void setDelegate(ItemWriter delegate) { - this.delegate = delegate; - } - - /** - * Set the EntityManager to be used internally. - * - * @param entityManagerFactory the entityManagerFactory to set - */ - public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { - this.entityManagerFactory = entityManagerFactory; - } - - /** - * Check mandatory properties - there must be a delegate and - * entityManagerFactory. - */ - public void afterPropertiesSet() throws Exception { - Assert.notNull(delegate, "An ItemWriter to be used as a delegate is required."); - Assert.notNull(entityManagerFactory, "An EntityManagerFactory is required"); - } - - /** - * Delegate the writing to the delegate writer and then flush and clear the - * entity manager. - * - * @see org.springframework.batch.item.ItemWriter#write(java.util.List) - */ - public void write(List items) throws Exception { - EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory); - if (entityManager == null) { - throw new DataAccessResourceFailureException("Unable to obtain a transactional EntityManager"); - } - delegate.write(items); - try { - entityManager.flush(); - } - finally { - entityManager.clear(); - } - } - -} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaAwareItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaAwareItemWriterTests.java deleted file mode 100644 index e10b6178f..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaAwareItemWriterTests.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.ArrayList; -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.batch.item.ItemWriter; -import org.springframework.orm.jpa.EntityManagerHolder; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -/** - * @author Thomas Risberg - * - */ -public class JpaAwareItemWriterTests { - - JpaAwareItemWriter writer = new JpaAwareItemWriter(); - - ItemWriter delegate; - - EntityManagerFactory emf; - - final List list = new ArrayList(); - - @Before - @SuppressWarnings( { "unchecked" }) - public void setUp() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - delegate = createMock("delegate", ItemWriter.class); - writer.setDelegate(delegate); - emf = createMock("emf", EntityManagerFactory.class); - writer.setEntityManagerFactory(emf); - } - - @Test - public void testAfterPropertiesSet() throws Exception { - writer = new JpaAwareItemWriter(); - try { - writer.afterPropertiesSet(); - fail("Expected IllegalArgumentException"); - } - catch (IllegalArgumentException e) { - // expected - assertTrue("Wrong message for exception: " + e.getMessage(), e.getMessage().indexOf("delegate") >= 0); - } - writer.setDelegate(delegate); - 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 testWriteAndFlushSunnyDay() throws Exception { - EntityManager em = createMock("em", EntityManager.class); - em.flush(); - em.clear(); - replay(em); - replay(emf); - TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em)); - List items = Arrays.asList(new String[] { "foo", "spam" }); - delegate.write(items); - replay(delegate); - - writer.write(items); - - verify(delegate); - verify(em); - TransactionSynchronizationManager.unbindResource(emf); - } - - @Test - public void testWriteAndFlushWithFailure() throws Exception { - final RuntimeException ex = new RuntimeException("bar"); - EntityManager em = createMock("em", EntityManager.class); - em.flush(); - expectLastCall().andThrow(ex); - em.clear(); - replay(em); - replay(emf); - TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em)); - List items = Arrays.asList(new String[] { "foo", "spam" }); - delegate.write(items); - replay(delegate); - - try { - writer.write(items); - fail("Expected RuntimeException"); - } - catch (RuntimeException e) { - assertEquals("bar", e.getMessage()); - } - - verify(delegate); - verify(em); - TransactionSynchronizationManager.unbindResource(emf); - } - -}