概念

1.事务

1)事务特性:ACID

原子性 :强调事务的不可分割.

一致性 :事务的执行的前后数据的完整性保持一致.

隔离性 :一个事务执行的过程中,不应该受到其他事务的干扰.

持久性 :事务一旦结束,数据就持久到数据库.

2)事务并发问题

脏读 :一个事务读到了另一个事务的未提交的数据.

不可重复读 :一个事务读到了另一个事务已经提交的 update 的数据导致多次查询结果不一致.

虚幻读 :一个事务读到了另一个事务已经提交的 insert 的数据导致多次查询结果不一致.

3)事务的隔离级别

(1)读未提交:脏读,不可重复读,虚读都有可能发生.

(2)读已提交:避免脏读。但是不可重复读和虚读有可能发生

(3)可重复读:避免脏读和不可重复读.但是虚读有可能发生.

(4)串行化:避免以上所有读问题.

Mysql 默认:可重复读

Oracle 默认:读已提交

2.spring封装了事务管理代码

1)事务操作

(1)打开事务

(2)提交事务

(3)回滚事务

2)事务操作对象

因为在不同平台,操作事务的代码各不相同.spring提供了一个接口:PlatformTransactionManager,在spring中玩事务管理.最为核心的对象就是TransactionManager对象。

3)spring管理事务的属性介绍

(1)事务的隔离级别

(2)是否只读

true 只读

false 可操作

(3)事务的传播行为

* 保证同一个事务中

        PROPAGATION_REQUIRED 支持当前事务,如果不存在 就新建一个(默认)

PROPAGATION_SUPPORTS 支持当前事务,如果不存在,就不使用事务

PROPAGATION_MANDATORY 支持当前事务,如果不存在,抛出异常

* 保证没有在同一个事务中

        PROPAGATION_REQUIRES_NEW 如果有事务存在,挂起当前事务,创建一个新的事务

PROPAGATION_NOT_SUPPORTED 以非事务方式运行,如果有事务存在,挂起当前事务

PROPAGATION_NEVER 以非事务方式运行,如果有事务存在,抛出异常

        PROPAGATION_NESTED 如果当前事务存在,则嵌套事务执行

隔离级别

传播行为

   事务传播行为(propagation behavior)指的就是当一个事务方法被另一个事务方法调用时,这个事务方法应该如何进行。

spring管理事务方式一Template模板

1.导包

2.DAO

AccountDao.java

package cn.mf.dao;

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

AccountDaoImpl.java

package cn.mf.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  {

    @Override
public void increaseMoney(Integer id, Double money) {
getJdbcTemplate().update("update account set money = money+? where id = ? ", money,id);
} @Override
public void decreaseMoney(Integer id, Double money) {
getJdbcTemplate().update("update account set money = money-? where id = ? ", money,id);
} }

3.SERVICE

AccountService.java

package cn.mf.service;

public interface AccountService {
//转账方法
void transfer(Integer from,Integer to,Double money);
}

AccountServiceImpl.java

package cn.mf.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate; import cn.mf.dao.AccountDao; public class AccountServiceImpl implements AccountService { private AccountDao ad ;
private TransactionTemplate tt; @Override
public void transfer(final Integer from,final Integer to,final Double money) {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected 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;
} }

4.db.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

5.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>
<!-- 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.mf.dao.AccountDaoImpl" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 3.Service-->
<bean name="accountService" class="cn.mf.service.AccountServiceImpl" >
<property name="ad" ref="accountDao" ></property>
<property name="tt" ref="transactionTemplate" ></property>
</bean> </beans>

Junit

package cn.mf.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.mf.service.AccountService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
@Resource(name="accountService")
private AccountService as; @Test
public void fun1(){
as.transfer(1, 2, 100d);
}
}

资料

https://blog.csdn.net/weixin_39625809/article/details/80707695

spring框架学习(六)AOP事务及spring管理事务方式之Template模板的更多相关文章

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

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

  2. Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建

    之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...

  3. Spring框架学习笔记(10)——Spring中的事务管理

    什么是事务 举例:A给B转500,两个动作,A的账户少500,B的账户多500 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 一.注解添加事务管理方 ...

  4. Spring框架学习笔记(7)——Spring Boot 实现上传和下载

    最近忙着都没时间写博客了,做了个项目,实现了下载功能,没用到上传,写这篇文章也是顺便参考学习了如何实现上传,上传和下载做一篇笔记吧 下载 主要有下面的两种方式: 通过ResponseEntity实现 ...

  5. Spring框架学习05——AOP相关术语详解

    1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...

  6. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  7. Spring框架学习06——AOP底层实现原理

    在Java中有多种动态代理技术,如JDK.CGLIB.Javassist.ASM,其中最常用的动态代理技术是JDK和CGLIB. 1.JDK的动态代理 JDK动态代理是java.lang.reflec ...

  8. Spring框架学习笔记(9)——Spring对JDBC的支持

    一.使用JdbcTemplate和JdbcDaoSupport 1.配置并连接数据库 ①创建项目并添加jar包,要比之前Spring项目多添加两个jar包c3p0-0.9.1.2.jar和mysql- ...

  9. Spring框架学习01——使用IDEA开发Spring程序

    1.创建项目 点击“Create New Project”,新建项目 选择Maven项目 项目配置 使用本地安装的Maven 一直点击Next,最后点击完成当控制台中出现“BUILD SUCCESS” ...

随机推荐

  1. C++操作mysql方法总结(1)

    C++通过mysql的c api和通过mysql的Connector C++ 1.1.3操作mysql的两种方式 使用vs2013和64位的msql 5.6.16进行操作 项目中使用的数据库名为boo ...

  2. 第六周PSP&进度条

    团队项目PSP 一.表格:     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论alpha完成情况并总结 9:40 11:20 17 ...

  3. JMeter性能测试基础 (2) - 变量的使用

    在使用JMeter进行性能测试时,一般情况下要保证样本容量尽可能大,这样才能得到一个比较合理的结果.也就是说,我们不能只对同一个URL进行多次访问,而是要对统一模块下尽可能多的URL进行访问,以取得相 ...

  4. vim出现自动补全的问题

    当使用vim编辑文件自动补全文件名称的时候,可能会出现_arguments:451: _vim_files: function definition file not found的错误,这个时候一般都 ...

  5. Apache+Nginx+php共存(一)

    在实际开发中个人的电脑中经常需要安装 WNMRP.WAMRP.LNMRP.LAMRP等各种开发环境来应对不同的开发需求. 此篇主要是对WINDOWS系统下 Apache+Nginx + PHP +My ...

  6. ecplise maven springmvc工程搭建

    转载自:https://www.cnblogs.com/crazybirds/p/4643497.html 内网上网代理配置: 第一步:新建maven项目,选择Maven Project,如图1.   ...

  7. Day22-Django之缓存

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...

  8. kafka问题集(一):broker少于kafka节点数

    问题集仅为个人实践,若有不准确的,欢迎交流! 一.现象: 集群有3台kafka服务器,而kafka 的9002界面上broker仅有2个:log.dirs配置路径为/data/kafka/data,而 ...

  9. 【模板】ISAP最大流

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  10. BZOJ1061 [Noi2008]志愿者招募 【单纯形】

    题目链接 BZOJ1061 题解 今天终于用正宗的线性规划\(A\)了这道题 题目可以看做有\(N\)个限制和\(M\)个变量 变量\(x_i\)表示第\(i\)种志愿者的人数,对于第\(i\)种志愿 ...