内容源自:Spring中如何使用事务?

一、为什么要使用事务?

如果我们一个业务逻辑只执行一次sql,是不需要使用事务的。但如果要执行多条sql语句才能完成一个业务逻辑的话,这个时候就要使用事务了。 因为这几条sql可能有的执行成功,有的执行失败。 而事务就是对一组sql语句进行统一的提交或回滚操作,为了保证数据执行的一致性,这组sql不是全部成功就是全部失败。

举个例子吧: 
我们要实现转账的功能。首先要在账户a中扣100元,然后在账户b中加100元,这里涉及两次sql操作。大概像下面代码这样操作:

boolean flag1 = accountDao.minus(map)==1;
            if(flag1){
                // 减成功
                map.put("toAccount", toAccount);
                boolean flag2 = accountDao.plus(map)==1;
                if(flag2){
                    // 加成功
                    return true;
                }else{
                    /** 加失败 [减生效了] 回滚
                     *  为这个转账的过程添加事务控制
                    // 自定义异常
                    throw new PayException();
                }
            }else{
                // 减失败
                return false;
            }

如果账户a减钱成功,账户b加钱失败(比如账户输入错误,数据库中没有这个账户,sql执行失败), 这里显然要把a账户减的钱还回来。即回滚。所以在这里需要添加事务控制。

二.如何在spring中添加事务

  • spring中的事务 作为 局部事务 , 配置方式依赖于持久层使用的技术

spring整合jdbc 
spring整合hibernate

  • spring中即提供了编程式事务的管理方式,也提供了声明式事务的管理方式:

编程式事务 TransactionTemplate

模板类 将事务的管理过程封装模板类中】

声明式事务 AOP
提供根接口 PlatFormTransactionManager 事务管理器接口 
用于描述spring中事务如何管理[如何创建、如何提交、如何回滚] 】

  • 声明式事务

底层采用AOP技术实现,将事务管理过程(创建、提交、回滚)封装在一个事务通知bean[AfterAdvice ThrowsAdvice]中; 
通过在ioc容器中配置切入点的方式,将这个事务通知bean提供的事务管理功能引用给需要事务的核心业务逻辑方法(DAO)

在容器中添加配置如下:(这里orm用的mybatis) 
applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:component-scan base-package="com"/>

    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/etoak"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="ds"></property>
        <property name="mapperLocations">
            <list>
                <value>classpath:com/etoak/dao/AccountDaoIf-mapper.xml</value>
            </list>
        </property>
    </bean>

    <bean id="accountDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="ssf"></property>
        <property name="mapperInterface" value="com.etoak.dao.AccountDaoIf"></property>
    </bean>

    <!--
        配置声明式事务!
        1 aop命名空间、schema文件
          tx命名空间、schame文件(用于提供声明式事务的支持)
        2 配置事务通知bean [Advice]
        [before after around throws]
            管理事务的使用方式[创建、提交、回滚]
        transaction-manager: 设置事务管理器
        事务管理器 PlatFormTransactionManager
                - DataSourceTransactionManager
     -->
    <tx:advice id="tran" transaction-manager="tm">
        <tx:attributes>
            <!--
                tx:method 为不同类型的方法添加不同的事务属性
             -->
            <tx:method name="pay"
                isolation="DEFAULT"
                propagation="REQUIRED"
                read-only="false"
                timeout="-1"
                rollback-for="com.etoak.util.PayException"/>
            <tx:method name="add*"/>
            <tx:method name="del*"/>
            <tx:method name="sel*" read-only="true"/>
            <tx:method name="update*" timeout="10"/>
        </tx:attributes>
    </tx:advice>

    <!--
        配置事务管理器bean
     -->
    <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"></property>
    </bean>

    <!--
        3 配置切入点
            描述需要将通知[tran]提供的功能[事务管理功能]引用给哪个/哪些方法
     -->
    <aop:config>
        <aop:pointcut
            expression="execution(* com.etoak.service.*.*(..))"
            id="pc"/>
        <aop:advisor advice-ref="tran" pointcut-ref="pc"/>
    </aop:config>
</beans>

配置好了,还要写一个异常类,在需要事务管理的地方抛出自己定义的异常,从而启用事务。

public class PayException extends Exception{
    @Override
    public String getMessage() {
        return "减操作成功,加操作失败,失败原因:没有这个账户.转账失败,我要回滚";
    }
}

在web.xml中添加异常页面配置:

  <error-page>
    <exception-type>com.etoak.util.PayException</exception-type>
    <location>/error.jsp</location>
  </error-page>

方法里面添加事务管理:

public class PayService {

    @Autowired
    private AccountDaoIf accountDao;

    // 提供转账服务
    public boolean pay(String fromAccount , String toAccount , Double money)throws Exception{

        // 调用DAO
        double yue = accountDao.查询余额(fromAccount);
        if(yue>=money){
            Map map = new HashMap();
            map.put("fromAccount", fromAccount);
            map.put("money" , money);
            boolean flag1 = accountDao.minus(map)==1;
            if(flag1){
                // 减成功
                map.put("toAccount", toAccount);
                boolean flag2 = accountDao.plus(map)==1;
                if(flag2){
                    // 加成功
                    return true;
                }else{
                    /** 加失败 [减生效了] 回滚
                     *  为这个转账的过程添加事务控制
                     *  添加声明式事务
                     *  以AOP的方式进行添加
                     *  将事务的管理过程封装在一个事务通知bean
                     *  以配置、引用的方式将事务通知bean添加给需要事务的方法
                     *  切入点指向的是哪 : 服务层中Service对象提供的pay()
                     *
                     *  声明式事务回滚的方式:检测到异常时
                     */
                    // 自定义异常
                    throw new PayException();
                }
            }else{
                // 减失败
                return false;
            }
        }else{
            return false;
        }
    }
}

