springmvc + mybatise 一个service 处理处理多个mapper 事物不回滚

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011567806/article/details/51189065


问题出现情况:

声明式事物@Transactional

在一个service调用了两个不同mapper 先后update两个数据,人为模拟后一个mapper异常,发现第一个mapper会提交数据。

网上有两种解决方法 第一个 方法 ,在@Transactional 调整事物传播方式从默认requared改为NESTED,并加入rollbackFor=Exception.class,修改后,无效。检查日志,发现当第一个mapper在执行完update时会按如下顺序执行:

DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d0d75] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==>  Preparing: update ss_user s_user set s_user.certsn=?,s_user.deptno=?,s_user.userName=?,s_user.freeze=? where id=?
DEBUG - ==> Parameters: null, 1(String), 要12(String), true(Boolean), 01ed2b6bee29458a88c0d54b9ab3b63a(String)
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d0d75]
DEBUG - Returning JDBC Connection to DataSource

此时,已经Returning JDBC Connection to DataSource

再执行第二个mapper的update会报异常,但是没有回滚第一次操作。


网上第二个解决办法 原文来着这里:

http://blog.csdn.net/will_awoke/article/details/12002705


主要意思是,因为web.xml中的设置,spring会默认加载bean*.xl中的容器作为父容器

 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:bean*.xml</param-value>
 </context-param>
 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springmvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

而在bean.xml中我设置了

<context:component-scan base-package="org.ezca.signsserver">
</context:component-scan>

父容器中的扫描到@trancction的bean是具有事物管理能力的,而springmvc.xml 中再次用到

<context:component-scan base-package="org.ezca" >
 </context:component-scan>

而此时,装配出来的bean是不具有事物管理能力的

,于是,子容器的bean将父容器的bean覆盖。

所以解决办法为在springmvc的扫描中加入:

 <context:component-scan base-package="org.ezca" >
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
 </context:component-scan>

将可能出现@tranction的bean过滤掉.

修改后运行效果如下:

第一个mapper

DEBUG - Creating a new SqlSession
DEBUG - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] will be managed by Spring
DEBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==>  Preparing: update ss_user s_user set s_user.certsn=?,s_user.deptno=?,s_user.userName=?,s_user.freeze=? where id=?
DEBUG - ==> Parameters: null, 1(String), 要13(String), true(Boolean), 01ed2b6bee29458a88c0d54b9ab3b63a(String)
DEBUG - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]

第二个mapper异常时:

EBUG - ooo Using Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - ==>  Preparing: insert into ss_user_cert(id,alias,crlUrl,destory,encode,invalid,oid,publicKey,sn,subject,time,type,notBefore,notafter) values(?,'','http://www.ezca.org/crl/crl8.crl',?,?,?,?,?,?,?,?,1,?,?)
DEBUG - Unable to translate SQLException with Error code '17004', will now try the fallback translator
DEBUG - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Initiating transaction rollback
DEBUG - Rolling back JDBC transaction on Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver]
DEBUG - Transaction synchronization rolling back SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@101b455]
DEBUG - Releasing JDBC Connection [jdbc:oracle:thin:@192.168.7.10:1521:VD, UserName=JOKER, Oracle JDBC driver] after transaction
DEBUG - Returning JDBC Connection to DataSource

关键词 :Rolling back JDBC transaction on Connection

于是,回滚完成。


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页