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

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. 【angular5项目积累总结】表单复杂校验

    view code form.css :host { display: flex; width: 100%; height:100%; border-left:1px solid #ccc; } .i ...

  2. MVC缓存(一)

    //OutputCache是设置缓存,参数Duration设置缓存的过期时间,OutputCache可以加到Controller上,也可以加到Action上,但是当Controller与Action都 ...

  3. 工作中遇到的一道SQL应用题

    登录日志表 CREATE TABLE [dbo].[LoginLog]([Seq] [int] NOT NULL IDENTITY(1, 1),  --Seq[UserId] [varchar] (2 ...

  4. 经典SQL分页语句

    select top pageSize, * from (SELECT row_number() over(order by id desc) as rownumber,*FROM tb1) A wh ...

  5. Fask中的路由-模版-静态文件引用 及宏定义与表单

    文档: flask: http://docs.jinkan.org/docs/flask/quickstart.html#redirects-and-errors jinja2 http://docs ...

  6. 解决-word里无论怎么改变字体颜色,字体总是红色的

    1.你遇到的问题是Word当前处于审阅状态,修改的内容显示为红色字体.2.解决办法是退出Word审阅状态,或者接受全部修订.3.不同Word版本的审阅模式不同,可在菜单栏里退出审阅,或者按鼠标右键弹出 ...

  7. 关于一次美团java程序员招聘面试的经历

    美团一面: 中间省略掉大概几个问题,因为我不记得了,下面记得的基本都是我没怎么答好的. 1.了解SOA,微服务吗? 2.分布式系统如何负载均衡?如何确定访问的资源在哪个服务器上? 一.轮询.二.随机. ...

  8. springboot基本注解

    声明Bean的注解: @Component组件 @Service service层 @Respository dao层 @Controller 注入Bean的注解: @Autowired Spring ...

  9. BZOJ1927: [Sdoi2010]星际竞速(最小费用最大流 最小路径覆盖)

    题意 题目链接 Sol 看完题不难想到最小路径覆盖,但是带权的咋做啊?qwqqq 首先冷静思考一下:最小路径覆盖 = \(n - \text{二分图最大匹配数}\) 为什么呢?首先最坏情况下是用\(n ...

  10. ionCube 安装

    有些程序在php环境下运行需要安装ionCube Loader的扩展支持,得到如下提示 Site error: the ionCube PHP Loader needs to be installed ...