diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateAwareItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateAwareItemWriter.java deleted file mode 100644 index d2a9a21ee..000000000 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateAwareItemWriter.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2006-2007 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 org.hibernate.SessionFactory; -import org.springframework.batch.item.ItemWriter; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.orm.hibernate3.HibernateOperations; -import org.springframework.orm.hibernate3.HibernateTemplate; -import org.springframework.util.Assert; - -/** - * {@link ItemWriter} that is aware of the Hibernate session and can take some - * responsibilities to do with chunk boundaries away from a less smart - * {@link ItemWriter} (the delegate). A delegate is required, and will be used - * to do the actual writing of the item.

- * - * 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 - * - */ -public class HibernateAwareItemWriter implements ItemWriter, InitializingBean { - - private ItemWriter delegate; - - private HibernateOperations hibernateTemplate; - - /** - * Public setter for the {@link ItemWriter} property. - * - * @param delegate the delegate to set - */ - public void setDelegate(ItemWriter delegate) { - this.delegate = delegate; - } - - /** - * Public setter for the {@link HibernateOperations} property. - * - * @param hibernateTemplate the hibernateTemplate to set - */ - public void setHibernateTemplate(HibernateOperations hibernateTemplate) { - this.hibernateTemplate = hibernateTemplate; - } - - /** - * Set the Hibernate SessionFactory to be used internally. Will - * automatically create a HibernateTemplate for the given SessionFactory. - * - * @see #setHibernateTemplate - */ - public final void setSessionFactory(SessionFactory sessionFactory) { - this.hibernateTemplate = new HibernateTemplate(sessionFactory); - } - - /** - * Check mandatory properties - there must be a delegate and - * hibernateTemplate. - */ - public void afterPropertiesSet() throws Exception { - Assert.notNull(delegate, "HibernateAwareItemWriter requires an ItemWriter as a delegate."); - Assert.notNull(hibernateTemplate, "HibernateAwareItemWriter requires a HibernateOperations"); - } - - /** - * Delegate the writing and then flush and clear te hibernate session. - * - * @see org.springframework.batch.item.ItemWriter#write(java.util.List) - */ - public void write(List items) throws Exception { - delegate.write(items); - try { - hibernateTemplate.flush(); - } - finally { - // This should happen when the transaction commits anyway, but to be - // sure... - hibernateTemplate.clear(); - } - } - -} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateAwareItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateAwareItemWriterTests.java deleted file mode 100644 index d813f4a83..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateAwareItemWriterTests.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2006-2007 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.ArrayList; -import java.util.Collections; -import java.util.List; - -import junit.framework.TestCase; - -import org.springframework.batch.item.ItemWriter; -import org.springframework.batch.repeat.support.RepeatSynchronizationManager; -import org.springframework.dao.DataAccessException; -import org.springframework.orm.hibernate3.HibernateTemplate; - -/** - * @author Dave Syer - * - */ -public class HibernateAwareItemWriterTests extends TestCase { - - private class HibernateTemplateWrapper extends HibernateTemplate { - public void flush() throws DataAccessException { - list.add("flush"); - } - - public void clear() { - list.add("clear"); - }; - } - - private class StubItemWriter implements ItemWriter { - public void write(List items) { - list.addAll(items); - } - } - - HibernateAwareItemWriter writer = new HibernateAwareItemWriter(); - - final List list = new ArrayList(); - - /* - * (non-Javadoc) - * - * @see junit.framework.TestCase#setUp() - */ - protected void setUp() throws Exception { - writer.setDelegate(new StubItemWriter()); - writer.setHibernateTemplate(new HibernateTemplateWrapper()); - list.clear(); - } - - /* - * (non-Javadoc) - * - * @see junit.framework.TestCase#tearDown() - */ - protected void tearDown() throws Exception { - RepeatSynchronizationManager.clear(); - } - - /** - * Test method for - * {@link org.springframework.batch.item.database.HibernateAwareItemWriter#afterPropertiesSet()} - * . - * - * @throws Exception - */ - public void testAfterPropertiesSet() throws Exception { - writer = new HibernateAwareItemWriter(); - try { - writer.afterPropertiesSet(); - fail("Expected IllegalArgumentException"); - } - catch (IllegalArgumentException e) { - // expected - assertTrue("Wrong message for exception: " + e.getMessage(), e.getMessage().indexOf("delegate") >= 0); - } - } - - /** - * Test method for - * {@link org.springframework.batch.item.database.HibernateAwareItemWriter#afterPropertiesSet()} - * . - * - * @throws Exception - */ - public void testAfterPropertiesSetWithDelegate() throws Exception { - writer.afterPropertiesSet(); - } - - public void testWriteAndFlushSunnyDay() throws Exception { - writer.write(Collections.singletonList("foo")); - assertEquals(3, list.size()); - assertTrue(list.contains("foo")); - assertTrue(list.contains("flush")); - assertTrue(list.contains("clear")); - } - - public void testWriteAndFlushWithFailure() throws Exception { - final RuntimeException ex = new RuntimeException("bar"); - writer.setHibernateTemplate(new HibernateTemplateWrapper() { - public void flush() throws DataAccessException { - throw ex; - } - }); - try { - writer.write(Collections.singletonList("foo")); - fail("Expected RuntimeException"); - } - catch (RuntimeException e) { - assertEquals("bar", e.getMessage()); - } - assertEquals(2, list.size()); - assertTrue(list.contains("foo")); - writer.setHibernateTemplate(new HibernateTemplateWrapper() { - public void flush() throws DataAccessException { - list.add("flush"); - } - }); - writer.write(Collections.singletonList("foo")); - System.err.println(list); - assertTrue(list.contains("flush")); - assertTrue(list.contains("clear")); - } - -}