问题描述
在某些批处理作业中,我从一个类中调用了一个方法,该方法标记为:
In some batch job, i call a method from a class, that's marked with:
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = {
Fault.class,
AnotherFault.class
})
public class ...
而且,当我调用此方法时,抛出异常,我得到了异常:
And, when i call this method, and an exception throws i get exception:
00:29:25,765 ERROR [org.springframework.batch.core.step.AbstractStep] (executor-2) Encountered an error executing the step: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
为解决此问题,我尝试使用嵌套事务. Jpa不支持嵌套事务,因此我使用以下方法创建了虚拟类:
To solve this problem, i try use nested transactions. Jpa don't support nested transactions, so i create dummy class with:
@Transactional(propagation = Propagation.NOT_SUPPORTED)
并从没有事务的虚拟类中调用,我的方法是具有事务和rollbackFor.
and call from this dummy class without transactions, my method with transactions and rollbackFor.
此处描述的结果: http: //postgresql.1045698.n5.nabble.com/locking-problem-in-jdbc-driver-td2174897.html
因此,解决此问题的另一种方法-配置作业:
So, another way to resolve this problem - configure job:
<batch:step id="parse-step">
<batch:tasklet>
<batch:chunk reader="xmlCommonJob.Reader"
processor="xmlCommonJob.Processor"
writer="xmlCommonJob.Writer"
commit-interval="2"/>
<batch:no-rollback-exception-classes>
<batch:include class="com.my.common.services.fault.Fault"/>
<batch:include class="com.my.common.services.fault.AnotherFault"/>
</batch:no-rollback-exception-classes>
</batch:tasklet>
<batch:next on="FAILED" to="file-failed-step"/>
<batch:next on="COMPLETED" to="file-success-step"/>
</batch:step>
全部无济于事.
有什么主意吗?
推荐答案
在Spring Batch中,我们不应分步捕获异常,对于登录数据库,我们应该使用侦听器:
In Spring Batch we shouldn't catch exceptions in steps, for log in database, we should use listener:
<batch:step id="parse-step">
<batch:tasklet>
<batch:chunk reader="xmlCommonJob.Reader"
processor="xmlCommonJob.Processor"
writer="xmlCommonJob.Writer"
commit-interval="1">
<batch:skip-policy>
<bean class="org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy" scope="step"/>
</batch:skip-policy>
</batch:chunk>
</batch:tasklet>
<batch:next on="FAILED" to="file-failed-step"/>
<batch:next on="COMPLETED" to="file-success-step"/>
<batch:listeners>
<batch:listener ref="parseStepExecutionListener"/>
<batch:listener ref="parseStepSkipListener"/>
</batch:listeners>
</batch:step>
在bean parseStepSkipListener 中,我用logger记录异常,并且可以将信息保存到数据库中.
in bean parseStepSkipListener i log exceptions with logger and can save info to database.
这篇关于春季批.使用rollbackFor的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!