在一个项目中发现spring的事务无法回滚。

DEBUG: org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49f9ce55] was not registered for synchronization because synchronization is not active

DEBUG: org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [ConnectionHandle{url=jdbc:postgresql://localhost/mypro, user=mypro, debugHandle=null, lastResetAgoInSec=92, lastUsedAgoInSec=92, creationTimeAgoInSec=92}] will not be managed by Spring

在网上找了好多,都没解决

我搜到的资料相关链接有:

http://www.cnblogs.com/xunux/p/4388124.html

http://www.iteye.com/topic/1123069

http://hwak.iteye.com/blog/1611970

http://blog.csdn.net/will_awoke/article/details/12002705 (最终在这里得到启发,问题解决)

其实,上面几个链接,都提到是包扫描的问题,要在包扫描的配置里加上

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>

没错,真的是这个问题引起的。

起初,我已经加上了,但问题依旧,问题研究出在哪里呢?????:( :( :((伤心啊!!!)

后来找到这里:http://blog.csdn.net/will_awoke/article/details/12002705,里面有讲到:

Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service,因为在多上下文的情况下,如果同一个bean被定义两次,后面一个优先。

好了,问题关键是父容器和子容器的事了。

我细心检查项目中的web.xml配置

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
classpath:/applicationContext-*.xml
classpath:/applicationContext-mypro.xml
classpath:/applicationContext-core.xml
</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> (说明:这个是产生父容器的,这与上面context-param,contextConfigLocation有关系)
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> (说明:这个是产生子容器的,即springmvc的ContextApplication是子容器)
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

我的项目是maven构建,项目分为mypro-web,和mypro-core两个模块,其中mypro-web依赖mypro-core模块,mypro-core最终打成jar包,放在mypro-web的lib下面。

原因就是我的mypro-web和mypro-core中都有applicationContext-mypro.xml这个配置文件(这两个文件里面的东西是一样的),这个文件里就配了事务相关的东西。

<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 支持注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

所以,我猜这导致spring在构建事务声明时,出问题了,

把mypro-web中的applicationContext-mypro.xml文件删除,问题解决!!!

spring + mybatis 注解式事务不回滚的原因分析 @Transactional的更多相关文章

  1. Spring 16: SM(Spring + MyBatis) 注解式事务 与 声明式事务

    Spring事务处理方式 方式1:注解式事务 使用@Transactional注解完成事务控制,此注解可添加到类上,则对类中所有方法执行事务的设定,注解添加到方法上,则对该方法执行事务处理 @Tran ...

  2. springmvc注解式事务手动回滚

    Spring的AOP事务管理默认是针对unchecked exception回滚(运行期异常,Runtime Exception),如果希望手动控制事务的回滚,可以通过 TransactionAspe ...

  3. Spring AOP声明式事务异常回滚(转)

    转:http://hi.baidu.com/iduany/item/20f8f8ed24e1dec5bbf37df7 Spring AOP声明式事务异常回滚 近日测试用例,发现这样一个现象:在业务代码 ...

  4. spring中注解式事务不生效的问题

    常用的解决方法可以百度,我针对我的问题描述一下 Mysql中InnoDB引擎才支持事务, MyISAM不支持事务. 当你尝试了各种方法解决spring中注解式事务不生效时, 一定要查看一下数据库中表的 ...

  5. springmvc mybatis 声明式事务管理回滚失效,(checked回滚)捕捉异常,传输错误信息

    一.知识点及问题 后端框架: Spring .Spring mvc .mybatis 业务需求: client先从服务端获取用户大量信息到client,编辑完毕之后统一Post至服务端,对于数据的改动 ...

  6. Spring AOP声明式事务异常回滚

    近日测试用例,发现这样一个现象:在业务代码中,有如下两种情况,比如:throw new RuntimeException("xxxxxxxxxxxx"); 事物回滚throw ne ...

  7. MySql事务无法回滚的原因

    使用MySQL时.假设发现事务无法回滚,但Hibernate.Spring.JDBC等配置又没有明显问题时.不要苦恼,先看看MySQL创建的表有没有问题.即表的类型. InnoDB和MyISAM是在使 ...

  8. Spring aop 影响本地事务的回滚总结

    1  @Before   不会,因为还没执行到service的业务逻辑 2  @ After    默认情况下,报错会影响事务回滚., 当设置@Order属性并设置值优先级大小, 即使报错也不会回滚了 ...

  9. Java事务不回滚的原因总结

    1.首先要检查数据的引擎,InnoDB支持事务,MyIsam不支持事务 2.  默认spring事务只在发生未被捕获的 runtimeexcetpion时才回滚.     spring aop  异常 ...

随机推荐

  1. Beaglebone Back学习二(功能测试)

    开发板测试 买到开发板后,首先要测试一下板子的功能,这个可以参考官方的步骤(Getting Started)进行. 1 首先下载最新的镜像文件http://beagleboard.org/latest ...

  2. sql之解决数据库表的循环依赖问题

    三张数据表关系如图: 其实出现表循环依赖情况:就是 同时  ConfigCompany和 Department 也有依赖,就会报错,说有循环依赖,就会报错. 所以 这个时候的解决办法:可以改 在数据库 ...

  3. CocoaPods - 在 Mac 中的生与死

    1. 集成 Cocoapod: 1.1 安装 ruby环境 mac 系统默认有 Ruby 环境, 通过 $ ruby -v 可以查看当前的 Ruby 版本.  如果 Ruby 版本过低可以通过 rvm ...

  4. 进入 App Store 打分

    很多用户用了好软件后忘记或嫌麻烦而不去 App Store 进行打分评星,为此开发者可以在应用中加入打分按钮,点击后直接跳转到 App Store 的评分界面. App Store 上评论的链接地址是 ...

  5. Linux桌面扩展 Docky

    Docky是从GNOME Do项目剥离出来的一个Dock软件,最初即为GNOME Do的“Docky”模式. 后来开发者觉得,GNOME Do是个快速启动工具,弄个Docky模式有点不伦不类,于是,D ...

  6. IE 坑爹的浏览器兼容模式

    作为作为Web的前端开发人员,最悲催的莫过于要不断的,不断的去调试各种浏览器的显示效果,个人比较喜欢用火狐浏览器来做开发和调试,对于不怎么懂CSS的我来说,IE的样式调整一看就头大了.对于没有美工的团 ...

  7. fzu 2105 Digits Count ( 线段树 ) from 第三届福建省大学生程序设计竞赛

    http://acm.fzu.edu.cn/problem.php?pid=2105 Problem Description Given N integers A={A[0],A[1],...,A[N ...

  8. datagridview 右键选中行 并弹出菜单

    private void dataGridView_OLUsers_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { i ...

  9. leetcode2 Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

  10. 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中

     前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...