一、博主在学习到整合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. php+文件分块上传

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  2. learning memchr func

    extern void *memchr(const void *buf, int ch, size_t count);   用法:#include <string.h> 功能:从buf所指 ...

  3. 早停!? earlystopping for keras

    为了获得性能良好的神经网络,网络定型过程中需要进行许多关于所用设置(超参数)的决策.超参数之一是定型周期(epoch)的数量:亦即应当完整遍历数据集多少次(一次为一个epoch)?如果epoch数量太 ...

  4. C++类中函数(构造函数、析构函数、拷贝构造函数、赋值构造函数)

    [1]为什么空类可以创建对象呢? 示例代码如下: #include <iostream> using namespace std; class Empty { }; void main() ...

  5. python排序之冒泡排序

    def sort(list): for i in range(len(list)): for j in range(len(list) - i - 1): if list[j] < list[j ...

  6. asp.net webAPI

    Get: 1.Get参数传递的本质是url字符串拼接:2.url字符串长度受限制:3.Get参数传递在Http请求头部传递,而不支持Request-Body传递:4.Get类型的方法支持参数为基本类型 ...

  7. ios-tableViewcell展开与收缩动画处理

    [前言] 在使用华尔街见闻 app 时,看到它的 tableVeiw 上的 cell 具有很好的展开与收缩功能.于是自己想了一下实现,感觉应该挺简单的,于是心痒痒写个 demo 实现一波.华尔街见闻 ...

  8. [git]使用Idea创建一个git项目

    第一次使用git的方法,如建立的项目名叫:my-webapp   第一步:在远程gitlab上建立空白项目:my-webapp 第二步:在本地建立项目my-webapp,添加代码 第三步:创建一个本地 ...

  9. nginx.conf 配置 (反向代理,负载均衡,fastdfs model)

    #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...

  10. 自定义zabbix脚本--网卡平均流量

    自定义zabbix脚本--网卡平均流量1. 在客户端修改配置文件 /etc/zabbix/zabbix_agentd.conf需要改动两个地方:(1) UnsafeUserParameters=1(2 ...