org.springframework.batch.item.database
Class JdbcBatchItemWriter<T>

java.lang.Object
  extended by org.springframework.batch.item.database.JdbcBatchItemWriter<T>
All Implemented Interfaces:
ItemWriter<T>, InitializingBean

public class JdbcBatchItemWriter<T>
extends Object
implements ItemWriter<T>, InitializingBean

ItemWriter that uses the batching features from 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(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.

Since:
2.0
Author:
Dave Syer, Thomas Risberg

Field Summary
protected static Log logger
           
 
Constructor Summary
JdbcBatchItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
          Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a parameter source.
 void setAssertUpdates(boolean assertUpdates)
          Public setter for the flag that determines whether an assertion is made that all items cause at least one row to be updated.
 void setDataSource(DataSource dataSource)
          Public setter for the data source for injection purposes.
 void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter)
          Public setter for the ItemPreparedStatementSetter.
 void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider)
          Public setter for the ItemSqlParameterSourceProvider.
 void setSimpleJdbcTemplate(SimpleJdbcOperations simpleJdbcTemplate)
          Public setter for the JdbcOperations.
 void setSql(String sql)
          Public setter for the query string to execute on write.
 void write(List<? extends T> items)
          Process the supplied data element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Log logger
Constructor Detail

JdbcBatchItemWriter

public JdbcBatchItemWriter()
Method Detail

setAssertUpdates

public void setAssertUpdates(boolean assertUpdates)
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;

setSql

public void setSql(String sql)
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

setItemPreparedStatementSetter

public void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter)
Public setter for the ItemPreparedStatementSetter.

Parameters:
preparedStatementSetter - the ItemPreparedStatementSetter to set. This is required when using traditional '?' placeholders for the SQL statement.

setItemSqlParameterSourceProvider

public void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider)
Public setter for the ItemSqlParameterSourceProvider.

Parameters:
itemSqlParameterSourceProvider - the ItemSqlParameterSourceProvider to set. This is required when using named parameters for the SQL statement.

setDataSource

public void setDataSource(DataSource dataSource)
Public setter for the data source for injection purposes.

Parameters:
dataSource -

setSimpleJdbcTemplate

public void setSimpleJdbcTemplate(SimpleJdbcOperations simpleJdbcTemplate)
Public setter for the JdbcOperations.

Parameters:
simpleJdbcTemplate - the JdbcOperations to set

afterPropertiesSet

public void afterPropertiesSet()
Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a parameter source.

Specified by:
afterPropertiesSet in interface InitializingBean

write

public void write(List<? extends T> items)
           throws Exception
Description copied from interface: ItemWriter
Process the supplied data element. Will not be called with any null items in normal operation.

Specified by:
write in interface ItemWriter<T>
Throws:
Exception - if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.


Copyright © 2013 SpringSource. All Rights Reserved.