-
Transaction rolled back
2018-01-05 17:26:15出现这个问题的原因是查询跟插入一个事务了,因此在插入事务或者查询事务回滚的时候冲突了。把这两个分开来,查询归查询,插入归插入。出现这个问题的原因是查询跟插入一个事务了,因此在插入事务或者查询事务回滚的时候冲突了。把这两个分开来,查询归查询,插入归插入。
-
Transaction rolled back because it has been marked as rollback-only
2018-08-29 15:26:56Transaction rolled back because it has been marked as rollback-only 问题 程序运行报异常 org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been ...Transaction rolled back because it has been marked as rollback-only
问题
程序运行报异常 org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only,错误日志堆栈找不出明显痕迹。
场景还原
入口:
@Transactional public void saveUserList(List<User> userList) { User user = new User("100", "杭州", "张三"); serviceWarp.saveUser(user); try { serviceWarp.saveUserList(userList); } catch (Exception e) { log.error("啥也不干"); } }
内部调用:
public void saveUser(User user) { userMapper.saveUser(user); }
@Transactional public void saveUserList(List<User> userList) { for (User user : userList) { userMapper.saveUser(user); // 数据库中造好数据,让它抛出主键冲突异常 } }
原因
spring 事务管理,启用事务的方法,调用另一个事务方法时,会进行事务传播。注解@Transactional默认的传播机制是PROPAGATION_REQUIRED。
传播行为 含义 PROPAGATION_REQUIRED 表示当前方法必须运行在事务中。如果当前事务存在,方法将会在该事务中运行。否则,会启动一个新的事务 所以是补齐方法运行的时候,同步的事务合并到了补齐的事务里面。当同步发票发生异常后,被try catch 捕获,没有抛出来。但是事务还是会进行回滚,回滚执行到 DataSourceTransactionManager 类的 doSetRollbackOnly 方法时,设置了rollbackOnly = true;
public void setRollbackOnly() { this.getConnectionHolder().setRollbackOnly(); }
public void setRollbackOnly() { this.rollbackOnly = true; }
由于异常被catch, 不阻断整个事务执行。整个事务执行完后,执行commit 提交,执行到 DefaultTransactionStatus 类的 isGlobalRollbackOnly方法时,判断rollbackOnly 为true, 则执行回滚,并且打出那句报错的日志”Transaction rolled back because it has been marked as rollback-only”。
if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Global transaction is marked as rollback-only but transactional code requested commit"); } processRollback(defStatus); // Throw UnexpectedRollbackException only at outermost transaction boundary // or if explicitly asked to. if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) { throw new UnexpectedRollbackException( "Transaction rolled back because it has been marked as rollback-only"); }
@Override public boolean isGlobalRollbackOnly() { return ((this.transaction instanceof SmartTransactionObject) && ((SmartTransactionObject) this.transaction).isRollbackOnly()); }
一些思考
- 一个事务能完成就一个事务
- 事务嵌套传播的时候,少用try catch, 该回滚就回滚
- 事务嵌套传播的时候, 可以指定传播方式,根据需求可以将内部事务指定为@Transactional(propagation = Propagation.NESTED) 或 @Transactional(propagation = Propagation.PROPAGATION_REQUIRED_NEW)
传播行为 含义 PROPAGATION_REQUIRED_NEW 表示当前方法必须运行在它自己的事务中。一个新的事务将被启动。如果存在当前事务,在该方法执行期间,当前事务会被挂起。 PROPAGATION_NESTED 表示如果当前已经存在一个事务,那么该方法将会在嵌套事务中运行。嵌套的事务可以独立于当前事务进行单独地提交或回滚。如果当前事务不存在,那么其行为与PROPAGATION_REQUIRED一样。注意各厂商对这种传播行为的支持是有所差异的。可以参考资源管理器的文档来确认它们是否支持嵌套事务 -
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has
2015-12-10 18:41:14今天在操作过程中遇到了...org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at org.springframework.transaction.support.Abs今天在操作过程中遇到了这样一个问题:
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:695) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321) at org.springframework.transaction.interceptor.TransactionInter.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662)ceptor.invoke(TransactionInterceptor.java:116) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635) at com.newspace.atetgame.activeDevice.bo.impl.ActiveDeviceBoImpl$$EnhancerByCGLIB$$39953cee.saveActiveDevice(<generated>) at com.newspace.atetgame.web.activeDevice.ActiveDeviceServlet.doPost(ActiveDeviceServlet.java:50) at javax.servlet.http.HttpServlet.service(HttpServlet.java:643) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve
这个是我在做一个接口操作的时候出现的,情景如下:1、在接口中我首先做了一个insert,插入数据库操作,然后写了一个try ... catch ...
2、当时是已经在第1点里面,抛了异常了,然后e.printStackTrace();将异常打印出来;然后从bo方法里面跳转回来时候,结果捕捉异常时候就将这个问题打印出来
解决方法:
1、你肯定在Action或者servlet类中已经抛了异常并捕获了异常,如下格式:
try { // do something } catch(Exception e) { e.printStackTrace(); logger.error("XXXX.do: 操作失败", e); }
2、
在do something 里面如果进行了insert或者save等操作,如果在一个bo里面进行的,那么那个bo类里面不需要再写try...catch...
亦或是
在bo类中加上try ... catch ... 也把异常进行捕获,然后终止其操作就对了。
(我采用了在bo类中不加try ... catch ... 操作)
问题解决。
更多精彩敬请关注公众号
Java极客思维
微信扫一扫,关注公众号
-
[REGRESSION] Transaction rolled back due to time out
2020-12-09 12:40:58<p>javax.transaction.RollbackException: Transaction rolled back due to time out. <p>Steps to reproduce: <p>1) checkout test from cvs <p>set variables: export CVSROOT=:pserver:.red.iplanet.... -
Can't reconnect until invalid transaction is rolled back
2020-12-09 08:52:17<div><p>OpenTaxii server responds with ERROR to all...t reconnect until invalid transaction is rolled back" features in the log files.</p><p>该提问来源于开源项目:eclecticiq/OpenTAXII</p></div> -
org.springframework.transaction.UnexpectedRollbackException: JTA transaction already rolled back (pr
2019-12-09 16:46:00org.springframework.transaction.UnexpectedRollbackException: JTA transaction already rolled back (probably due to a timeout) 错误原因默认的Spring 默认的事务是 tomeout 是 -1 原因是 这里使用Spring 默认...org.springframework.transaction.UnexpectedRollbackException: JTA transaction already rolled back (probably due to a timeout)
错误原因默认的Spring 默认的事务是 tomeout 是 -1
原因是 这里使用Spring 默认事务
java int timeout() default -1;
sql 查询时间长 再加上入库时间 超出了默认的时间范围
所以抛出了 UnexpectedRollbackException 异常
打印 JTA transaction already rolled back (probably due to a timeout) 错误解决办法:
- 设置超时间时间 长点
java @Transactional(rollbackFor = Exception.class,timeout = 60)
- 将插入语句分离出去,将事务加到插入语句中
- 优化sql 减少查询时间
- 设置超时间时间 长点
-
Srping Transaction rolled back because it has been marked as rollback-only解决方案
2016-07-29 10:54:541.异常相关描述 ...org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at org.springframework.transaction.support -
UnexpectedRollbackException: Transaction rolled back 关于失误自动回滚的问题
2018-10-09 17:43:58org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only 字面意思: 出现了不可预知的回滚异常,因为事务已经被标志位... -
Transaction was rolled back more times than it was started
2021-01-11 02:27:24<p>com.orientechnologies.orient.core.exception.OTransactionException: Transaction was rolled back more times than it was started. at ... -
UnexpectedRollbackException: Transaction rolled back because it has been mar
2013-12-03 11:41:48UnexpectedRollbackException: Transaction rolled back because it has been mar,不知道还有没有其他办法 -
UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback
2021-04-12 13:53:11org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only at org.springframework.transaction.support.... -
Transaction rolled back because it has been marked as rollback-only异常原因及处理
2019-10-12 14:44:32Transaction rolled back because it has been marked as rollback-only异常原因及处理 报错异常信息: org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has ... -
Transaction silently rolled back because it has been marked as rollback-only
2021-02-28 18:04:32Transaction silently rolled back because it has been marked as rollback-only try catch 与 throws 重复使用 -
Outbound MDB connection still associated with transaction after being rolled back
2020-12-09 13:41:49MDB receives message first time, sets mdbContext.setRollbackOnly so the transaction is rolled back. 2. MDB receives message second time and sends the reply message (no rollback, should succeed). <p>... -
Transaction rolled back because it has been marked as rollback-only分析解决方法
2020-08-19 12:46:48Transaction rolled back because it has been marked as rollback-only分析解决方法 -
Transaction rolled back because it has been marked as rollback-only异常
2017-11-10 16:32:10org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at org.springframework.transaction.support.AbstractPlatfo -
Conditions for "Transaction rolled back even if marked as succesful
2020-12-27 09:09:42the transaction is supposed to have been rolled back, the transaction is actually committed. In this case what I would also like to ask is if we can safely ignore this particular exception if it comes... -
and re-raise if transaction is rolled back
2020-12-26 19:19:52<p>I would suggest that while in a transaction, the <code>PropertyChanged</code> is raised immediately on the owning thread and re-raised, if the transaction is rolled back. All other threads should ... -
transaction silently rolled back becauser it han been marked as rollback-only
2020-12-14 16:53:18transaction silently rolled back becauser it han been marked as rollback-only 是因为代码里有多重事务,可以看看controller service 是否都做了 事务的回滚 -
UnexpectedRollbackException:Transaction rolled back because it has been marked rollback-only
2019-11-15 14:16:10在程序运行过程中出现:org.springframework.transaction.UnexpectedRollbackException:Transaction rolled back because it has been marked rollback-only 抛出该异常的原因,方法A调用了方法B, 方法A有事务,... -
springboot 事务 Transaction 异常 Transaction rolled back because it has been marked as rollback-only
2019-08-22 15:16:55解决参考:... 异常信息 ERROR c.r.f.w.e.DefaultExceptionHandler - [notFount,50] - 运行时异常: org.springframework.transaction.UnexpectedRollbackExceptio...