spring 事务 笔记3.1
Spring事务
以前的事务都是编程式事务,需要开启和关闭,然后程序写在这里面
spring,声明式事务
Spring事务隔离级别
DEFAULT 使用数据库默认隔离级别
READ_UNCOMMITTED 允许读取尚未提交的数据。可能导致脏读、幻读或不可重复读。
READ_COMMITTED 允许从已经提交的并发事务读取。可以防止脏读,但依然会出现幻读和不可重复读。
REPEATABLE_READ 对相同字段的多次读取结果是相同的,除非数据被当前事务改变。可以防止脏读和不可重
复读,但幻读依然出现。
SERIALIZABLE 完全符合ACID的隔离级别,确保不会发生脏读,幻读和不可重复读。
脏读: 一个事务读取到另一个事务没有提交到的数据。
不可重复读: 在同一事务中,多次读取同一数据返回的结果不同。
幻读: 一个事务读取到另一个事务已经提交的事务。
==============================================
Spring事务传播属性
REQUIRED
业务方法需要在一个事务中运行。如果方法运行时,已经处在一个事务中,那么加入到这个事务中,
否则自己创建一个事务。(大部分情况下使用)
NOT-SUPPORTED
声明方法需要事务。如果方法没有关联到一个事务,容器会为它开启一个事务,如果方法在
一个事务中被调用,该事务将会被挂起,在方法调用结束后 ,原先的事务会恢复执行。
REQUIREDNEW
业务方法必须在自己的事务中运行。一个新的事务将被启动,而且如果有一个事务正在运行,
则将这个事务挂起,方法运行结束后,新事务执行结束,原来的事务恢复运行。
MANDATORY 该方法必须运行在一个现有的事务中,自身不能创建事务,如果方法在没有事务的环境下被调用,
则会抛出异常。
SUPPORTS
如果该方法在一个事务环境中运行,那么就在这个事务中运行,如果在事务范围外调用,那么就在
一个没有事务的环境下运行。
NEVER
表示该方法不能在有事务的环境下运行,如果在有事务运行的环境下调用,则会抛出异常
NESTED
如果一个活动的事务存在, 则运行在一个嵌套的事 务中, 如果没 有活动事务, 则按照REQUIRED事
务方式执行。该事务可以独立的进行提交或回滚,如果回滚不会对外围事务造成影响
=========================================
spring事务出现在service层
1.建立jdbc事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
2.基于xml配置的事务 需要导入schema
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
<!-- 这一句 -->
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
<!-- 这一句 -->
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
>
事务通知
<!-- 事务通知 transaction-manager="transactionManager"跟上面事务管理器的配置的id相同-->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!-- method指的是service类中的哪些方法需要加事务,以及加哪种事务
isolation隔离级别 propagation传播属性
find*表示只要是以find开头的都加上事务,查询的时候read-only="true"表示只读,性能比较高-->
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="eidt*"/>
<tx:method name="del*"/>
</tx:attributes>
</tx:advice>
<!-- AOP 通知加到哪些类上了 -->
<aop:config>
<aop:pointcut expression="execution(* com.kaishengit.service..*.*(..))" id="pt"/>
<!-- 通知引用上面的id advice-ref="advice" pointcut-ref="pt"上面的id-->
<aop:advisor advice-ref="advice" pointcut-ref="pt"/>
</aop:config>
举例
@Named
public class StudentService { @Inject
private StudentDao studentDao; public void save(Student stu){
studentDao.save(stu);
if(1==1){
throw new RuntimeException();
}
studentDao.save(stu);
} public Student findById(int id) {
return studentDao.findById(id);
}
}
============================================
2.基于于Annotation的事务
JDBC事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
基于注解的事务
<tx:annotation-driven transaction-manager="transactionManager"/>
举例
@Named
@Transactional
public class StudentService { @Inject
private StudentDao studentDao; public void save(Student stu){
studentDao.save(stu);
if(1==1){
throw new RuntimeException();
}
studentDao.save(stu);
} @Transactional(readOnly=true)
public Student findById(int id) {
return studentDao.findById(id);
}
}
事务在发生异常的时候才会回滚,如果在service中调用的Dao中的方法,在dao中
直接try catch掉了,是不会回滚的,使用spring,执行的sql默认是向上抛出运行时异常。意思就是说
spring默认的 是只有出现运行时异常才会回滚
if(1==1){
throw new Exception();
}
但是是可以修改的
@Named
/*默认的是 @Transactional(rollbackFor=RuntimeException.class)*/
@Transactional(rollbackFor=Exception.class)
public class StudentService { @Inject
private StudentDao studentDao; public void save(Student stu){
studentDao.save(stu);
if(1==1){
throw new RuntimeException();
}
studentDao.save(stu);
} @Transactional(readOnly=true)
public Student findById(int id) {
return studentDao.findById(id);
}
}
spring 事务 笔记3.1的更多相关文章
- Spring事务笔记
1:在同一个类中,如果A方法有事务,B方法也有事务(propagation = Propagation.REQUIRES_NEW),如下代码所示: @Override@Transactionalpub ...
- Spring 事务笔记
代码写着写着就钻进源码了. 概念 InfrastructureProxy 结构代理 百度查了查,这个类还没有解释. 进去看了一下: Interface to be implemented by tra ...
- Spring 事务管理笔记
本文为 Spring 框架的事务管理学习笔记,官网文档地址为:Transaction Management,隔离级别及传播属性解释来自 org.springframework.transaction. ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- 本人遇到的spring事务之UnexpectedRollbackException异常解决笔记
本人最近在使用spring事务管理的过程中遇到如下异常,导致服务端抛出500给前端,让搞前端的哥们抱怨我心里着实不爽,前前后后折腾了近半个小时才得于解决,今天就做个笔记,以免日后又犯这个错误.好了,错 ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- Spring事务源码阅读笔记
1. 背景 本文主要介绍Spring声明式事务的实现原理及源码.对一些工作中的案例与事务源码中的参数进行总结. 2. 基本概念 2.1 基本名词解释 名词 概念 PlatformTransaction ...
- Spring学习笔记五:Spring进行事务管理
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6776256.html 事务管理主要负责对持久化方法进行统一的提交或回滚,Spring进行事务管理即我们无需在 ...
- Spring事务管理笔记
事务的目的就是要保证数据的高度完整性和一致性. 在实际的项目中,大多都是使用注解的方式来实现事物,这里也就简单记录下使用@Transactional方法和注意事项. 在xml中添加配置 1234567 ...
随机推荐
- linux之线程之互斥
线程概述: 其实在Linux中,新建的线程并不是在原先的进程中,而是系统通过一个系统调用clone().该系统copy了一个和原先进程完全一样的进程,并在这 个进程中执行线程函数.不过这个copy过程 ...
- 在基类中的析构函数声明为virtual
#include <iostream> using namespace std; class Father { public: ~Father() { cout << &quo ...
- C#中两个整数相除得到带小数点的结果
有时候需要将两个整数相除,获得带小数点的float类型数.例如一个整数12345,需要变成123.45.常见与串口与硬件通讯,DSP处理浮点型比较麻烦,DSP传递来的温度等数据都以整型的方式传递,串口 ...
- C++如何将一个整数输出为小数
double a=3; //一定是double不能是int cout.setf(ios::fixed); cout.precision(2); cout< ...
- POJ 1743 Musical Theme(不可重叠最长重复子串)
题目链接:http://poj.org/problem?id=1743 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一 ...
- Codeforces 116C - Party(dfs)
n个人,每个人之多有一个上司.“上司”关系具有传递性.求最少将人分成多少组,每组中的每个人的上司或者间接上司都不在该组.拿到题就用树的直径wa了一炮... 正解是有向无环森林的最长路.从每个跟节点df ...
- android中像素单位dp、px、pt、sp的比较
dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖 ...
- [LeetCode]题解(python):068-Text Justification
题目来源: https://leetcode.com/problems/text-justification/ 题意分析: 输入一个字符串数组和一个规定长度L.将这个字符串数组的元素尽可能放到长度的L ...
- [LeetCode]题解(python):001-Two-Sum
题目来源: https://leetcode.com/problems/two-sum/ 题意分析: 这道题目是输入一个数组和target,要在一个数组中找到两个数字,其和为target,从小到大输出 ...
- poj 3243 Clever Y 高次方程
1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...