在整合springMVC+ibatis+spring框架时采用的是声明式事务,代码写完后故意测试了一下事务是否生效,写了一个测试方法:
public int[] delAndUpdate() {
int a = testDao.delCart();
int c = testDao.insertCart();
int b = testDao.updateCart();
int[] count = new int[] { a, c, b };
return count;
}
有增删改三种操作类型,最后一个update操作时候故意把sql写错,在执行时候就会抛出异常,再通过查看数据库看前两条数据是否删除和插入,杯具的是事务竟然没回滚,以为配置写的有问题
<!--声明式事务控制 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 指定事务切入点 -->
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* cn.myshop.service.*.*(..))" />
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 读取数据方法,一般采用只读事务-->
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<!--以下方法,如save,update,insert等对数据库进行写入操作的方法,当产生Exception时进行回滚 -->
<tx:method name="insert*" />
<tx:method name="update*" />
<tx:method name="save*" />
<tx:method name="add*" />
<tx:method name="create*" />
<tx:method name="del*" />
<tx:method name="remove*" />
<tx:method name="batchCommit*" />
<!--2015-03-11,修改为抛出异常都进行回滚 -->
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
经仔细查看和网上的配置对比,发现没什么问题,实在想不到什么地方有问题,之后google了半天,造成声明式事务无效不回滚的原因主要有几个(事务配置错了就不说了):
1、由于数据库为mysql,网上有说法是,
mysql默认存储引擎为MyISAM是不支持事务的,
需要设置为InnoDB模式,通过show engines; 命令看到
InnoDB已经是默认的存储引擎,查看操作的表
也为InnoDB模式,又看了my.ini中的配置
default-storage-engine=INNODB
.......
#skip-innodb
都没问题,看来是其它地方的问题了。
2、另一种说法,不太相信,说是Spring的声明式事务需要抛出RuntimeException后才会触发事务的回滚,是这样吗,添加抛出RuntimeException依然没回滚,对于抛出RuntimeException事务才生效说法只能是一连串疑问了。
3、英语不杂地,官方文档也只能沉睡了,无奈继续google,最后在iteye上看到一帖子同为事务不生效
http://www.iteye.com/topic/1123069
其中有一兄弟的回帖:
1.root-context.xml
<!-- 不扫描带有@Controller注解的类。因为这些类已经随容器启动时,在servlet-context中扫描过一遍了 -->
<context:component-scan base-package="com.kimho">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2、servlet-context.xml:
<!-- 扫描业务组件,让spring不扫描带有@Service注解的类(留在root-context.xml中扫描@Service注解的类),防止事务失效 -->
<context:component-scan base-package="com.kimho">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
同样我也使用的spring注解+springMVC注解,默认情况下spring应该先加载applicationContext.xml,之后再加载springMVC-servlet.xml,按那兄弟说法可能会造成事务失效。
我的springMVC-servlet.xml配置如下
<mvc:annotation-driven />
<context:component-scan base-package="cn.myshop"/>
而applicationContext.xml没有再配置component-scan,本以为spring和springMVC同根生,使用同一个component-scan即可,没想到还相煎甚急,那就修改配置测试一下。springMVC-servlet.xml修改为:
<mvc:annotation-driven />
<context:component-scan base-package="cn.myshop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
applicationContext.xml中添加
<context:component-scan base-package="cn.myshop"/>
重启tomcat,测试
事务正常回滚,还真是这个原因,另外也不需要抛出RuntimeException。
看来如果项目采用spring、 springMVC的注解方式这个地方确实该注意,如果使用其它架构或者不使用注解的方式,应该就不会有这样的问题。
附加:使用powerdedigner生成表建表sql后,执行到mysql,用nvaicat for mysql查看表字段的中文注释乱码了,而直接用navicat修改字段注释不乱码,powerdesigner导出时也选择的为utf-8,看来是mysql在执行脚本时乱码了,查看mysql的my.in文件发现默认编码为default-character-set=latin1 不知什么编码,那就改为default-character-set=utf8,重启服务再执行脚本,没有乱码了。 (注意default-character-set=latin1 共有两个地方,都改为utf8)
- 大小: 29.7 KB
- 大小: 57 KB
分享到:
相关推荐
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...
基于Spring+SpringMVC+Mybatis架构的博客系统:博客管理、图表数据、日志分析、访问记录、图库管理、资源管理、友链通知等。良好的页面预加载,无限滚动加载,文章置顶,博主推荐等。提供 用户端+管理端 的整套系统...
使用mybatis+spring+springMVC+maven+dubbo搭建的一个消费者与提供者的项目
基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...
Spring+SpringMVC+Hibernate+maven项目,适用初学者学习。
Spring+SpringMVC+Mybatis+Maven+bootstrap+ajax+jQuery整合开发简单的员工后台管理系统,免责声明,朋友分享,切勿商用
spring+springmvc+mybatis搭建的一个酒店管理系统附带mysql数据库 spring+springmvc+mybatis搭建的一个酒店管理系统附带mysql数据库
Spring+SpringMvc+Mybatis学生管理系统,含登录,数据库。
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis).pdfSSM框架——详细整合教程(Spring+SpringMVC+MyBatis).pdf
基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场...
酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统...
使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBat.
基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+...
与我的博客理解Spring+SpringMVC+Hibernate开发流程,附一定源码(一)配套的用于理解Spring+SpringMVC+Hibernate开发的一个简单的小例子。
Spring+SpringMVC+Mybatis框架整合例子——亲测可用.zip
spring+springMVC+Hibernate整合
Spring+SpringMVC+Mybatis+Velocity+Maven整合
Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA
案例包括spring+springMVC+hibernate的完整增删改查,并且附上前端技术