Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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;
 
 

org.springframework.batch.item.ItemWriter that uses the batching features from org.springframework.jdbc.core.simple.SimpleJdbcTemplate to execute a batch of statements for all items provided.

The user must provide an SQL query and a special callback in the for of either ItemPreparedStatementSetter, or a ItemSqlParameterSourceProvider. You can use either named parameters or the traditional '?' placeholders. If you use the named parameter support then you should provide a ItemSqlParameterSourceProvider, otherwise you should provide a ItemPreparedStatementSetter. This callback would be responsible for mapping the item to the parameters needed to execute the SQL statement.
It is expected that write(java.util.List) is called inside a transaction.
The writer is thread safe after its properties are set (normal singleton behavior), so it can be used to write in multiple concurrent transactions.

Author(s):
Dave Syer
Thomas Risberg
Since:
2.0
 
 public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
 
 	protected static final Log logger = LogFactory.getLog(JdbcBatchItemWriter.class);
 
 
 
 
 	private String sql;
 
 	private boolean assertUpdates = true;
 
 	private int parameterCount;
 
 	private boolean usingNamedParameters;

Public setter for the flag that determines whether an assertion is made that all items cause at least one row to be updated.

Parameters:
assertUpdates the flag to set. Defaults to true;
 
 	public void setAssertUpdates(boolean assertUpdates) {
 		this. = assertUpdates;
 	}

Public setter for the query string to execute on write. The parameters should correspond to those known to the ItemPreparedStatementSetter.

Parameters:
sql the query to set
 
 	public void setSql(String sql) {
 		this. = sql;
 	}

Public setter for the ItemPreparedStatementSetter.

Parameters:
preparedStatementSetter the ItemPreparedStatementSetter to set. This is required when using traditional '?' placeholders for the SQL statement.
	public void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter) {
		this. = preparedStatementSetter;
	}

Public setter for the ItemSqlParameterSourceProvider.

Parameters:
itemSqlParameterSourceProvider the ItemSqlParameterSourceProvider to set. This is required when using named parameters for the SQL statement.
	public void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider) {
		this. = itemSqlParameterSourceProvider;
	}

Public setter for the data source for injection purposes.

Parameters:
dataSource
	public void setDataSource(DataSource dataSource) {
		if ( == null) {
			this. = new SimpleJdbcTemplate(dataSource);
		}
	}

Public setter for the org.springframework.jdbc.core.JdbcOperations.

Parameters:
simpleJdbcTemplate the org.springframework.jdbc.core.JdbcOperations to set
	public void setSimpleJdbcTemplate(SimpleJdbcOperations simpleJdbcTemplate) {
		this. = simpleJdbcTemplate;
	}

Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a parameter source.
	public void afterPropertiesSet() {
		Assert.notNull("A DataSource or a SimpleJdbcTemplate is required.");
		Assert.notNull("An SQL statement is required.");
		List<StringnamedParameters = new ArrayList<String>();
		 = JdbcParameterUtils.countParameterPlaceholders(namedParameters);
		if (namedParameters.size() > 0) {
			if ( != namedParameters.size()) {
				throw new InvalidDataAccessApiUsageException("You can't use both named parameters and classic \"?\" placeholders: " + );
			}
		}
			Assert.notNull("Using SQL statement with named parameters requires an ItemSqlParameterSourceProvider");
		}
		else {
			Assert.notNull("Using SQL statement with '?' placeholders requires an ItemPreparedStatementSetter");
		}
	}
	/* (non-Javadoc)
	 * @see org.springframework.batch.item.ItemWriter#write(java.util.List)
	 */
	public void write(final List<? extends T> itemsthrows Exception {
		if (!items.isEmpty()) {
				.debug("Executing batch with " + items.size() + " items.");
			}
			int[] updateCounts = null;
				SqlParameterSource[] batchArgs = new SqlParameterSource[items.size()];
				int i = 0;
				for (T item : items) {
				}
				updateCounts = .batchUpdate(batchArgs);
			}
			else {
						for (T item : items) {
						}
						return ps.executeBatch();
					}
				});
			}
				for (int i = 0; i < updateCounts.lengthi++) {
					int value = updateCounts[i];
					if (value == 0) {
						throw new EmptyResultDataAccessException("Item " + i + " of " + updateCounts.length
" did not update any rows: [" + items.get(i) + "]", 1);
					}
				}
			}
		}
	}
New to GrepCode? Check out our FAQ X