Spring框架学习(10)Spring中如何使用事务?的更多相关文章

  1. Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建

    上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...

  2. Spring框架学习02——Spring IOC 详解

    1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...

  3. Spring框架学习03——Spring Bean 的详解

    1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...

  4. Spring 框架学习(1)--Spring、Spring MVC扫盲

    纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...

  5. Spring框架学习10——JDBC Template 实现数据库操作

    为了简化持久化操作,Spring在JDBC API之上提供了JDBC Template组件. 1.添加依赖 添加Spring核心依赖,MySQL驱动 <!--Spring核心基础依赖--> ...

  6. Spring框架学习1

    AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理   Spring框架学习(一)   阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...

  7. Spring框架学习之IOC(二)

    Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...

  8. Spring框架学习笔记(5)——Spring Boot创建与使用

    Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...

  9. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

  10. spring框架学习(三)junit单元测试

    spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...

随机推荐

  1. Jmeter----读取excel表中的数据

    Jmeter 读取excel数据使用的方法是使用CSV Data Set Config参数化,之后使用BeanShell Sampler来读取excel表中的数据 第一步.查看所需的接口都要哪些字段和 ...

  2. 寻找与网页内容相关的图片(二)reddit的做法

    正如前文所述,内容聚合网站,比如新浪微博.推特.facebook等网站对于网页的缩略图是刚需.为了让分享的内容引人入胜,网页的图片缩略图是必不可少的.年轻人的聚集地.社交新闻网站reddit也是一个这 ...

  3. Java学习笔记(八)——java多线程

    [前面的话] 实际项目在用spring框架结合dubbo框架做一个系统,虽然也负责了一块内容,但是自己的能力还是不足,所以还需要好好学习一下基础知识,然后做一些笔记.希望做完了这个项目可以写一些dub ...

  4. PHP7 微信支付回调失败 解决

    升级完PHP7 发现微信支付回调失败.原来是 $GLOBALS['HTTP_RAW_POST_DATA'];没有定义的问题.php7 移除了这个全局变量. 问题代码如下: 微信API :WxPay.A ...

  5. Nodejs Mocha测试学习

    参考大神阮一峰的文章<测试框架 Mocha 实例教程> 目前在使用Nodejs,但写完的程序很容易出错,那怎么办?需要引入单元测试去做基础的测试 目前Nodejs用来做单元测试的工具接触的 ...

  6. C# 推送到极光

    https://docs.jiguang.cn/jpush/resources/ 下载后有完整的例子 引用 Jiguang.JPush.dll using System; using Jiguang. ...

  7. Python开发基础-Day14正则表达式和re模块

    正则表达式 就其本质而言,正则表达式(或 re)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...

  8. Xamarin中VS无法连接Mac系统的解决办法

    Xamarin中VS无法连接Mac系统的解决办法 按照以下步骤排查:(1)确认Mac系统中安装Xamarin.iOS开发必备的组件,如Mono.Xamarin.iOS.(2)将Windows和Mac下 ...

  9. coreseek 段错误 (core dumped) 问题

    coreseek建立索引出现上面问题经过测试发现有下面几个原因: 1. 分词配置文件不存在  uni.lib 2. uni.lib配置文件格式不正确

  10. 【BZOJ 1528】 1528: [POI2005]sam-Toy Cars (贪心+堆)

    1528: [POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到 ...