项目测试发生问题,方法正常结束,但是报了

  1. Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
  2. at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
  3. at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
  4. at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
  5. at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
  6. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
  7. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  8. at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)

错误,问什么不能提交呢?

经过查找发现了这么一段话

  1. I finally understood the problem:
  2.  
  3. methodA() {
  4. methodB()
  5. }
  6.  
  7. @Transactional(noRollbackFor = Exception.class)
  8. methodB() {
  9. ...
  10. try {
  11. methodC()
  12. } catch (...) {...}
  13. log("OK");
  14. }
  15.  
  16. @Transactional
  17. methodC() {
  18. throw new ...();
  19. }
  20. What happens is that even though the methodB has the right annotation, the methodC does not. When the exception is thrown, the second @Transactional marks the first transaction as Rollback only anyway.

原来,在一个transactional中如果有另一transaction发生了异常,即使你捕捉了这个异常,那么Transaction也会被定义成RollbackOnly,这也正是事务管理的原则,可是我的系统哪里出异常了呢?

原来,spring jpa JpaRepository的实现方法中用ID删除的源码是这样的

  1. @Transactional
  2. public void delete(ID id) {
  3.  
  4. Assert.notNull(id, ID_MUST_NOT_BE_NULL);
  5.  
  6. T entity = findOne(id);
  7.  
  8. if (entity == null) {
  9. throw new EmptyResultDataAccessException(String.format("No %s entity with id %s exists!",
  10. entityInformation.getJavaType(), id), 1);
  11. }
  12.  
  13. delete(entity);
  14. }

他是这样实现的,先查找这个ID的对象看是否存在如果不存在则直接抛出一个非检查型异常,就不再执行delete操作。这和我平常习惯认为的不太一样,一般我习惯删除没有的记录不会报错,执行sql也是这样的。而我只是在外面捕捉了这个异常,所以发生的这样的问题。

解决Could not commit JPA transaction RollbackException: Transaction marked as rollbackOnly的更多相关文章

  1. Could not commit JPA transaction RollbackException: Transaction marked as rollbackOnly

    项目调试时,报以下错误: org.springframework.transaction.TransactionSystemException: Could not commit JPA transa ...

  2. spring事务(Transaction )报 marked as rollback-only异常的原因及解决方法

    很多朋友在使用spring+hibernate或mybatis等框架时经常遇到报Transaction rolled back because it has been marked as rollba ...

  3. 使用JPA保存对象时报nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly错误

    使用JPA保存对象时报nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOn ...

  4. [springboot jpa] [bug] Could not open JPA EntityManager for transaction

    前言 最近,测试环境遇到了一个问题.经过一番百度加谷歌,终于解决了这个问题.写下这篇博客是为了记录下解决过程,以便以后查看.也希望可以帮助更多的人. 环境 java版本:8 框架:spring clo ...

  5. Transaction rolled back because it has been marked as rollback-only分析解决方法

    1. Transaction rolled back because it has been marked as rollback-only事务已回滚,因为它被标记成了只回滚<prop key= ...

  6. Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Cannot open connection

    Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceE ...

  7. --解决Lock wait timeout exceeded; try restarting transaction

    --解决Lock wait timeout exceeded; try restarting transaction select * from information_schema.innodb_t ...

  8. spring boot配置spring-data-jpa的时候报错CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError

    org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager f ...

  9. Transaction rolled back because it has been marked as rollback-only

    出现这种错误的原因 1.接口A 调用了接口B 2.接口B报异常了,没有在B里面进行try catch捕获 3.接口A对 接口B进行了try catch捕获 因为接口B报异常 会把当前事物A接口的事物( ...

随机推荐

  1. ajax从零基础到实战

    一. 什么是AJAX? ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 二. 在项目中怎么运用AJAX? 项目主要文件夹目录有img文件夹,css文件夹,js文件夹,如果你要运 ...

  2. 实现一个简单的"jQuery"

    本次博客记录一个简单的"jQuey"的实现方式,来加深对jQuery的理解: 代码: <!DOCTYPE html> <html> <head> ...

  3. 利用jquery的ajax实现跨域,内部其实是jsonp协议了,不是XHRhttp协议

    一.同源策略 要理解跨域,先要了解一下“同源策略”.所谓同源是指,域名,协议,端口相同.所谓“同源策略“,简单的说就是基于安全考虑,当前域不能访问其他域的东西. 一些常见的是否同源示例可参照下表: 在 ...

  4. postgresql 备份与恢复

    备份 C:\PostgreSQL\\bin>pg_dump -h localhost -p -U postgres Mes > d:/.bak 恢复 C:\PostgreSQL\\bin& ...

  5. xsl 和xml transform方法的调用

    xsl 和xml生成html,兼容多个浏览器 <html> <head> <meta charset="UTF-8"/> </head&g ...

  6. Spring Boot学习笔记(二)全局捕获异常处理

    非常简单只需要创建自己的异常处理类,加上两个注解,就可以了

  7. fuzhou 1683 纪念SlingShot ***

    Problem 1683 纪念SlingShot Accept: 361    Submit: 1287Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  8. BZOJ 3809Gty的二逼妹子序列 解题报告+data marker

    --BZOJ http://www.lydsy.com/JudgeOnline/problem.php?id=3809 考虑对l,r跑莫队,对一组维护美丽度出现次数的桶修改, 然后把桶序列用分块维护查 ...

  9. 使用javascript调用android代码

    1.使用webview对象的addJavascriptInterface方法 2.addJavascriptInterface方法有两个参数,第一个参数就是我们一般会实现一个自己的类,类里面提供我们要 ...

  10. 代码整洁之道读书笔记(Ch4-Ch7)

    这几章从注释.程序格式.对象与数据结构的规范以及错误处理四个方面介绍了如何使代码变得简洁易懂.不同于上次摘抄的方法,这一次我会结合第一次个人作业的代码进行分析. 第四章  注释 这一章告诉我们,好的注 ...