文章目录

  • 事务
    • 为什要用到Spring中AOP事务
    • 事物的特性 ACID
    • 事务并发问题
    • 事务的隔离级别
  • spring事务管理
    • 事务操作
    • 事务操作对象
    • spring管理事务的属性介绍
    • spring管理事务方式
      • 编码式
      • xml配置(aop)
      • 注解配置
      • @Transactional注解在方法上添加
      • @Transactional注解在类上添加
  • 实际案例
    • xml配置注入applicationContext.xml:
    • 手动注解注入applicationContext2.xml:
    • DAO层:目标对象
    • service层:业务处理
    • 测试类:
      • xml配置测试类
      • 注解测试类:

事务

为什要用到Spring中AOP事务

首先举个例子了解一下什么是事务操作?以及出现的问题?课程中老师用到的例子是转账的事务例子,A将钱转给B,此时service层提供一个方法用来在数据库中进行减操作、B收到钱后会开启一个事务用来加操作,但是如果A在赚钱给B操作过程中出现了异常,此时很可能是A中的减操作已经执行完毕,而B却没有收到钱,那么此时钱就不翼而飞了,那么此时我们就应该提供一种解决方案,让A出现异常后不会执行减操作这样不就可以避免上述情况的发生,通过AOP的学习,我们可以通过SpringAOP来对service层中的加减方法的功能进行加强,开始学习吧!

事物的特性 ACID

原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)

事务并发问题

脏读、不可重复读、幻读
名词解释

事务的隔离级别

1 读未提交
2 读已提交
4 可重复读
8 串行化
名词解释

spring事务管理

事务操作

  1. 打开事务
  2. 提交事务
  3. 回滚事务

在执行SQL语句之前,先执行start transaction,这就开启了一个事务(事务的起点),然后可以去执行多条SQL语句,最后要结束事务,commit表示提交,即事务中的多条SQL语句所做出的影响会持久化到数据库中。或者rollback,表示回滚,即回滚到事务的起点,之前做的所有操作都被撤消了!

事务操作对象

因为在不同平台,操作事务的代码各不相同,spring提供了一个接口

  1. DataSourceTransactionManager
  2. HibernateTransitionmanager

注意:在spring中玩事务管理.最为核心的对象就是TransactionManager对象

spring管理事务的属性介绍

  1. 事务的隔离级别
  2. 是否只读:true 只读、false 可操作
  3. 事务的传播行为

spring管理事务方式

编码式

1.将核心事务管理器配置到spring容器

<!-- 事物核心管理器 ,封装了所有的事物操作依赖于连接池-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property>
</bean>

2.配置TransactionTemplate模板

<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"><property name="transactionManager" ref="transactionManager"></property>
</bean>

3.将事务模板注入Service

<!-- 3.Service-->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" ><property name="ad" ref="accountDao" ></property><property name="tt" ref="transactionTemplate" ></property>
</bean>

4.在Service中调用模板

 public void transfer(final Integer from,final Integer to,final Double money) {tt.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus arg0) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}});}

xml配置(aop)

1.导包:

2.导入新的约束(tx)

beans: 最基本
context:读取properties配置
aop:配置aop
tx:配置事务通知

3.配置通知

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" ><tx:attributes><!-- 以方法为单位,指定方法应用什么事务属性isolation:隔离级别propagation:传播行为read-only:是否只读--><tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /><tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /><tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /></tx:attributes>
</tx:advice>

4.配置将通知织入目标

<!-- 配置织入 -->
<aop:config  ><!-- 配置切点表达式 --><aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"/><!-- 配置切面 : 通知+切点advice-ref:通知的名称pointcut-ref:切点的名称--><aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>

注解配置

在注解配置ApplicationContext.xml的时候需要把上述方式中xml配置注入中的配置事务通知、配置织入去掉,
然后在service实现类方法中添加注解即可。

@Transactional注解在方法上添加

 @Override@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)public void transfer(final Integer from,final Integer to,final Double money) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}

@Transactional注解在类上添加

为了避免每次重写方法的时候都需要手动添加注解,用这种方式会统一给每个方法都注入,如果想改变某一个方法的注解内容如只读属性改成可操作,在方法上再添加注解即可

//统一配置
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {private AccountDao ad ;private TransactionTemplate tt;//只读属性改成可操作@Override@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)public void transfer(final Integer from,final Integer to,final Double money) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}
}

逻辑关系图:

实际案例

xml配置注入applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "><!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"  /><!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" ><property name="transactionManager" ref="transactionManager" ></property>
</bean><!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" ><tx:attributes><!-- 以方法为单位,指定方法应用什么事务属性isolation:隔离级别propagation:传播行为read-only:是否只读--><tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /><tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /><tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /><tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /></tx:attributes>
</tx:advice><!-- 配置织入 -->
<aop:config  ><!-- 配置切点表达式 --><aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"/><!-- 配置切面 : 通知+切点advice-ref:通知的名称pointcut-ref:切点的名称--><aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config><!-- 1.将连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ><property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property><property name="driverClass" value="${jdbc.driverClass}" ></property><property name="user" value="${jdbc.user}" ></property><property name="password" value="${jdbc.password}" ></property>
</bean><!-- 2.Dao-->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" ><property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 3.Service-->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" ><property name="ad" ref="accountDao" ></property><property name="tt" ref="transactionTemplate" ></property>
</bean>  </beans>

