1、事务(https://www.cnblogs.com/zhai1997/p/11710082.html

(1)事务的特性:acdi

(2)事务的并发问题:丢失修改,脏读,不可重复读

(3)事务的隔离级别:1、2、4、8

2、Spring的事务管理

(1)Spring封装了事务管理的代码:打开事务、提交事务、回滚事务

在我们学习的不同阶段(JDBC、Hibernate),对事物处理的方法是不一样的,为了解决这个问题,Spring提供了一个接口,PlatformTransactionManager(平台事务管理器),

该接口可以根据不同的平台提供不同的方法来处理事务,

(2)Spring管理事务的属性

事务的隔离级别:1:读未提交、2:读已提交、4:可重复读、8:串行化

本次事务是否只读:true:只读

事务的传播行为:

PROPAGATION REQUIRED 支持当前事务,如果不存在就新建一个(默认)
PROPAGATION_SUPPORTS 支持当前事务,如果不存在,就不使用事务
PROPAGATION MANDATORY 支持当前事务,如果不存在,抛出异常
PROPAGATION_REQUIRES_NEW 如果有事务存在,挂起当前事务,创建一个新的事务
PROPAGATION_NOT_SUPPORTED 以非事务方式运行,如果有事务存在,挂起当前事务
PROPAGATION_NEUER 以非事务方式运行,如果有事务存在,抛出异常
PROPAGATION_NESTED 如果当前事务存在,则嵌套事务执行

如果,事务method1调用事务method2,如果,method1没有开启事务,则method1需要先开启一个事务,method2也调用该事务,如果,method1,已经开启了一个事务,则method2直接用这个事务即可。

3、Spring管理事务的方式:编码式

(1)配置文件:

db.properties:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///bank_transfer
jdbc.user=root
jdbc.password=root

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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.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">
<!--指定要读取的配置文件的位置-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--将连接池放入Spring容器-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--Dao-->
<bean name="accountDao" class="pers.zhb.dao.AccountDaoImp">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--Service-->
<bean name="accountserviceimp" class="pers.zhb.service.AccountServiceImp">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--核心事务管理器,依赖于连接池-->
<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>
</beans>

要确定好每个类之间的依赖关系。

(2) Dao层:

接口:

public interface AccountDao {
void increaseMoney(Integer id,Double money);
void decreaseMoney(Integer id,Double money);
}

实现类:

public class AccountDaoImp extends JdbcDaoSupport implements AccountDao{
@Override
public void increaseMoney(Integer id, Double money) {
String sql="update transfer set money=money+? where id = ?";
super.getJdbcTemplate().update(sql,money,id);
} @Override
public void decreaseMoney(Integer id, Double money) {
String sql="update transfer set money=money-? where id = ?";
super.getJdbcTemplate().update(sql,money,id);
}
}

(3)Service层:

接口:

public interface AccountService {
void transfer(Integer from, Integer to,Double money);
}

实现类:

public class AccountServiceImp implements AccountService {
private AccountDao accountDao;
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(final Integer from, final Integer to,final Double money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
accountDao.decreaseMoney(from,money);
int i=1/0;
accountDao.increaseMoney(to,money);
}
});
}
}

(4)测试类:

  public static void main(String [] args){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
AccountServiceImp accountServiceImp =(AccountServiceImp)applicationContext.getBean("accountserviceimp");
accountServiceImp.transfer(1,2,12d);
}

在service层的方法中,故意制造了错误,再发生异常后未出现转账方钱减少而收款方前未增加的情况,即:钱的总数不会变。

4、Spring事务管理方式:xml配置aop事务

(1)导入约束:

导入tx、aop、context约束。

tx:配置事务通知

aop:配置aop

context:注解

(2)导包:

(3)Dao层的接口和实现类。

(4)Service层的接口和实现类,改层调用Dao层的两个转账方法。

(5)配置文件:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///bank_transfer
jdbc.user=root
jdbc.password=root

该配置文件加jdbc前缀的目的是,与其他的功能的配置文件加以区别。

<?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 "> <!--指定要读取的配置文件的位置-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--将连接池放入Spring容器-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--Dao-->
<bean name="accountDao" class="pers.zhb.dao.AccountDaoImp">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--Service-->
<bean name="accountserviceimp" class="pers.zhb.service.AccountServiceImp">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--核心事务管理器,依赖于连接池-->
<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="transactionInterceptor" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
</beans>

指出数据库相关的配置文件的位置。

读取配置文件中的数据,连接池被放入到了Spring容器。

将Dao层和Service层的对象放入到Spring容器中,其中Service层依赖于Dao层。

核心事务管理器,依赖于连接池。

事务模板对象,依赖于核心事务管理器。

配置事务通知:以方法为单位,isolation:隔离级别,propagation:传播行为,read-only:是否只读,是以方法为单位的。这里是将事务管理的通知(这里不用手动书写)织入到业务逻辑形成代理对象。

(6)测试类:

public class Test {
public static void main(String [] args){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
AccountServiceImp accountServiceImp =(AccountServiceImp)applicationContext.getBean("accountserviceimp");
accountServiceImp.transfer(1,2,12d);
}
}

