且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring事务和多个表上的回滚

更新时间:2023-02-04 20:51:48

更新我的答案:

  1. 您希望公开报价保存(报价)"方法具有事务性.
  2. 调用此方法时...事务在TransactionInterceptor中开始,并从代理中调用公共报价保存(报价)"
  3. 行"quoteLineDao.delete(new Long(44));"效果很好
  4. 行"System.out.println(°°°°°°°°°°°°°°°°°°行44已删除");效果很好
  5. 行"quoteDao.save(quote);"给出约束违反异常.交易被标记为回滚
  6. 您正在捕获此异常并使用它,而不传播该异常
  7. 由于"return null;"行,方法"public quote save(Quote quote)"将返回null.
  8. 现在代码到达事务拦截器,并且由于此拦截器没有异常,因此它尝试提交,但是事务已被标记为回滚,因此失败.

解决方案:-您不能因为事务需要而消耗异常,而应传播异常.

Solution:- You must not consume the exception and instead propagate the exception because of your transactional need.

更改为以下内容.添加了throw语句.

Change to following. have added throw statement.

try{
      quoteLineDao.delete(new Long(44));
      System.out.println("°°°°°°°°°°°°°°°°°°Line 44 deleted");
      return  quoteDao.save(quote); 
} catch(Exception e){
      Logger.getLogger(QuoteService.class).log(Logger.Level.ERROR, e);
      throw e;
}

此链接中提供了逐步说明:无法提交JPA交易:标记为rollbackOnly的交易

Step by step explanation is available in this link: Could not commit JPA transaction: Transaction marked as rollbackOnly