-1

我正在使用在我的 ItemWriter 中自动装配的现有 DAO 在数据库中插入元素。

我设置了提交间隔 = 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 个元素已提交!!!!

我需要帮助来找出我做错了什么!

问候

4

3 回答 3

3

终于找到我的问题了!!!我使用的是 MySql,默认情况下,它使用不支持事务的 MyIsam 引擎。

将其更改为 innoDB,现在一切正常。

于 2012-12-11T17:37:05.333 回答
1

我的猜测是它ServiceTransactionConciliationInterac有自己的事务处理,例如每个调用paiementService.insertAndReturnKey()都被事务包围。Spring Batch 将无法回滚那些内部事务。

于 2012-12-11T16:11:48.693 回答
0

将其更改为 innoDB,现在一切正常。

spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

于 2020-03-03T09:02:23.927 回答