我的类(class)声明上方有此声明。 spring只会在抛出DAOException时回滚吗?
@Transactional(rollbackFor = { ManagerException.class, DAOException.class })
如果是这种情况,我怎样才能回滚错误的提交而不引发异常。目前我的数据库包函数返回错误消息,我想回滚并仍然向用户显示错误消息,而不抛出异常并使应用程序崩溃。
最佳答案
You can also indicate a required rollback programmatically. Although very simple, this process is quite invasive, and tightly couples your code to the Spring Framework's transaction infrastructure:
public void resolvePosition() {
try {
// some business logic...
} catch (NoProductInStockException ex) {
// trigger rollback programmatically
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
}
关于java - 回滚事务功能是否仅在抛出异常时才起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19540480/