spring的事件是在调用业务方法之前开始的,业务方法执行完毕之后才执行commit or rollback,事件是否执行取决于是否抛出runtime异常。如果抛出runtime exception 并在你的业务方法中没有catch到的话,事件会回滚。
在业务方法中一般不需要catch异常,如果非要catch肯定要抛出throw new RuntimeException(),或者注解中指定抛异常类型@Transactional(rollbackFor=Exception.class),否则会导致事件失效,数据commit造成数据不同等,所以有些时间try catch反倒会画蛇添足。
嵌套事件题目
</bean> txObject.setConnectionHolder((ConnectionHolder)null, false); } throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", var7); } }
</bean> throw new NestedTransactionNotSupportedException("Transaction manager does not allow nested transactions by default - specify 'nestedTransactionAllowed' property with value 'true'"); <bean id="valuationTransactionManager" >
public final void commit(TransactionStatus status) throws TransactionException { // 事件状态已完成则抛异常 if (status.isCompleted()) { throw new IllegalTransactionStateException("Transaction is already completed - do not call commit or rollback more than once per transaction"); } else { DefaultTransactionStatus defStatus = (DefaultTransactionStatus)status; //发现回滚标志 if (defStatus.isLocalRollbackOnly()) { <bean id="valuationTransactionManager" >
</bean> throw new UnexpectedRollbackException("Transaction rolled back because it has been marked as rollback-only"); <bean id="valuationTransactionManager" >
</bean> throw new UnexpectedRollbackException("Transaction silently rolled back because it has been marked as rollback-only"); <bean id="valuationTransactionManager" >