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

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
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)

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

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

I finally understood the problem:

methodA() {
methodB()
} @Transactional(noRollbackFor = Exception.class)
methodB() {
...
try {
methodC()
} catch (...) {...}
log("OK");
} @Transactional
methodC() {
throw new ...();
}
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删除的源码是这样的

@Transactional
public void delete(ID id) { Assert.notNull(id, ID_MUST_NOT_BE_NULL); T entity = findOne(id); if (entity == null) {
throw new EmptyResultDataAccessException(String.format("No %s entity with id %s exists!",
entityInformation.getJavaType(), id), 1);
} delete(entity);
}

他是这样实现的,先查找这个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. 构建流式应用—RxJS详解[转]

    目录 常规方式实现搜索功能 RxJS · 流 Stream RxJS 实现原理简析 观察者模式 迭代器模式 RxJS 的观察者 + 迭代器模式 RxJS 基础实现 Observable Observe ...

  2. JS Date函数在safari中的问题

    问题描述:在做Web的时候,在PC上用Chrome调试成功,但是在safari一测就出现了问题.经过debug发现是日期相关出现问题.查阅一些资料后发现,safari中对于JavaScript的Dat ...

  3. Python——如何搭建Python的环境

    最近在学Python,只知道python一般是用来写爬虫的,以前看过一个朋友用Python做的爬虫从妹子图网站上下载图片,觉得很有趣,自己也想学一学. 俗话说,万事开头难,首先第一步就是搭建Pytho ...

  4. PL/SQL之存储过程和函数

    1.创建存储过程 .1语法: CREATE[OR REPLACE] PROCEDURE [schema.] procedure_name[(argument[{IN|OUT|IN OUT}] data ...

  5. 【10】Quartz.net 定时服务实例

    一.安装nuget包 Install-Package Quartz Install-Package Common.Logging.Log4Net1211 Install-Package log4net ...

  6. Q:java中的泛型数组

     对于java,其是不支持直接创建泛型数组的.当采用如下的方式去创建一个泛型数组时,其会出现错误,编译无法通过的情况. package other.jdk1_5; /** * 该类用于演示泛型数组的创 ...

  7. LeetCode GrayCode

    class Solution { public: vector<int> grayCode(int n) { vector<int> res; res.push_back(); ...

  8. SQL修改表结构

    --(1)向数据库Student表中添加Name字段 use MR_NXT alter table student add Name char(20) --(2)将Student表中Name的字段的数 ...

  9. javascript函数中with的介绍

    /*js函数中with函数的用法分析定义 方便用来引用某个对象中已有的属性但是不能用来给对象添加属性 要给对象创建新的属性 必须明确的引用该对象*/代码格式with(object) statement ...

  10. git杂记-远程仓库的使用

    查看远程仓库:克隆自己的仓库,如不命名则默认远程仓库名字为origin: $ git clone https://github.com/OuFeng/JF_WEB.git Cloning into ' ...