Java 事务回滚不使用传播。设置默认回滚规则时需要\u NEW
我使用MyBatis 3.4.5和Spring 4.1.6。我已经检查了抽象类GenericeException(ExtendesException)和CustomException(ExtendesGenericeException)。我在tx中为GenericeException设置了默认回滚:Java 事务回滚不使用传播。设置默认回滚规则时需要\u NEW,java,spring,spring-transactions,rollback,Java,Spring,Spring Transactions,Rollback,我使用MyBatis 3.4.5和Spring 4.1.6。我已经检查了抽象类GenericeException(ExtendesException)和CustomException(ExtendesGenericeException)。我在tx中为GenericeException设置了默认回滚: <tx:advice id="txSetRollBackForCoreExceptionsAdvice" transaction-manager="transactionManager">
<tx:advice id="txSetRollBackForCoreExceptionsAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="RuntimeException,xxx.exceptions.GenericException"/>
</tx:attributes>
</tx:advice>
服务2:
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Service
public class TimerStatePushServiceBean implements TimerStatePushService {
@Autowired
PushService pushService;
@Override
public Long pushEvent(TimerState timerState) throws GenericException {
return pushService.pushEventById(timerState.getId());
}
如果pushService抛出CustomException(它扩展了GenericeException),我将在Service1中捕获意外的回滚异常。在跟踪日志中,我看到:
12:42:48.677 TRACE {pool-15-thread-1} [o.s.t.i.RuleBasedTransactionAttribute] : Applying rules to determine whether transaction should rollback on xxx.exceptions.CustomException
12:42:48.677 TRACE {pool-15-thread-1} [o.s.t.i.RuleBasedTransactionAttribute] : Winning rollback rule is: null
12:42:48.677 TRACE {pool-15-thread-1} [o.s.t.i.RuleBasedTransactionAttribute] : No relevant rollback rule found: applying default rules
12:42:48.677 DEBUG {pool-15-thread-1} [o.s.j.d.DataSourceTransactionManager] : Global transaction is marked as rollback-only but transactional code requested commit
如果我设置“rollboor=GenericException.class”-一切正常:
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = GenericException.class)
@Service
public class TimerStatePushServiceBean implements TimerStatePushService {
@Autowired
PushService pushService;
@Override
public Long pushEvent(TimerState timerState) throws GenericException {
return pushService.pushEventById(timerState.getId());
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Service
public class TimerStatePushServiceBean implements TimerStatePushService {
@Autowired
PushService pushService;
@Override
public Long pushEvent(TimerState timerState) throws GenericException {
throw new CustomException("msg");
}
如果我在方法“pushEvent”中抛出CustomException,而不设置rollbackFor,则一切正常:
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = GenericException.class)
@Service
public class TimerStatePushServiceBean implements TimerStatePushService {
@Autowired
PushService pushService;
@Override
public Long pushEvent(TimerState timerState) throws GenericException {
return pushService.pushEventById(timerState.getId());
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Service
public class TimerStatePushServiceBean implements TimerStatePushService {
@Autowired
PushService pushService;
@Override
public Long pushEvent(TimerState timerState) throws GenericException {
throw new CustomException("msg");
}
我不明白发生了什么事。仅供参考:我需要在TimerStatePushService中回滚事务,而不在Service1中回滚事务。我解决了这个问题。我有两个模块:核心模块和管理模块。对于核心设置:
<aop:config>
<aop:pointcut expression="within(xxx.core..*) && @within(org.springframework.stereotype.Service)" id="inCoreServiceCall"/>
<aop:advisor order="1" advice-ref="txSetRollBackForCoreExceptionsAdvice" pointcut-ref="inCoreServiceCall"/>
</aop:config>
PushService在核心模块中,另一个服务在manager模块中。
我为管理器模块添加了设置