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

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

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

ItemWriter that uses the batching features from PreparedStatement if available and can take some rudimentary steps to locate a failure during a flush, and identify the items that failed. When one of those items is encountered again the batch is flushed aggressively so that the bad item is eventually identified and can be dealt with in isolation.
The user must provide an SQL query and a special callback ItemPreparedStatementSetter, which is responsible for mapping the item to a PreparedStatement.
It is expected that write(List) is called inside a transaction.
The writer is thread safe after its properties are set (normal singleton behaviour), so it can be used to write in multiple concurrent transactions. Note, however, that the set of failed items is stored in a collection internally, and this collection is never cleared, so it is not a great idea to go on using the writer indefinitely. Normally it would be used for the duration of a batch job and then discarded.

Author:
Dave Syer

Constructor Summary
BatchSqlUpdateItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
          Check mandatory properties - there must be a delegate.
 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 setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter)
          Public setter for the ItemPreparedStatementSetter.
 void setJdbcTemplate(JdbcOperations jdbcTemplate)
          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
 

Constructor Detail

BatchSqlUpdateItemWriter

public BatchSqlUpdateItemWriter()
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

setJdbcTemplate

public void setJdbcTemplate(JdbcOperations jdbcTemplate)
Public setter for the JdbcOperations.

Parameters:
jdbcTemplate - the JdbcOperations to set

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Check mandatory properties - there must be a delegate.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

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 © 2008 SpringSource. All Rights Reserved.