手动注解注入applicationContext2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "><!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"  /><!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" ><property name="transactionManager" ref="transactionManager" ></property>
</bean><!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven/><!-- 1.将连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ><property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property><property name="driverClass" value="${jdbc.driverClass}" ></property><property name="user" value="${jdbc.user}" ></property><property name="password" value="${jdbc.password}" ></property>
</bean><!-- 2.Dao-->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl" ><property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 3.Service-->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl" ><property name="ad" ref="accountDao" ></property><property name="tt" ref="transactionTemplate" ></property>
</bean>  </beans>

DAO层:目标对象

AccountDao和AccountDaoImpl

package cn.itcast.dao;public interface AccountDao {//加钱void increaseMoney(Integer id,Double money);//减钱void decreaseMoney(Integer id,Double money);
}package cn.itcast.dao;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  {@Overridepublic void increaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id);}@Overridepublic void decreaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id);}}

service层:业务处理

AccountService与AccountServiceImpl

package cn.itcast.service;public interface AccountService {//转账方法void transfer(Integer from,Integer to,Double money);}package cn.itcast.service;import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;import cn.itcast.dao.AccountDao;@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {private AccountDao ad ;private TransactionTemplate tt;//只读属性改成可操作@Override@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)public void transfer(final Integer from,final Integer to,final Double money) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}/*   @Overridepublic void transfer(final Integer from,final Integer to,final Double money) {tt.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus arg0) {//减钱ad.decreaseMoney(from, money);int i = 1/0;//加钱ad.increaseMoney(to, money);}});}
*/public void setAd(AccountDao ad) {this.ad = ad;}public void setTt(TransactionTemplate tt) {this.tt = tt;}}

测试类:

xml配置测试类

package cn.itcast.tx;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.itcast.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {@Resource(name="accountService")private AccountService as;@Testpublic void fun1(){as.transfer(1, 2, 100d);}
}

注解测试类:

package cn.itcast.tx;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.itcast.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Demo2 {@Resource(name="accountService")private AccountService as;@Testpublic void fun1(){as.transfer(1, 2, 100d);}
}

spring中aop事务相关推荐

  1. Spring(四)——AOP、Spring实现AOP、Spring整合Mybatis、Spring中的事务管理

    文章目录 1. 什么是AOP 2. 使用Spring实现AOP 2.1 使用Spring的API 接口实现 2.2 自定义实现 2.3 使用注解实现 3. 整合MyBatis 3.1 MyBatis- ...

  2. Spring中的事务管理详解

    在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...

  3. spring中的事务配置

    为什么80%的码农都做不了架构师?>>>    一 简介 spring中提供了三种事务管理的方法. 编程式事务管理 :亦即自己编写事务管理的代码,通过注入获取到spring中的事务管 ...

  4. Spring中解决事务以及异步注解失效

    Spring中解决事务以及异步注解失效 一.重现@Transaction失效的场景 有如下业务场景,新增订单后,自动发送短信,下面的代码在同一个类中: @Transaction public void ...

  5. spring事务管理器的作用_【面试必问】Spring中的事务管理详解

    在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...

  6. (转)Spring中的事务操作

    http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ...

  7. 基于Spring中的事务管理机制

    什么是事务? 通俗理解,事务其实就是一系列指令的集合. 为什么要使用事务管理? 我们在实际业务场景中,经常会遇到数据频繁修改读取的问题.在同一时刻,不同的业务逻辑对同一个表数据进行修改,这种冲突很可能 ...

  8. java day60【 Spring 中的 JdbcTemplate[会用] 、Spring 中的事务控制 、Spring5 的新特性[了解] 】...

    第1章 Spring 中的 JdbcTemplate[会用] 1.1JdbcTemplate 概述 1.2JdbcTemplate 对象的创建 1.3spring 中配置数据源 1.3.1 环境搭建 ...

  9. spring中AOP(面向切面编程)

    spring中AOP(面向切面编程) 面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是spring框架中的一个重要内容 ...

最新文章

  1. rust高级矿场_高级 Rust 所有权管理
  2. Serializable:明明就一个空接口!为什么还要实现它?
  3. 各类型数据的导入与导出
  4. python判断是不是整数的命令_介绍python判断一个数是不是正小数和整数的方法
  5. ExtClassLoader和AppClassLoader是Launcher类的两个内部类
  6. SpringCloud Netflix Ribbon
  7. 前端学习(2629):npm安装成功配置
  8. Java中的enum详细解析------Java enum 枚举还可以这么用
  9. 戴尔SC5020发布,专为提高效率/经济性优化设计的中端存储利器
  10. air英语怎么读_如何用英语发音“Macbook Air”
  11. 使用Visual Studio 部署SharePoint时提示“路径中具有非法字符”
  12. 创业,一定要咬牙坚持吗?屁,该放就放
  13. java常用面板_Java 常用面板
  14. Python 标准库 —— cgi/html
  15. Algo: Basic
  16. 源代码仓库选择-gitlab
  17. Vue中<keep-alive>组件的使用
  18. 求助可移动磁盘显示没有格式化
  19. nginx安装部署ssl证书
  20. qboimathtest1 t2 配对

热门文章

  1. SpringMVC获取response的问题
  2. php怎么上传函数,php上传函数怎么封装
  3. 复数卷积 tensorflow_PyTorch 中的傅里叶卷积
  4. 串口助手驱动_STM32Cube22(补充) | 使用硬件I2C驱动OLED(SSD1306)
  5. python decimal用法_Python decimal模块使用方法详解
  6. Android studio的设置界面介绍
  7. hive 开窗函数_Hive的架构剖析
  8. 国嵌c语言深度,国嵌C语言3部全
  9. git上传分支的原理_几张图让你彻底弄懂git工作流(二) ——git分支
  10. 添加日志_第五章springboot2.0添加aop日志实现记录请求地址