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

男娘i 2022-04-12 10:14 265阅读 0赞
  1. 项目中出现了“Transaction rolled back because it has been marked as rollback-only”错误,上网搜索了一下,发现如下文章写的很好。
  2. spring的声明事务提供了强大功能,让我们把业务关注和非业务关注的东西又分离开了。好东西的使用,总是需要有代价的。使用声明事务的时候,一个不小心经常会碰到“Transaction rolled back because it has been marked as rollback-only”这个异常。有时候又常常会纳闷,"我已经try-catch了,为什么还这样呢?"
  3. <!-- 0 placeHolder -->
  4. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  5. <property name="locations">
  6. <list>
  7. <value>files/pro.properties</value>
  8. </list>
  9. </property>
  10. </bean>
  11. <!-- 1 dataSource -->
  12. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  14. <property name="url" value="${jdbc.mysql.url}"></property>
  15. <property name="username" value="${jdbc.username}"></property>
  16. <property name="password" value="${jdbc.userpassword}"></property>
  17. </bean>
  18. <!-- 2 jdbcTemplate -->
  19. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  20. <property name="dataSource" ref="dataSource"></property>
  21. </bean>
  22. <!-- 3 BaseDao -->
  23. <bean id="baseDao" class="transaction.dao.BaseDao" abstract="true">
  24. <property name="jdbcTemplate" ref="jdbcTemplate" />
  25. </bean>
  26. <bean id="aDao" class="transaction.dao.Adao" parent="baseDao">
  27. </bean>
  28. <bean id="bDao" class="transaction.dao.Bdao" parent="baseDao">
  29. </bean>
  30. <!-- 4 service -->
  31. <bean id="aBo" class="transaction.bo.AboImpl">
  32. <property name="aDao" ref="aDao" />
  33. <property name="bBo" ref="bBo" />
  34. </bean>
  35. <bean id="bBo" class="transaction.bo.BboImpl">
  36. <property name="bDao" ref="bDao" />
  37. </bean>
  38. <!-- 5 transaction -->
  39. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  40. <property name="dataSource" ref="dataSource" />
  41. </bean>
  42. <bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor" >
  43. <property name="transactionManager" ref="transactionManager"></property>
  44. <property name="transactionAttributes">
  45. <props>
  46. <prop key="*">PROPAGATION_REQUIRED</prop>
  47. </props>
  48. </property>
  49. </bean>
  50. <bean id="autoProxy1" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  51. <property name="beanNames">
  52. <list>
  53. <value>*Bo</value>
  54. </list>
  55. </property>
  56. <property name="interceptorNames">
  57. <list>
  58. <!--
  59. <value>transactionInterceptor2</value>
  60. -->
  61. <value>transactionInterceptor1</value>
  62. </list>
  63. </property>
  64. </bean>
  65. 这里的声明事务是作用于所有以Bo为后缀的bean的所有方法上,使用REQUIRED传播方式。
  66. public int insertA(A a)
  67. {
  68. aDao.insertA(a);
  69. B b = new B();
  70. b.setName("bbb");
  71. try
  72. {
  73. bBo.insertB(b);
  74. }
  75. catch(Exception e)
  76. {
  77. System.out.println("aaa");
  78. }
  79. return 0;
  80. }
  81. 这里,insertA 开始一个事务,调用aDao.insertA(a)\[一个简单的数据库操作\],然后调用 bBo.insertB(b)\[bodao,dao直接抛异常\]bBoinsertB方法,也要开始一个事务,但是这里的传播机制是REQUIREDOK,和insertA 的事务合二为一吧。因为bBo.insertB(b)会抛异常出来,这里try-catch下,希望aDao.insertA(a)的操作能够成功。
  82. 但是现实总是残酷的,这里会有一个大大的 Transaction rolled back because it has been marked as rollback-only ,结果你会发现aDao.insertA(a)的操作也没有成功。
  83. try-catch不起作用的原因简单的说就是,try-catch的不是地方,你认为你的try-catch是最接近异常抛出点了,是第一个处理的handler了。实际上,spring在更早一步就try-catch 住了,同时还设置了一些标志位,再把catch住的异常往外抛。这个时候才是我们的try-catch。而"Transaction rolled back because it has been marked as rollback-only"就是因为事务在提交的时候,发现标志位已经被设置了,不应该去提交了,然后吭哧吭哧的回滚调,再提示你已经被设置成rollback-only了。
  84. 原因是既然如此,那么在不改变代码的情况下,依靠配置能否解决这个问题呢?使用PROPAGATION\_REQUIRES\_NEW吧。对于bBo.insertB(b)开个新的事务,如果失败了就回滚调,不影响外面的insertA不就OK了。最简单的情况就是在transactionInterceptor1前面,再加个拦截器transactionInterceptor2,该拦截器只针对insertB的事务属性进行修改。
  85. <bean id="autoProxy1" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  86. <property name="beanNames">
  87. <list>
  88. <value>*Bo</value>
  89. </list>
  90. </property>
  91. <property name="interceptorNames">
  92. <list>
  93. <value>transactionInterceptor2</value>
  94. <value>transactionInterceptor1</value>
  95. </list>
  96. </property>
  97. </bean>
  98. <bean id="transactionInterceptor2" class="org.springframework.transaction.interceptor.TransactionInterceptor" >
  99. <property name="transactionManager" ref="transactionManager"></property>
  100. <property name="transactionAttributes">
  101. <props>
  102. <prop key="insertB">PROPAGATION_REQUIRES_NEW</prop>
  103. </props>
  104. </property>
  105. </bean>
  106. 注意interceptorNames里面元素的位置。先要使用transactionInterceptor2,再使用transactionInterceptor1.因为调用insertB的时候,transactionInterceptor2先开了一个新事务,而后transactionInterceptor1融合进这个事务。如果这2个拦截器的顺序颠倒的话,那么还是会出现“Transaction rolled back because it has been marked as rollback-only”。因为,transactionInterceptor2生成事务回滚以后,还是会把ex抛给transactionInterceptor1。这个时候,transactionInterceptor1的事务和insertA的事务是同一个。transactionInterceptor1,把标志设置好,等到insertA真的结束的时候,因为异常被我们的try-catch捕获了,spring就会发现需要提交的事务具有一个已经被标记号的rollback。所以就又抛出来了。
  107. 但是如果系统有很多遗留的因素导致你不敢盲目的修改配置文件的话(比如事务的poincut),那么我们就再加一个事务proxyOK了。
  108. <bean id="autoProxy2" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  109. <property name="beanNames">
  110. <list>
  111. <value>*Bo</value>
  112. </list>
  113. </property>
  114. <property name="interceptorNames">
  115. <list>
  116. <value>transactionInterceptor2</value>
  117. <!--
  118. <value>transactionInterceptor1</value>
  119. -->
  120. </list>
  121. </property>
  122. </bean>
  123. <bean id="autoProxy1" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  124. <property name="beanNames">
  125. <list>
  126. <value>*Bo</value>
  127. </list>
  128. </property>
  129. <property name="interceptorNames">
  130. <list>
  131. <value>transactionInterceptor1</value>
  132. <!--
  133. <value>transactionInterceptor2</value>
  134. -->
  135. </list>
  136. </property>
  137. </bean>
  138. 如上的配置还是会带来悲剧的“Transaction rolled back because it has been marked as rollback-only”。
  139. 但是如果我们把 autoProxy2 放到 autoProxy1 或者给自动代理加上顺序的话,结果就正确了。
  140. <bean id="autoProxy1" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  141. <property name="beanNames">
  142. <list>
  143. <value>*Bo</value>
  144. </list>
  145. </property>
  146. <property name="interceptorNames">
  147. <list>
  148. <value>transactionInterceptor1</value>
  149. <!--
  150. <value>transactionInterceptor2</value>
  151. -->
  152. </list>
  153. </property>
  154. </bean>
  155. <bean id="autoProxy2" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  156. <property name="beanNames">
  157. <list>
  158. <value>*Bo</value>
  159. </list>
  160. </property>
  161. <property name="interceptorNames">
  162. <list>
  163. <value>transactionInterceptor2</value>
  164. <!--
  165. <value>transactionInterceptor1</value>
  166. -->
  167. </list>
  168. </property>
  169. </bean>
  170. 造成这个原因是由使用了2个代理的顺序导致的。
  171. 在做自动代理的时候,spring会按照postBeanProcessor bean声明的顺序(如果没有设置顺序的话),来依次处理bean。如果autoProxy2 autoProxy1 之前,这样transactionInterceptor2 就会更加贴近insertB的调用,其效果就像如下配置。
  172. <bean id="autoProxy1" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  173. <property name="beanNames">
  174. <list>
  175. <value>*Bo</value>
  176. </list>
  177. </property>
  178. <property name="interceptorNames">
  179. <list>
  180. <value>transactionInterceptor1</value>
  181. <value>transactionInterceptor2</value>
  182. </list>
  183. </property>
  184. </bean>
  185. 看来,Spring还是要注意bean的顺序。

文章来源:http://narcissusoyf.iteye.com/blog/710261

发表评论

表情:
评论列表 (有 0 条评论,265人围观)

还没有评论,来说两句吧...

相关阅读