Class JdbcBatchItemWriter<T>
java.lang.Object
org.springframework.batch.item.database.JdbcBatchItemWriter<T>
- All Implemented Interfaces:
ItemWriter<T>
,org.springframework.beans.factory.InitializingBean
public class JdbcBatchItemWriter<T>
extends Object
implements ItemWriter<T>, org.springframework.beans.factory.InitializingBean
ItemWriter
that uses the batching features from
NamedParameterJdbcTemplate
to execute a batch of statements for all items
provided.
ItemPreparedStatementSetter
or 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(Chunk)
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, Michael Minella, Mahmoud Ben Hassine
-
Field Summary
Modifier and TypeFieldDescriptionprotected boolean
protected ItemPreparedStatementSetter<T>
protected ItemSqlParameterSourceProvider<T>
protected static final org.apache.commons.logging.Log
protected org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations
protected int
protected String
protected boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Check mandatory properties - there must be a NamedParameterJdbcOperations and an SQL statement plus a parameter source.protected void
processUpdateCounts
(int[] updateCounts) Extension point to post process the update counts for each item.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 theItemPreparedStatementSetter
.void
setItemSqlParameterSourceProvider
(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider) Public setter for theItemSqlParameterSourceProvider
.void
setJdbcTemplate
(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate) Public setter for theNamedParameterJdbcOperations
.void
Public setter for the query string to execute on write.void
Process the supplied data element.
-
Field Details
-
logger
protected static final org.apache.commons.logging.Log logger -
namedParameterJdbcTemplate
protected org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate -
itemPreparedStatementSetter
-
itemSqlParameterSourceProvider
-
sql
-
assertUpdates
protected boolean assertUpdates -
parameterCount
protected int parameterCount -
usingNamedParameters
protected boolean usingNamedParameters
-
-
Constructor Details
-
JdbcBatchItemWriter
public JdbcBatchItemWriter()
-
-
Method Details
-
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 setter for the query string to execute on write. The parameters should correspond to those known to theItemPreparedStatementSetter
.- Parameters:
sql
- the query to set
-
setItemPreparedStatementSetter
Public setter for theItemPreparedStatementSetter
.- Parameters:
preparedStatementSetter
- theItemPreparedStatementSetter
to set. This is required when using traditional '?' placeholders for the SQL statement.
-
setItemSqlParameterSourceProvider
public void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider) Public setter for theItemSqlParameterSourceProvider
.- Parameters:
itemSqlParameterSourceProvider
- theItemSqlParameterSourceProvider
to set. This is required when using named parameters for the SQL statement and the type to be written does not implementMap
.
-
setDataSource
Public setter for the data source for injection purposes.- Parameters:
dataSource
-DataSource
to use for querying against
-
setJdbcTemplate
public void setJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate) Public setter for theNamedParameterJdbcOperations
.- Parameters:
namedParameterJdbcTemplate
- theNamedParameterJdbcOperations
to set
-
afterPropertiesSet
public void afterPropertiesSet()Check mandatory properties - there must be a NamedParameterJdbcOperations and an SQL statement plus a parameter source.- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
write
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 interfaceItemWriter<T>
- Parameters:
chunk
- of items to be written. Must not benull
.- Throws:
Exception
- if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.
-
processUpdateCounts
protected void processUpdateCounts(int[] updateCounts) Extension point to post process the update counts for each item.- Parameters:
updateCounts
- the array of update counts for each item- Since:
- 5.1
-