SSM项目配置事务不生效解决办法

avatar 2021年08月07日22:23:44 6 2379 views
博主分享免费Java教学视频,B站账号:Java刘哥

今天帮一个朋友解决事务不生效的问题,这里总结下主要原因。

 

事务失效有几个常见原因解决办法

1)数据库表需要设置使用 INNODB 引擎,直接用Navicat修改,或者用命令

ALTER TABLE 表名 ENGINE = InnoDB;

2)加事务注解的方法不能使用 private 

3)确保扫包如下

spring-mvc.xml扫包 <context:component-scan base-package="com.ssm.blog.controller" />

spring-mybatis.xml扫包 <context:component-scan base-package="com.ssm.blog" />

这个地方比较关键

 

完整配置如下

spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.ssm.blog" />


    <!--加载db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--配置druid连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <property name="driverClassName" value="${mysql.driver}" />
        <property name="url" value="${mysql.url}" />
        <property name="username" value="${mysql.username}" />
        <property name="password" value="${mysql.password}" />

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

        <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
        <property name="filters" value="stat" />
    </bean>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载mybatis全局配置文件-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <!--mapper.xml所在位置-->
        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml" />
        <!--指定需要使用别名的PO类所在的包-->
        <property name="typeAliasesPackage" value="com.liuyanzhao.ssm.blog.entity" />
    </bean>

    <!--mapper扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--如果需要扫描多个包,中间使用半角逗号隔开-->
        <property name="basePackage" value="com.ssm.blog.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>



    <!-- 对mybatis操作数据事务控制,spring使用jdbc的事务控制类 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源dataSource在spring-mybatis.xml中配置了 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 通知 事务管理-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- <tx:method name="save*" propagation="REQUIRED"/>
        其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。
        propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 -->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--  配置参与事务的类 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssm.blog.service.impl.*.*(..))"/>
    </aop:config>


</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    <context:component-scan base-package="com.ssm.blog.controller" />

    <!-- 一个配置节解决映射器和适配器的配置注解配置   自动转换json-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 配置视图解析器
        进行jsp解析,默认使用jstl标签,classpath下得有jstl的包
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <!--配置前缀和后缀,也可以不指定-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--&lt;!&ndash;文件上传&ndash;&gt;-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--设置上传最大尺寸为50MB-->
        <property name="maxUploadSizePerFile" value="52428800"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="resolveLazily" value="true"/>
    </bean>



    <!-- 静态资源映射 -->
    <mvc:resources mapping="/css/**" location="/resource/assets/css/"></mvc:resources>
    <mvc:resources mapping="/js/**" location="/resource/assets/js/"></mvc:resources>
    <mvc:resources mapping="/img/**" location="/resource/assets/img/"></mvc:resources>
    <mvc:resources mapping="/plugin/**" location="/resource/assets/plugin/"></mvc:resources>


    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.ssm.blog.interceptor.HomeResourceInterceptor"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/admin"/>
            <bean class="com.ssm.blog.interceptor.SecurityInterceptor"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/admin/**"/>
            <bean class="com.ssm.blog.interceptor.SecurityInterceptor"/>
        </mvc:interceptor>

    </mvc:interceptors>
</beans>

 

 

 

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0