Java 春季批次。使用rollboor调用方法
在某些批处理作业中,我从类中调用一个方法,该类标记为:Java 春季批次。使用rollboor调用方法,java,jpa,transactions,spring-batch,Java,Jpa,Transactions,Spring Batch,在某些批处理作业中,我从类中调用一个方法,该类标记为: @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = { Fault.class, AnotherFault.class }) public class ... 当我调用此方法时,异常抛出,我得到异常: 00:29:25,765 ERROR [org.springframework.batch.core.step.Abstrac
@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)
并从这个没有事务的伪类调用,我的方法有事务和rollboor
此处描述了此操作的结果:
因此,解决此问题的另一种方法是配置作业:
<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批处理时,不建议使用@Transaction注释业务方法。Spring批处理框架内的事务语义,因此添加@Transactional可能会导致问题。我建议删除注释并让Spring批处理它。在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中,我使用logger记录异常,并可以将信息保存到数据库。是否尝试在描述作业的同一xml中进行设置?在具有作业配置的xml中指出这一点很重要?我在appContex.xml中设置了inlude batch.xml。不久前,我遇到了tx管理不正确的问题,并将
放入作业xml中解决了问题;也许这能帮你,不一定。这并不能解决我的问题。也许更容易理解信息:我可以从类中删除@Transaction,但不能从业务方法中删除。我努力解决这个问题。很快我描述了我是如何做到这一点的。