事务配置在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 ...
随机推荐
- MySql大小写配置
新安装mysql5.7版本后,linux环境下默认是大小写敏感的.可以在客户端执行以下命令: SHOW VARIABLES LIKE '%case%' 可以看到 lower_case_table_na ...
- learning armbian steps(8) ----- armbian 源码分析(三)
在lib/main.sh当中 ) == main.sh ]]; then echo "Please use compile.sh to start the build process&quo ...
- putty ssh 证书登录及问题
1.用PUTTYGEN.exe生成密钥,生成的时候鼠标在进度条下面的空白处移动,为什么?就理解成随机得厉害点吧. 2.保存私钥,请看下面的图片说明 3.把公钥的内容想办法放到用户目录的.ssh/aut ...
- Java中String、StringBuffer、StringBuilder
String 对象 String 创建机制 String 是 Java 语言中非常基础和重要的类,提供了构造和管理字符串的各种基本逻辑,由源码可知,它是典型的 Immutable (不可变)类,被fi ...
- Navicat连接的某个表一直加载并且不能关闭
问题: 今天下午突然发现数据库的一张表一直加载,也出不来数据,并且也不能关闭.解决办法: 在Navicat中中执行如下命令: SHOW PROCESSLIST; 如果state列中有lock字眼,通过 ...
- 【BZOJ3098】 Hash Killer II
BZOJ3098 Hash Killer II Solution 这道题目好像题面里面给了提示(当然没给就有点难想了.) 曾经讲过一个叫做生日悖论的,不知道还有多少人记得 考虑相同的可能性大概是\(\ ...
- Visual Studio Code(VS code)介绍
一.日常安利 VS code VS vode特点: 开源,免费: 自定义配置 集成git 智能提示强大 支持各种文件格式(html/jade/css/less/sass/xml) 调试功能强大 各种方 ...
- Django环境搭建win(二)
Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6,17年4月4号 已经发布(长期支持版本 LTS) win7下: 一.使用pip安装 1.首先要安装pip 2.p ...
- linux下查看tomcat的日志
工作期间有碰到服务器日志相关的,需要看tomcat运行日志,简单搜了下,摘为随笔,以供参考 一种是利用docker查看 1.使用dockerdocker logs -f -t --since=&quo ...
- Flutter移动电商实战 --(12)首页导航区域编写
1.导航单元素的编写 从外部看,导航是一个GridView部件,但是每一个导航又是一个上下关系的Column.小伙伴们都知道Flutter有多层嵌套的问题,如果我们都写在一个组件里,那势必造成嵌套严重 ...