EMMA Coverage Report (generated Tue May 06 07:28:24 PDT 2008)
[all classes][org.springframework.batch.item.database]

COVERAGE SUMMARY FOR SOURCE FILE [BatchSqlUpdateItemWriter.java]

nameclass, %method, %block, %line, %
BatchSqlUpdateItemWriter.java100% (2/2)100% (12/12)99%  (94/95)100% (24.9/25)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BatchSqlUpdateItemWriter100% (1/1)100% (10/10)98%  (63/64)100% (17.9/18)
<static initializer> 100% (1/1)94%  (17/18)94%  (0.9/1)
BatchSqlUpdateItemWriter (): void 100% (1/1)100% (3/3)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (9/9)100% (3/3)
doClear (): void 100% (1/1)100% (1/1)100% (1/1)
doFlush (): void 100% (1/1)100% (18/18)100% (4/4)
doWrite (Object): void 100% (1/1)100% (1/1)100% (1/1)
getResourceKey (): String 100% (1/1)100% (2/2)100% (1/1)
setItemPreparedStatementSetter (ItemPreparedStatementSetter): void 100% (1/1)100% (4/4)100% (2/2)
setJdbcTemplate (JdbcOperations): void 100% (1/1)100% (4/4)100% (2/2)
setSql (String): void 100% (1/1)100% (4/4)100% (2/2)
     
class BatchSqlUpdateItemWriter$1100% (1/1)100% (2/2)100% (31/31)100% (7/7)
BatchSqlUpdateItemWriter$1 (BatchSqlUpdateItemWriter, Set): void 100% (1/1)100% (9/9)100% (1/1)
doInPreparedStatement (PreparedStatement): Object 100% (1/1)100% (22/22)100% (6/6)

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 */
16package org.springframework.batch.item.database;
17 
18import java.sql.PreparedStatement;
19import java.sql.SQLException;
20import java.util.Iterator;
21import java.util.Set;
22 
23import org.springframework.batch.item.ClearFailedException;
24import org.springframework.batch.item.ItemWriter;
25import org.springframework.batch.repeat.RepeatContext;
26import org.springframework.beans.factory.InitializingBean;
27import org.springframework.dao.DataAccessException;
28import org.springframework.jdbc.core.JdbcOperations;
29import org.springframework.jdbc.core.PreparedStatementCallback;
30import org.springframework.util.Assert;
31 
32/**
33 * {@link ItemWriter} that uses the batching features from
34 * {@link PreparedStatement} if available and can take some rudimentary steps to
35 * locate a failure during a flush, and identify the items that failed. When one
36 * of those items is encountered again the batch is flushed aggressively so that
37 * the bad item is eventually identified and can be dealt with in isolation.<br/>
38 * 
39 * The user must provide an SQL query and a special callback
40 * {@link ItemPreparedStatementSetter}, which is responsible for mapping the
41 * item to a PreparedStatement.<br/>
42 * 
43 * It is expected that {@link #write(Object)} is called inside a transaction,
44 * and that {@link #flush()} is then subsequently called before the transaction
45 * commits, or {@link #clear()} before it rolls back.<br/>
46 * 
47 * The writer is thread safe after its properties are set (normal singleton
48 * behaviour), so it can be used to write in multiple concurrent transactions.
49 * Note, however, that the set of failed items is stored in a collection
50 * internally, and this collection is never cleared, so it is not a great idea
51 * to go on using the writer indefinitely. Normally it would be used for the
52 * duration of a batch job and then discarded.
53 * 
54 * @author Dave Syer
55 * 
56 */
57public class BatchSqlUpdateItemWriter extends AbstractTransactionalResourceItemWriter implements InitializingBean {
58 
59        /**
60         * Key for items processed in the current transaction {@link RepeatContext}.
61         */
62        private static final String ITEMS_PROCESSED = BatchSqlUpdateItemWriter.class.getName() + ".ITEMS_PROCESSED";
63 
64        private JdbcOperations jdbcTemplate;
65 
66        private ItemPreparedStatementSetter preparedStatementSetter;
67 
68        private String sql;
69 
70        /**
71         * Public setter for the query string to execute on write. The parameters
72         * should correspond to those known to the
73         * {@link ItemPreparedStatementSetter}.
74         * @param sql the query to set
75         */
76        public void setSql(String sql) {
77                this.sql = sql;
78        }
79 
80        /**
81         * Public setter for the {@link ItemPreparedStatementSetter}.
82         * @param preparedStatementSetter the {@link ItemPreparedStatementSetter} to
83         * set
84         */
85        public void setItemPreparedStatementSetter(ItemPreparedStatementSetter preparedStatementSetter) {
86                this.preparedStatementSetter = preparedStatementSetter;
87        }
88 
89        /**
90         * Public setter for the {@link JdbcOperations}.
91         * @param jdbcTemplate the {@link JdbcOperations} to set
92         */
93        public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
94                this.jdbcTemplate = jdbcTemplate;
95        }
96 
97        /**
98         * Check mandatory properties - there must be a delegate.
99         */
100        public void afterPropertiesSet() throws Exception {
101                Assert.notNull(jdbcTemplate, "BatchSqlUpdateItemWriter requires an data source.");
102                Assert.notNull(preparedStatementSetter, "BatchSqlUpdateItemWriter requires a ItemPreparedStatementSetter");
103        }
104 
105        /**
106         * Create and execute batch prepared statement.
107         */
108        protected void doFlush() {
109                final Set processed = getProcessed();
110                if (!processed.isEmpty()) {
111                        jdbcTemplate.execute(sql, new PreparedStatementCallback() {
112                                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
113                                        for (Iterator iterator = processed.iterator(); iterator.hasNext();) {
114                                                Object item = (Object) iterator.next();
115                                                preparedStatementSetter.setValues(item, ps);
116                                                ps.addBatch();
117                                        }
118                                        return ps.executeBatch();
119                                }
120                        });
121                }
122        }
123 
124        protected String getResourceKey() {
125                return ITEMS_PROCESSED;
126        }
127 
128        /**
129         * No-op.
130         */
131        protected void doWrite(Object output) {
132        }
133        
134        /**
135         * No-op.
136         */
137        protected void doClear() throws ClearFailedException {
138        }
139 
140}

[all classes][org.springframework.batch.item.database]
EMMA 2.0.5312 (C) Vladimir Roubtsov