事务配置在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对象时没有使用接口的类型,使用了其实现类的类型
事务配置在applicationContext.xml文件中不起作用,控制不了异常回滚的更多相关文章
- Hibernate中OpenSessionInViewFilter(通常配置在web.xml文件中)的作用
Spring为我们解决Hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Sessio ...
- spring将service添加事务管理,在applicationContext.xml文件中的设置
在applicationContext.xml文件中的设置为: <beans> <bean id="sessionFactory" class="org ...
- Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)
<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...
- ssm框架中applicationContext.xml文件中配置别名
在applicationContext.xml中配置如下: 通过以下property标签中给定name属性value属性及对应的值,来将domain包下所有实体类设置别名. 在xxxDao.xml中 ...
- XML文件中CDATA的作用
操作XML文件时,如果允许用户输入内容,例如∶"< ".">"."/".""等,当生成XML时,会破坏了XM ...
- web.xml文件中context-param的作用
转 <context-param>的作用:web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件 ...
- ApplicationContext.xml文件详解
想必用过Spring的程序员们都有这样的感觉,Spring把逻辑层封装的太完美了(个人感觉View层封装的不是很好).以至于有的初学者都不知道Spring配置文件的意思,就拿来用了.所以今天我给大家详 ...
- applicationContext.xml文件如何调用外部properties等配置文件
只需要在applicationContext.xml文件中添加一行: <!-- 导入外部的properties文件 --> <context:property-placeholder ...
- 当你的Spring IOC 容器(即applicationContext.xml文件)忘记配到web.xml 文件中时
当你的Spring IOC 容器忘记配到web.xml 文件中时,启动服务器就会报错. 部分错误如下: Caused by: org.springframework.beans.factory.NoS ...
随机推荐
- pypdf2:下载Americanlife网页生成pdf合并pdf并添加书签
初步熟悉 安装 pip install pypdf2 合并并添加书签 #!/usr/bin/env python3.5 # -*- coding: utf-8 -*- # @Time : 2019/1 ...
- MFC 模态对话框、非模态对话框
modal dialogs与modeless dialogs,modal dialogs会让原窗体不被选中,modeless dialogs原窗体依然可以选中. 1.模态对话框的显示 DlgModal ...
- 分布式hadoop 架构图
zk:zookeeper NN:namenode DN:datanode ZF Failover Controller:简称zkfc ,是zk的客户端,只运行在主备的namenode上,主要作用是判断 ...
- Codevs 1404 字符串匹配(Kmp)
1404 字符串匹配 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你两个串A,B,可以得到从A的任意位开始的子串和B匹配的 ...
- Selenium报错:StaleElementReferenceException
一个学生在操作页面跳转时遇到一个Selenium报错, 如下图所示: StaleElementReferenceException: Message: stale element reference: ...
- python 生成金字塔
num = eval(input("请输入一个整数:")) , num + ): , -): print(" ", end="\t") , ...
- python_re模块
正则表达式:http://www.regexlab.com/zh/regref.htm
- AGC032E modulo pairing
题意 原题 给出\(2n\)个\(\leq m\)的数,求最优的两两配对方案 使\(n\)组\((x,y)\)的\((x+y)mod \space m\)最大值最小 \(n\leq 10^5,m \l ...
- IO之复制文件的四种方式
1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是 ...
- AngularJS复习小结
开发移动端App,首先得在头部 <meta name="viewport" content="width=device-width, initial-scale=1 ...