Spring提供两种事务方式:编程式和声明式(重点)

前者需要手写代码,后者通过配置实现。

事务的回顾:

  • 事务:逻辑上的一组操作,组成这组事务的各个单元,要么全部成功,要么全部失败

事务的特性:ACID

  • 原子性(Atomicity):事务不可分割
  • 一致性(Consistency):事务执行前后数据完整性保持一致
  • 隔离性(Isolation):一个事务执行不应该受到其他事务的干扰
  • 持久性(Durability):一旦事务结束,数据就持久化到数据库

如果不考虑隔离性引发安全问题:

读问题:

  • 脏读:一个事务读到另一个事务未提交的数据
  • 不可重复读:一个事务读到另一个事务已经提交的update数据,导致另一个事务中多次查询结果不一致
  • 虚读、幻读:一个事务读到另一个事务已经提交的insert数据,导致另一个事务中多次查询结果不一致

解决读问题:

设置事务的隔离级别:开发常用二三。非常安全,效率低。

  • Read uncommitted: 未提交读,任何读问题解决不了
  • Read Committeed  : 已提交读, 解决脏读,但是不可重复读和虚读有可能发生。Oracle
  • Repeatable read   : 重复读,解决脏读和不可重复读,但是虚读有可能发生。mysql
  • Serializable:      解决所有读问题

写问题:

  • 丢失更新

事务管理的API:

PlatformTransactionManager:平台事务管理器

  • DataSourceTransactionManager:底层是JDBC管理事务
  • HibernateTransactionManager:底层是Hibernate管理事务

TransactionDefinition:事务定义信息

  • 事务定义:用于定义事务的相关信息,隔离级别,超时信息、传播行为、是否只读。
  • TransactionStates:事务的状态,用于记录在事务管理过程中事务的状态的对象。

TransactionStates:事务的状态

  • 事务状态:用于记录在事务管理过程中,事务的状态的对象。

事务管理的API的关系:

Spring进行事务管理的时候,首先是平台事务管理器根据事务定义信息进行事务的管理。

在事务管理过程中,产生各种状态,将这些状态信息记录到事务状态的对象中。

Spring事务的传播行为:主要解决事务在业务层方法相互调用的问题

主要理解红色部分,一般用默认

Spring的事务管理:

搭建事务管理的环境,转账案例

创建Service的接口和实现类

创建DAO的接口和实现类

配置Service和Dao:交给Spring管理

配置连接池和JDBC模板:

测试类:

Spring的事务管理:编程式(了解)和声明式(重点)

一类:编程式事务(需要手动编写代码,不用重点记)

第一步:配置平台事务管理器

第二步:配置事务管理的模版

第三步:编写事务管理的代码

第四步:测试:

 tx.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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">

     <!--(常用) 第二种方式:通过context标签引入 -->
     <context:property-placeholder location="classpath:jdbc.properties"/>

     <!-- 配置C3P0连接池 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="${jdbc.driverClass}"/>
       <property name="jdbcUrl" value="${jdbc.url}"/>
       <property name="user" value="${jdbc.user}"/>
       <property name="password" value="${jdbc.password}"/>
     </bean>

    <!-- 继承JdbcDaoSupport类,里面有set方法和连接池,配置SpringJDBC模版 -->
<!--     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>  -->

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

    <!-- 配置事务管理的模版===== -->
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
       <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <!-- 配置DAO -->
    <bean id="accountDao" class="com.spring4tx.demo1.AccountDaoImpl">
     <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置Service -->
    <bean id="accountService" class="com.spring4tx.demo1.AccountServiceImpl">
    <!-- 注入事务管理模版 -->
    <property name="transactionTemplate" ref="transactionTemplate"/>
    </bean>

</beans>

二类:声明式管理(通过配置实现)---AOP

  • xml方式
  • 注解方式

xml方式:

1 引入jar包

2 恢复转账环境

 3 配置事务

配置事务管理器:

配置事务增强:

aop配置:增强注解到目标类

4 测试:

 tx2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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">

     <!--(常用) 第二种方式:通过context标签引入 -->
     <context:property-placeholder location="classpath:jdbc.properties"/>

     <!-- 配置C3P0连接池 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="${jdbc.driverClass}"/>
       <property name="jdbcUrl" value="${jdbc.url}"/>
       <property name="user" value="${jdbc.user}"/>
       <property name="password" value="${jdbc.password}"/>
     </bean>

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

    <!-- 配置事务的增强 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- 事务管理的规则, propagation : 传播行为 -->
<!--        <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
       <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
       <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
       <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT"/>     -->
       <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
    </tx:attributes>
    </tx:advice>

    <!-- AOP的配置 -->
    <aop:config>
    <aop:pointcut expression="execution(* com.spring4tx.demo2.AccountServiceImpl.*(..))" id="pointcut1"/>
    <!-- advisor:一个切入点一个通知,aspect:多个切入点、通知 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1" />
    </aop:config>

    <!-- 配置DAO -->
    <bean id="accountDao" class="com.spring4tx.demo2.AccountDaoImpl">
     <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置Service -->
    <bean id="accountService" class="com.spring4tx.demo2.AccountServiceImpl">
    <!-- 注入事务管理模版 -->
<!--     <property name="transactionTemplate" ref="transactionTemplate"/> -->
    </bean>

</beans>

注解方式:

1 引入aop开发jar包

2 恢复转账环境

3 配置事务管理器(同xml)

4 开启注解事务:

5 在业务层添加注解

