调用的方法:
1. Struts Action
2. 服务类方法(使用@Transactional注解)
3. Xfire webservice调用
所有内容,包括struts(DelegatingActionProxy)和事务都是通过Spring配置的。
持久性是使用JPA/Hibernate完成的。
有时Web服务会抛出未经检查的异常。我捕获此异常并抛出已检查的异常。我不希望事务回滚,因为Web服务异常会改变当前状态。我像这样注释了该方法:
@Transactional(noRollbackFor={XFireRuntimeException.class, Exception.class})
public ActionForward callWS(Order order, ....) throws Exception
(...)
OrderResult orderResult = null;
try {
orderResult = webService.order(product, user)
} catch (XFireRuntimeException xfireRuntimeException) {
order.setFailed(true);
throw new WebServiceOrderFailed(order);
} finally {
persist(order);
}
}
我仍然会得到这个异常:
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
当我尝试使用junit重现此问题时,事务不会被标记为回滚,仍然可以提交该事务。
我如何让Spring不回滚该事务?
noRollbackFor
检查指定的异常类及其所有子类:http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/transaction/annotation/Transactional.html#noRollbackFor()此外,默认情况下,已检查异常不会触发回滚:http://static.springsource.org/spring/docs/2.5.x/reference/transaction.html#transaction-declarative-attransactional-settings - ChssPly76WebServiceOrderFailed
上回滚。 - Aaron DigullanoRollbackFor={..., Exception.class}
)不会有任何影响,因为Exception被特殊处理了(否则,继承代码也会忽略RuntimeException,因为它扩展了Exception)。 - Aaron Digulla