I am using a Existing DAO autowired in my ItemWriter to insert elements in the database.
I have set the commit-interval = 5.
In my xml (read), i have put invalid data in the 5th element to test out the restart feature.
here is my ItemWriter:
@Component("transactionItemWriter")
public class TransactionInteracItemWriter implements ItemStreamWriter<TransactionConciliationInterac> {
private static final Logger log = Logger.getLogger(TransactionInteracItemWriter.class);
@Autowired
private ConciliationContext conciliationContext;
@Autowired
private ServiceTransactionConciliationInterac paiementService;
private int transactionCount;
/**
* @see ItemWriter#write(java.util.List)
*/
public void write(List<? extends TransactionConciliationInterac> data) throws Exception {
int index = ++transactionCount - 1;
while (index < data.size()) {
TransactionConciliationInterac trx = data.get(index);
paiementService.insertAndReturnKey(trx,conciliationContext.getIdSommaire().toString());
index = ++transactionCount - 1;
}
}
the paiementService is a Spring bean using JDBCTemplate to insert data in the DB.
my config :
<batch:step id="traitementXmlIemtTransactionStep">
<batch:tasklet transaction-manager="transactionManager" start-limit="10">
<batch:chunk reader="IemtXmlTransactionReader" processor="IemtXmlTransactionConvertProcessor" writer="transactionItemWriter" commit-interval="5"/>
<batch:transaction-attributes isolation="DEFAULT" propagation="REQUIRED"/>
<batch:listeners>
<batch:listener ref="transactionStepListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
Now, when i run this, i see that i get an exception (DataIntegrityEx) :
<Rollback for RuntimeException: org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1>
2012-12-11 09:50:28,949 DEBUG [org.springframework.batch.repeat.support.RepeatTemplate] - <Handling exception: org.springframework.dao.DataIntegrityViolationException, caused by: org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1>
2012-12-11 09:50:28,949 DEBUG [org.springframework.batch.repeat.support.RepeatTemplate] - <Handling fatal exception explicitly (rethrowing first of 1): org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1>
2012-12-11 09:50:28,950 ERROR [org.springframework.batch.core.step.AbstractStep] - <Encountered an error executing the step>
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'TRANSACTIONTYPE' at row 1
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
The step status look like this :
<Step execution complete: StepExecution: id=3, version=2, name=traitementXmlIemtTransactionStep, status=FAILED, exitStatus=FAILED, readCount=5, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=0, rollbackCount=1>
This look ok, i can see i have read 5 elements, 0 were written and 1 rollback occurred.
BUT when i look in the Database, i can see that the first 4 elements were committed!!!!
I need help to find what i am doing wrong!
Regards
Finally found my issue!!! I was using MySql and by default, it uses MyIsam engine which does not support transactions.
Changed it to innoDB and now everything works great.