6 测试(效果同xml方式)

tx3.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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">

     <!--通过context标签引入jdbc配置文件 -->
     <context:property-placeholder location="classpath:jdbc.properties"/>

     <!-- 开启注解事务 -->
     <tx:annotation-driven transaction-manager="transactionManager"/>

     <!-- 配置C3P0连接池 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="${jdbc.driverClass}"/>
       <property name="jdbcUrl" value="${jdbc.url}"/>
       <property name="user" value="${jdbc.user}"/>
       <property name="password" value="${jdbc.password}"/>
     </bean>

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

    <!-- 配置DAO -->
    <bean id="accountDao" class="com.spring4tx.demo3.AccountDaoImpl">
     <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置Service -->
    <bean id="accountService" class="com.spring4tx.demo3.AccountServiceImpl">

    </bean>

</beans>

二十 Spring的事务管理及其API&事务的传播行为,编程式&声明式(xml式&注解式,底层AOP),转账案例的更多相关文章

  1. Spring事务管理的另一种方式--TransactionTemplate编程式事务管理简单入门

    1, 一直以来, 在用Spring进行事物管理时, 只知道用声明式的策略, 即根据不同的数据源, 配置一个事物管理器(TransactionManager), 通过配置切面(PointCut)应用到相 ...

  2. 事务之二:spring事务(事务管理方式,事务5隔离级别,7个事务传播行为,spring事务回滚条件)

    事物管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的一致性. spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或 ...

  3. Spring 事务管理的API

    Spring事务管理有3个API,均为接口. (1)PlatformTransactionManager    平台事务管理器 常用的实现类: DataSourceTransactionManager ...

  4. Spring事务管理——其他的事务属性

    之前我们说过Spring事务管理中的事务的传播行为的属性.下面我们来说一下它的其他属性. 一.事务的隔离级别 1 .数据库事务并发问题.假设现在有两个事务:Transaction01和Transact ...

  5. spring boot开启事务管理,使用事务的回滚机制,使两条插入语句一致

    spring boot 事务管理,使用事务的回滚机制 1:配置事务管理 在springboot 启动类中添加 @EnableTransactionManagement //开启事务管理 @Enable ...

  6. Linux学习之CentOS(二十六)--Linux磁盘管理:LVM逻辑卷的创建及使用

    在上一篇随笔里面 Linux学习之CentOS(二十五)--Linux磁盘管理:LVM逻辑卷基本概念及LVM的工作原理,详细的讲解了Linux的动态磁盘管理LVM逻辑卷的基本概念以及LVM的工作原理, ...

  7. Spring编程式和声明式事务实例讲解

    Java面试通关手册(Java学习指南):https://github.com/Snailclimb/Java_Guide 历史回顾: 可能是最漂亮的Spring事务管理详解 Spring事务管理 S ...

  8. Spring框架——事务处理(编程式和声明式)

     一. 事务概述 ●在JavaEE企业级开发的应用领域,为了保证数据的完整性和一致性,必须引入数据库事务的概念,所以事务管理是企业级应用程序开发中必不可少的技术. ●事务就是一组由于逻辑上紧密关联而合 ...

  9. 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】

    一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...

随机推荐

  1. Codeforces Round #618 E

    题意: 给你一个n的数组,你可以进行无数次,选择区间使得区间内的值相加,然后区间的所有的值变成平均值. 使得最后数组的字典序最小 思路: 最后的数组一定是单调递增的,只要它比之前的平均值数大,就不会操 ...

  2. mysql之mysql的安装

    此次MySQL安装的版本为:MySQL8.0 系统为:centos6.9 64位 一.利用yum仓库安装 wget https://repo.mysql.com//mysql80-community- ...

  3. vue中,实现锚点定位及跳转(url不发生变化)

    <div class="footer" @click="returnTop"> methods:{ returnTop:function(){ do ...

  4. EasyUI tree的三种选中状态

    EasyUI中tree有三种选中状态,分别是checked(选中).unchecked(未选中).indeterminate(部分选中). 其中indeterminate状态比较特殊,主要表示只有部分 ...

  5. 「题解」Just A String

    目录 题目 原题目 简易题意 思路及分析 代码 题目 原题目 点这里 简易题意 现定义一个合法的字符串满足将其打散并任意组合之后能够形成回文串. 给你 \(m\) 种字母,问随机构成长度为 \(n\) ...

  6. 理解Linux内核注释

    内核是Linux的心脏,它是在引导时装入的程序,用来提供用户层程序和硬件之间的接口,执行发生在多任务系统中的实际任务转换,处理读写磁盘的需求,处理网络接口,以及管理内存.一般情况下,自动安装的内核无需 ...

  7. 实现简单ORM案例

    ORM框架: • 我们希望设计一个可以实现对象和SQL自动映射的框架,但是整体用法和设计比Hibernate简单.砍掉不必要的功能.• 会穿插使用设计模式• 增加 – 将对象对应成sql语句,执行sq ...

  8. 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...

  9. HTMLUNIT另一种注册方法

    1 环境搭建: 1)下载 从链接:http://sourceforge.net/projects/htmlunit/files/htmlunit/ 下载最新的bin文件 2)关于bin文件 里面主要包 ...

  10. vue 组件,以及组件的复用

    有时候代码的某一模块可能会经常使用到,那么完全可以把这一模块抽取出来,封装为一个组件,哪里需要用到的时候只需把模块调用即可 .参考vue官方 https://cn.vuejs.org/v2/guide ...