5、Spring事务管理方式:注解

注解:

 @Transactional(isolation = Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly = false)
public void transfer(final Integer from, final Integer to,final Double money) {
accountDao.decreaseMoney(from,money);
accountDao.increaseMoney(to,money);
}

配置文件:

<!--指定要读取的配置文件的位置-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--将连接池放入Spring容器-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--Dao-->
<bean name="accountDao" class="pers.zhb.dao.AccountDaoImp">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--Service-->
<bean name="accountserviceimp" class="pers.zhb.service.AccountServiceImp">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--核心事务管理器,依赖于连接池-->
<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/>
<!--java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to...异常-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

Spring事务管理(编码式、配置文件方式、注解方式)的更多相关文章

  1. 【核心核心】10.Spring事务管理【TX】XML+注解方式

    转账案例环境搭建 1.引入JAR包 IOC的6个包 AOP的4个包 C3P0的1个包 MySQL的1个驱动包 JDBC的2个目标包 整合JUnit测试1个包 2.引入配置文件 log4j.proper ...

  2. Spring框架的事务管理之基于AspectJ的注解方式(重点掌握,最简单的方式)

    1. 步骤一:恢复转账的开发环境(具体开发环境实现见:https://www.cnblogs.com/wyhluckdog/p/10137283.html)2. 步骤二:applicationCont ...

  3. Spring事务管理中的配置文件(三)

    在开发中,遇到了sql语句报错,但是并没有回滚的情况. 经过几天的排查,终于找到了事务没有回滚的原因. 原来的项目用的是informix的数据库,原来针对事务回滚的机制都是好用的.我本地用的是mysq ...

  4. Spring 事务管理原理探究

    此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...

  5. 事务管理(下) 配置spring事务管理的几种方式(声明式事务)

    配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...

  6. Spring事务管理详解_基本原理_事务管理方式

    1. 事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,使用JDBC的事务管理机制,就是利用java.sql.Connection对象完成对事务的提交,那在没有Spring帮我们管理事 ...

  7. Spring事务管理的三种方式

    一 .第一种:全注解声明式事务 Xml代码 复制代码 收藏代码 .<?xml version="1.0" encoding="UTF-8"?> .& ...

  8. Spring事务管理之几种方式实现事务

    1.事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  9. Spring事务管理之几种方式实现事务(转)

    一:事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

随机推荐

  1. 第5篇 Scrum冲刺博客

    1.站立式会议 1.1 会议图片 1.2 项目进展 成员 昨日任务 今日计划完成任务 陈忠明 歌曲信息的上传/下载包 歌曲批量下载压缩包 吴茂平 完善评论系统 新消息提醒功能设计 黄海钊 修改代码规范 ...

  2. A Distributional Perspective on Reinforcement Learning

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! arXiv:1707.06887v1 [cs.LG] 21 Jul 2017 In International Conference on ...

  3. Trapdoors for Hard Lattices and New Cryptographic Constructions

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 以下是对本文关键部分的摘抄翻译,详情请参见原文. Abstract 我们展示了如何构造各种“trapdoor”密码工具,假设标准格问题的最 ...

  4. JS继承 小白文

    继承我的理解是 一个对象 能够使用另一个对象的方法和属性 继承需要一个父类构造函数 一.通过原型链继承 通过 创建一个 Person 的实例对象,该对象身上 不仅有 name 和 age 等方法, 也 ...

  5. Mysql 如何实现全文检索,关键词跑分

    一.前言 今天一个同事问我,如何使用 Mysql 实现类似于 ElasticSearch 的全文检索功能,并且对检索关键词跑分?我当时脑子里立马产生了疑问?为啥不直接用es呢?简单好用还贼快.但是听他 ...

  6. 使用C#对华为IPC摄像头二次开发(一)

    开发环境: 操作系统:Win10 x64专业版2004 开发工具:VS2019 16.7.2 目标平台:x86 首先去下载IPC SDK(点击下载,需要华为授权账户.) 新建一个WPF的项目,Fram ...

  7. PHP学习中的一些总结(持续更新)

    文件上传部分 在前台的<form>表单中 hidden隐藏域的MAX_FILE_SIZE可以起到实质性的控制作用,即在文件上传之前就可以判断文件的大小,格式为: <form acti ...

  8. Node.js的基础知识点

    一,语言 和 环境(平台) 之间的关系 1,浏览器环境 中的 Javascript 浏览器中 Javascript 的组成部分 ECMAScript核心 + DOM + BOM 2,Node环境 中的 ...

  9. js apply() call() bind() 的使用

    bind ,call,apply 这三者都是用来改变函数的this对象的指向的. call和apply其实是同一个东西,区别只有参数不同. 其实call和apply ,只要你调用调用一个函数的时候就可 ...

  10. Labview学习之路(七)for和while的理论要点

    for循环 循环次数可以为0(N的接线端为) 终止条件:1. 完成N次循环.      2. 添加条件接线端,就像while循环的红点一样,(方法,右键点击边框,添加条件接线端) 数组通过自动索引接入 ...