zoukankan      html  css  js  c++  java
  • 事务配置在applicationContext.xml文件中不起作用,控制不了异常回滚

    一、博主在学习到整合ssm框架的时候,遇到一个奇葩的问题就是将 事务的控制 ,写在在applicationContext.xml文件中不起作用,在事务控制的方法中,即使出现了异常,但是事务不会回滚的坑,按道理说,我们配置了事务,在异常发生是,运行时期的异常被我们的框架捕获到,就会为我们做出回滚的操作,但是就是没有,比如博主写的一个简单的转帐的事务,第一个账户的钱被扣除了,但是在执行完扣钱之后发生了异常但是我们在数据库中却发现,钱仍然被扣了,

    博主也试过网上的大多数的方法,都是只说原因,并不能解决实际的问题,下面就说说我自己的解决的办法

    我是把配置声明式事务管理的操作放在了springmvc.xml文件中,就神奇的发现事务可以实现控制了,即在转账出现异常的时候可以实现回滚的操作了;

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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">
    
        <!--开启扫描 可以配置不扫描controller-->
        <context:component-scan base-package="com.song">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
    
        <!--配置数据库的连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="url" value="jdbc:mysql:///song?serverTimezone=GMT%2B8"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        </bean>
    
        <!--配置sqlsessionfactory工厂-->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!--配置扫描接口-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
            <property name="basePackage" value="com.song.dao"></property>
        </bean>
    
        <!--原本写在这个文件中,并不能实现事务的控制-->
        <!--配置声明式事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
    
        <!--注解配置事务-->
        <!-- <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->
    
        <!--配置事务-->
        <tx:advice id="txadvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
                <tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
            </tx:attributes>
        </tx:advice>
    
        <!--织入事务-->
        <aop:config>
            <aop:pointcut id="txpoint" expression="execution(* com.song.service.impl.*ServiceImpl.*(..))"/>
            <aop:advisor advice-ref="txadvice" pointcut-ref="txpoint"></aop:advisor>
        </aop:config>
    
    </beans>

    做法就是将事务的三大步放在springmvc.xml的文件中去

    springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation=" http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.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.song">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <!-- 开启spring对mvc的支持(配置开启映射器和适配器)-->
        <mvc:annotation-driven></mvc:annotation-driven>
    
        <!--配置视图映射器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
            <property name="suffix" value=".jsp"></property>
            <!--        这个小杠杠一定要注意-->
            <property name="prefix" value="/WEB-INF/page/"></property>
        </bean>
    
        <!--配置放行静态资源 两种方式:
        使用了springmvc框架分析:
        如果配置的DispatcherServlet的映射路径不为/时,对静态资源的请求最终会由tomcat的默认配置来处理,所以不影响静态资源的正常访问。
        如果配置的DispatcherServlet的映射路径为/时,会覆盖掉tomcat的默认的default配置,所以需要在springmvc文件中进行配置,对静态资源进行放行。
        -->
        <!--<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>-->
    
        <mvc:default-servlet-handler default-servlet-name="default"></mvc:default-servlet-handler>
    
        <!--配置拦截器拦截-->
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/account/test_interceptor"/>
                <bean class="com.song.interceptor.MyInterceptor"></bean>
            </mvc:interceptor>
        </mvc:interceptors>
    
    
        <!--配置声明式事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
    
        <!--注解配置事务-->
       <!-- <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->
    
        <!--配置事务-->
        <tx:advice id="txadvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
                <tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
            </tx:attributes>
        </tx:advice>
    
        <!--织入事务-->
        <aop:config>
            <aop:pointcut id="txpoint" expression="execution(* com.song.service.impl.*ServiceImpl.*(..))"/>
            <aop:advisor advice-ref="txadvice" pointcut-ref="txpoint"></aop:advisor>
        </aop:config>
    
    
    </beans>

    这样就解决了问题   如果有其他的而解决办法 也欢迎提出来 ,我们共同学习

    ps:记录一个错误

    Error creating bean with name 'accountController': Unsatisfied dependency expressed through field 'accountService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountServiceImpl' is expected to be of type 'com.song.service.impl.AccountServiceImpl' but was actually of type 'com.sun.proxy.$Proxy24'

    这个错误就是在controller层注入service层的bean对象时没有使用接口的类型,使用了其实现类的类型

  • 相关阅读:
    VC 捕获Windows关机事件
    JAVA多态计算面积main函数调用方法
    Spring MVC 和 Spring 总结
    css知识总结
    数据库mysql大全(高级版)
    springboot+springcloud微服务项目全套资料(笔记+源码+代码)
    eclipse上的maven,添加依赖后无法自动下载相应的jar包
    使用angularJS接收json数据并进行数据的显示
    mybatis的基本语句的应用
    Idea集成maven插件
  • 原文地址:https://www.cnblogs.com/yifachen/p/12056899.html
Copyright © 2011-2022 走看看