我正在使用在ItemWriter中自动装配的现有DAO在数据库中插入元素。
我设置了commit-interval = 5.
在我的xml(读取)中,我已将第5个元素中的无效数据用于测试重启功能。
这是我的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;
}
}
paiementService是一个Spring bean,它使用JDBCTemplate在数据库中插入数据。
我的配置:
<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>
现在,当我运行它时,我看到我得到一个异常(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)
步骤状态如下所示:
<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>
这看起来不错,我可以看到我已经阅读了5个元素,写了0个并且发生了1个回滚。
但是当我查看数据库时,我可以看到前4个元素已经提交!!!!
我需要帮助才能找到我做错的事情!
此致
答案 0 :(得分:3)
终于找到了我的问题!我使用的是MySql,默认情况下,它使用不支持事务的MyIsam引擎。
将其更改为innoDB,现在一切正常。
答案 1 :(得分:1)
我的猜测是ServiceTransactionConciliationInterac
拥有自己的交易处理,例如每个对paiementService.insertAndReturnKey()
的调用都被一个Transaction包围。 Spring Batch无法回滚那些内部事务。
答案 2 :(得分:0)
将其更改为innoDB,现在一切正常。
spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect