springMvc+mybatis事务管理不起作用解决

来源:互联网 发布:linux ping 超时时间 编辑:程序博客网 时间:2024/06/16 09:16

我遇到的问题是事务不起作用,虽然多次数据库操作中有异常出现,但还是部分提交,并没有回滚;具体的解决方式见下:

1.注意抛出异常,配置正确

<tx:method name="add*" propagation="REQUIRED"   rollback-for="java.lang.Exception"/>


2.由于采用的是SpringMVC、 MyBatis,故统一采用了@Service、@Controller注解来声明Service和Controller 组件,于是我在Spring和SpringMVC的配置文件均用一行代码配置了Spring的组件自动扫描。
<context:component-scan base-package="com.pobo.info" />

而由于服务器启动时的加载Spring相关配置文件的顺序为applicationContext.xml(Spring的配置文件) ---> applicationContext-mvc.xml(SpringMVC的配置文件),按照上面的配置Spring加载applicationContext.xml配置文件时会加载@Controller注解标注的Controller组件,并且对其进行扫描装配,但是此时的Service还没有进行事务增强处理,得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力),所以我们应用在applicationContext.xml(Spring配置文件)中不扫描Controller,而在applicationContext-mvc.xml(SpringMVC配置文件)中不扫描Service。修改如下:
a.spring-context.xml配置文件

<context:component-scan base-package="com.pobo.info">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan>

b.spring-mvc.xml配置文件

<!-- 扫描所有的controller 但是不扫描service-->  
<context:component-scan base-package="com.pobo.info">  
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>  
</context:component-scan>






原创粉丝点击