一、博主在学习到整合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对象时没有使用接口的类型,使用了其实现类的类型

事务配置在applicationContext.xml文件中不起作用,控制不了异常回滚的更多相关文章

  1. Hibernate中OpenSessionInViewFilter(通常配置在web.xml文件中)的作用

    Spring为我们解决Hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Sessio ...

  2. spring将service添加事务管理,在applicationContext.xml文件中的设置

    在applicationContext.xml文件中的设置为: <beans> <bean id="sessionFactory" class="org ...

  3. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

  4. ssm框架中applicationContext.xml文件中配置别名

    在applicationContext.xml中配置如下: 通过以下property标签中给定name属性value属性及对应的值,来将domain包下所有实体类设置别名. 在xxxDao.xml中 ...

  5. XML文件中CDATA的作用

    操作XML文件时,如果允许用户输入内容,例如∶"< ".">"."/".""等,当生成XML时,会破坏了XM ...

  6. web.xml文件中context-param的作用

    转 <context-param>的作用:web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件 ...

  7. ApplicationContext.xml文件详解

    想必用过Spring的程序员们都有这样的感觉,Spring把逻辑层封装的太完美了(个人感觉View层封装的不是很好).以至于有的初学者都不知道Spring配置文件的意思,就拿来用了.所以今天我给大家详 ...

  8. applicationContext.xml文件如何调用外部properties等配置文件

    只需要在applicationContext.xml文件中添加一行: <!-- 导入外部的properties文件 --> <context:property-placeholder ...

  9. 当你的Spring IOC 容器(即applicationContext.xml文件)忘记配到web.xml 文件中时

    当你的Spring IOC 容器忘记配到web.xml 文件中时,启动服务器就会报错. 部分错误如下: Caused by: org.springframework.beans.factory.NoS ...

随机推荐

  1. TTTTTTTTTTTT Gym 100818B Tree of Almost Clean Money 树连剖分+BIT 模板题

    Problem B Tree of Almost Clean Money Input File: B.in Output File: standard output Time Limit: 4 sec ...

  2. 0078 Java与MySQL时间戳传递/存储/协调问题--userLegacyDatetimeCode--userTimezone--serverTimezone

    00. 基本问题 0.0 版本: 驱动5.1.47和8.0.17 0.1 MySQL驱动5.1有userLegacyDatetimeCode和userTimezone两个参数, 8.0没有 0.2 J ...

  3. ida 下载

    链接:https://pan.baidu.com/s/1Rs2UOSfroBGf_MIkJv0cGw 提取码:kjqd 官网:https://www.hex-rays.com/updida.shtml

  4. HTTP协议对收发消息的格式要求

    每个HTTP请求和响应都遵循相同的格式. 一个HTTP包含Header和Body两部分,其中Body是可选的. HTTP响应的Header中有一个Content-Type表明响应的内容格式. 它的值如 ...

  5. 【java设计模式】-06原型模式

    原型模式简述 定义: 使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象 ,也就是通过复制现有对象实例产生新的对象,也就是所谓的"克隆" 实现方式: 1.实现Cl ...

  6. HDU 2176 取(m堆)石子游戏 —— (Nim博弈)

    如果yes的话要输出所有情况,一开始觉得挺难,想了一下也没什么. 每堆的个数^一下,答案不是0就是先取者必胜,那么对必胜态显然至少存在一种可能性使得当前局势变成必败的.只要任意选取一堆,把这堆的数目变 ...

  7. response 下载文件火狐浏览器文件名乱码问题

    string path = Server.MapPath(Url.Content("~/") + "UploadFiles/Template/");       ...

  8. How to do Deep Learning on Graphs with Graph Convolutional Networks

    翻译: How to do Deep Learning on Graphs with Graph Convolutional Networks 什么是图卷积网络 图卷积网络是一个在图上进行操作的神经网 ...

  9. 微信公众号【黄小斜】和【Java技术江湖】

  10. Eratos筛法(筛选素数)

    对于n以内的非素数必有k*n1=n(n1<n)  所以 可有p1,2p2,3p3把非素数筛选掉 实现代码: #include<iostream> #include<string ...