CompositeItemWriter transactions rollback #4500
-
I have a class with CompositeItemWriter that writes to two tables. Following the example of: My question is about transactionality. I have verified that in case of an error in an insertion in the first table, an exception is thrown and no data is inserted into the database. On the contrary, if the error occurs in the second table to be processed, the data from the first table is inserted, so all changes are not rolled back. Is it possible to rollback both tables in case of error in the second table? Here my code: public CompositeItemWriter<CustomerViability> compositeWriter() throws Exception {
CompositeItemWriter<CustomerViability> compositeItemWriter = new CompositeItemWriter<>();
compositeItemWriter.setDelegates(Arrays.asList(customerWriter(), viabilityWriter()));
return compositeItemWriter;
}
// First table
private JdbcBatchItemWriter<CustomerViability> customerWriter() {
JdbcBatchItemWriter<CustomerViability> databaseItemWriter = new JdbcBatchItemWriter<>();
databaseItemWriter.setDataSource(dataSource);
databaseItemWriter.setSql("INSERT INTO customers (first_name, last_name, profession) VALUES (?, ?, ?)");
databaseItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
ItemPreparedStatementSetter<CustomerViability> invoicePreparedStatementSetter = new CustomerStatementSetter();
databaseItemWriter.setItemPreparedStatementSetter(invoicePreparedStatementSetter);
return databaseItemWriter;
}
// Second table
private JdbcBatchItemWriter<CustomerViability> viabilityWriter() {
JdbcBatchItemWriter<CustomerViability> databaseItemWriter = new JdbcBatchItemWriter<>();
databaseItemWriter.setDataSource(dataSource);
databaseItemWriter.setSql("INSERT INTO viability (first_name, last_name, viable) VALUES (?, ?, ?)");
databaseItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
ItemPreparedStatementSetter<CustomerViability> invoicePreparedStatementSetter = new ViabilityStatementSetter();
databaseItemWriter.setItemPreparedStatementSetter(invoicePreparedStatementSetter);
return databaseItemWriter;
} Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I provided a detailed answer with a complete example here: https://stackoverflow.com/questions/51904498/how-does-spring-batch-compositeitemwriter-manage-transaction-for-delegate-writer. To avoid duplicate efforts, I am closing this discussion. If you need more help, add a comment on that SO thread. |
Beta Was this translation helpful? Give feedback.
I provided a detailed answer with a complete example here: https://stackoverflow.com/questions/51904498/how-does-spring-batch-compositeitemwriter-manage-transaction-for-delegate-writer.
To avoid duplicate efforts, I am closing this discussion. If you need more help, add a comment on that SO thread.