SSM中,事务控制不生效问题

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

今天在使用ssm事务时,发现方法报错,事务没有回滚,弄了半天,终于解决,下面记录一下
事务有两种处理方式:
1、编程式事务,即使用配置文件
2、声明式事务,即使用注解
我这里用的声明式事务

1、spring的配置文件

applicationContext.xml,上下文组件扫描应该排除扫描Controller

<context:component-scan base-package="cn.tisson.mos.*.*">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

2、springmvc的配置文件

springmvc.xml,上下文扫描应该只扫描controller

<context:component-scan base-package="cn.tisson.mos.*.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

3、spring-myabatis的配置文件

spring-mybatis.xml中配置事务,并把该文件插入到applicationContext.xml

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
                      proxy-target-class="true" />

问题

  • 为什么要分开配置spring 和 mvc的扫描?
因为spring的context是父子容器,ContextLoaderListener监听器加载spring配置文件,
产生的是父容器,mvc加载mvc的配置文件,产生了子容器,子容器对@Controller进行装配扫描时,
也装配了@Service注解的实例,因为在@Contoller实例依赖@Service实例,而@Service注解的实例,
也就是一般的事务控制层,应该是有父容器初始化,以保证事务的增强处理,否则会事务事务处理能力。

因此也就有了上面的两个扫描,这是最关键的问题所在


  • 要确保数据库支持事务

我试了好久,发现debug日志都已经显示事务回滚成功了,但数据库还是没有回滚,后面发现,原来是mysql数据库的引擎没有修改

在mysql数据库中,通过show engines 可以查看支持的引擎,其中只有InnoDBz才支持事务功能
修改mysql的默认引擎:
1) 在配置文件中修改,针对整个mysql生效:
在[mysqld]下加入: default-storage-engine=INNODB
2)只修改某个表的:
在命令界面输入 ALTER TABLE xxx TYPE = INNODB

注意点

  • @Transactional(readOnly = true) ,只能读,默认是readOnly = false

  • 在类上加@Transactional(readOnly = true)是全局,该类所有的public方法都有事务了,如果在方法上再加@Transactional,就是覆盖了类的事务了

  • 事务配置中<tx:annotation-driven transaction-manager="transactionManager"
    proxy-target-class="true" />

    中的proxy-target-class="true"表示支持CGLB代理,如果不加,只支持JDK的代理,默认只支持面向接口,也就是说放在实现类上,事务不生效

  • @Transactional, 应该被放在public方法上,因为spring对private,protected等其他方法上的事务,是忽略的

    扫描二维码关注公众号,回复: 5875100 查看本文章

猜你喜欢

转载自blog.csdn.net/fantasic_van/article/details/79174624