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

今天在使用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等其他方法上的事务,是忽略的

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值