在某些批处理作业中,我从类调用一个方法,该方法标记为:
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = {
Fault.class,
AnotherFault.class
})
public class ...而且,当我调用此方法时,当异常抛出时,就会得到异常:
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不支持嵌套事务,因此我使用以下方法创建虚拟类:
@Transactional(propagation = Propagation.NOT_SUPPORTED)并从这个没有事务的虚拟类、使用事务的方法和rollbackFor调用。
这里描述的结果:http://postgresql.1045698.n5.nabble.com/locking-problem-in-jdbc-driver-td2174897.html
因此,解决此问题的另一种方法是配置作业:
<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>一切都没有结果。
有什么想法吗?
发布于 2013-12-14 11:37:45
在Spring批处理中,我们不应该分步骤捕获异常,对于登录数据库,我们应该使用侦听器:
<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中,我用记录器记录异常,并可以将信息保存到数据库中。
https://stackoverflow.com/questions/20411311
复制相似问题