| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.item.database; |
| 17 | |
| 18 | import org.hibernate.SessionFactory; |
| 19 | import org.springframework.batch.item.ClearFailedException; |
| 20 | import org.springframework.batch.item.ItemWriter; |
| 21 | import org.springframework.batch.repeat.RepeatContext; |
| 22 | import org.springframework.beans.factory.InitializingBean; |
| 23 | import org.springframework.orm.hibernate3.HibernateOperations; |
| 24 | import org.springframework.orm.hibernate3.HibernateTemplate; |
| 25 | import org.springframework.util.Assert; |
| 26 | |
| 27 | /** |
| 28 | * {@link ItemWriter} that is aware of the Hibernate session and can take some |
| 29 | * responsibilities to do with chunk boundaries away from a less smart |
| 30 | * {@link ItemWriter} (the delegate). A delegate is required, and will be used |
| 31 | * to do the actual writing of the item.<br/> |
| 32 | * |
| 33 | * It is expected that {@link #write(Object)} is called inside a transaction, |
| 34 | * and that {@link #flush()} is then subsequently called before the transaction |
| 35 | * commits, or {@link #clear()} before it rolls back.<br/> |
| 36 | * |
| 37 | * The writer is thread safe after its properties are set (normal singleton |
| 38 | * behaviour), so it can be used to write in multiple concurrent transactions. |
| 39 | * Note, however, that the set of failed items is stored in a collection |
| 40 | * internally, and this collection is never cleared, so it is not a great idea |
| 41 | * to go on using the writer indefinitely. Normally it would be used for the |
| 42 | * duration of a batch job and then discarded. |
| 43 | * |
| 44 | * @author Dave Syer |
| 45 | * |
| 46 | */ |
| 47 | public class HibernateAwareItemWriter extends AbstractTransactionalResourceItemWriter implements InitializingBean { |
| 48 | |
| 49 | /** |
| 50 | * Key for items processed in the current transaction {@link RepeatContext}. |
| 51 | */ |
| 52 | private static final String ITEMS_PROCESSED = HibernateAwareItemWriter.class.getName() + ".ITEMS_PROCESSED"; |
| 53 | |
| 54 | private ItemWriter delegate; |
| 55 | |
| 56 | private HibernateOperations hibernateTemplate; |
| 57 | |
| 58 | /** |
| 59 | * Public setter for the {@link ItemWriter} property. |
| 60 | * |
| 61 | * @param delegate the delegate to set |
| 62 | */ |
| 63 | public void setDelegate(ItemWriter delegate) { |
| 64 | this.delegate = delegate; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Public setter for the {@link HibernateOperations} property. |
| 69 | * |
| 70 | * @param hibernateTemplate the hibernateTemplate to set |
| 71 | */ |
| 72 | public void setHibernateTemplate(HibernateOperations hibernateTemplate) { |
| 73 | this.hibernateTemplate = hibernateTemplate; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set the Hibernate SessionFactory to be used internally. Will |
| 78 | * automatically create a HibernateTemplate for the given SessionFactory. |
| 79 | * |
| 80 | * @see #setHibernateTemplate |
| 81 | */ |
| 82 | public final void setSessionFactory(SessionFactory sessionFactory) { |
| 83 | this.hibernateTemplate = new HibernateTemplate(sessionFactory); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check mandatory properties - there must be a delegate and hibernateTemplate. |
| 88 | */ |
| 89 | public void afterPropertiesSet() throws Exception { |
| 90 | Assert.notNull(delegate, "HibernateAwareItemWriter requires an ItemWriter as a delegate."); |
| 91 | Assert.notNull(hibernateTemplate, "HibernateAwareItemWriter requires a HibernateOperations"); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Delegate to subclass and flush the hibernate session. |
| 96 | */ |
| 97 | protected void doFlush() { |
| 98 | delegate.flush(); |
| 99 | hibernateTemplate.flush(); |
| 100 | // This should happen when the transaction commits anyway, but to be |
| 101 | // sure... |
| 102 | hibernateTemplate.clear(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Call the delegate clear() method, and then clear the hibernate session. |
| 107 | */ |
| 108 | protected void doClear() throws ClearFailedException { |
| 109 | delegate.clear(); |
| 110 | hibernateTemplate.clear(); |
| 111 | } |
| 112 | |
| 113 | protected String getResourceKey() { |
| 114 | return ITEMS_PROCESSED; |
| 115 | } |
| 116 | |
| 117 | protected void doWrite(Object output) throws Exception { |
| 118 | delegate.write(output); |
| 119 | } |
| 120 | |
| 121 | } |