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 java.sql.PreparedStatement; |
19 | import java.sql.SQLException; |
20 | import java.util.Iterator; |
21 | import java.util.Set; |
22 | |
23 | import org.springframework.batch.item.ClearFailedException; |
24 | import org.springframework.batch.item.ItemWriter; |
25 | import org.springframework.batch.repeat.RepeatContext; |
26 | import org.springframework.beans.factory.InitializingBean; |
27 | import org.springframework.dao.DataAccessException; |
28 | import org.springframework.jdbc.core.JdbcOperations; |
29 | import org.springframework.jdbc.core.PreparedStatementCallback; |
30 | import 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 | */ |
57 | public 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 | } |