org.springframework.batch.item.database
Class BatchSqlUpdateItemWriter

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

public class BatchSqlUpdateItemWriter
extends AbstractTransactionalResourceItemWriter
implements 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 AbstractTransactionalResourceItemWriter.write(Object) is called inside a transaction, and that AbstractTransactionalResourceItemWriter.flush() is then subsequently called before the transaction commits, or AbstractTransactionalResourceItemWriter.clear() before it rolls back.
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.
protected  void doClear()
          No-op.
protected  void doFlush()
          Create and execute batch prepared statement.
protected  void doWrite(Object output)
          No-op.
protected  String getResourceKey()
           
 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 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.
 
Methods inherited from class org.springframework.batch.item.database.AbstractTransactionalResourceItemWriter
clear, flush, getProcessed, write
 
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 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

doFlush

protected void doFlush()
                throws EmptyResultDataAccessException
Create and execute batch prepared statement.

Specified by:
doFlush in class AbstractTransactionalResourceItemWriter
Throws:
EmptyResultDataAccessException - if any of the items does not cause an update

getResourceKey

protected String getResourceKey()
Specified by:
getResourceKey in class AbstractTransactionalResourceItemWriter
Returns:
Key for items processed in the current transaction RepeatContext.

doWrite

protected void doWrite(Object output)
No-op.

Specified by:
doWrite in class AbstractTransactionalResourceItemWriter

doClear

protected void doClear()
                throws ClearFailedException
No-op.

Specified by:
doClear in class AbstractTransactionalResourceItemWriter
Throws:
ClearFailedException


Copyright © 2009 SpringSource. All Rights Reserved.