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的更多相关文章

  1. Spring事务笔记

    1:在同一个类中,如果A方法有事务,B方法也有事务(propagation = Propagation.REQUIRES_NEW),如下代码所示: @Override@Transactionalpub ...

  2. Spring 事务笔记

    代码写着写着就钻进源码了. 概念 InfrastructureProxy 结构代理 百度查了查,这个类还没有解释. 进去看了一下: Interface to be implemented by tra ...

  3. Spring 事务管理笔记

    本文为 Spring 框架的事务管理学习笔记,官网文档地址为:Transaction Management,隔离级别及传播属性解释来自 org.springframework.transaction. ...

  4. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  5. 本人遇到的spring事务之UnexpectedRollbackException异常解决笔记

    本人最近在使用spring事务管理的过程中遇到如下异常,导致服务端抛出500给前端,让搞前端的哥们抱怨我心里着实不爽,前前后后折腾了近半个小时才得于解决,今天就做个笔记,以免日后又犯这个错误.好了,错 ...

  6. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  7. Spring事务源码阅读笔记

    1. 背景 本文主要介绍Spring声明式事务的实现原理及源码.对一些工作中的案例与事务源码中的参数进行总结. 2. 基本概念 2.1 基本名词解释 名词 概念 PlatformTransaction ...

  8. Spring学习笔记五:Spring进行事务管理

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6776256.html  事务管理主要负责对持久化方法进行统一的提交或回滚,Spring进行事务管理即我们无需在 ...

  9. Spring事务管理笔记

    事务的目的就是要保证数据的高度完整性和一致性. 在实际的项目中,大多都是使用注解的方式来实现事物,这里也就简单记录下使用@Transactional方法和注意事项. 在xml中添加配置 1234567 ...

随机推荐

  1. 第七届河南省赛H.Rectangles(lis)

    10396: H.Rectangles Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 229  Solved: 33 [Submit][Status] ...

  2. 用DBMS_ADVISOR.SQLACCESS_ADVISOR创建SQL Access Advisor访问优化建议

    使用OEM方式来创建SQL Access Advisor访问优化建议,已经是四五年的事了,下面就来写写怎样使用DBMS_ADVISOR.SQLACCESS_ADVISOR来创建SQL Access A ...

  3. js 定义类对象

    //定义类     //方式一     function A_class(arg1,arg2){         this.arg1=arg1;         this.arg2=arg2;     ...

  4. iOS中Block介绍(二)内存管理与其他特性

    我们在前一章介绍了block的用法,而正确使用block必须要求正确理解block的内存管理问题.这一章,我们只陈述结果而不追寻原因,我们将在下一章深入其原因. 一.block放在哪里 我们针对不同情 ...

  5. c++类的实例化,有没有new的区别

    A a; A * a = new a(); 以上两种方式皆可实现类的实例化,有new的区别在于: 1.前者在堆栈中分配内存,后者为动态内存分配,在一般应用中是没有什么区别的,但动态内存分配会使对象的可 ...

  6. hasOwnProperty和isPrototypeOf方法使用

    hasOwnProperty():判断对象是否有某个特定的属性.必须用字符串指定该属性.(例如,o.hasOwnProperty("name"))  //复制自w3cschool ...

  7. SMACSS:一个关于CSS的最佳实践-1.Overview

    什么是SMACSS? SMACSS(发音"smacks"),全称Scalable and Modular Architecture for CSS.顾名思义,SMACSS是一个可扩 ...

  8. Migration data on SQL

    从表里面导出数据XML: -- export declare @xml xml set @xml = (select * from ( select TableName = 'Schema', xml ...

  9. C++对象模型--C++对象模型

    何为C++对象模型? 部分: 1       语言中直接支持面向对象程序设计的部分 2       对于各种支持的底层实现机制 语言中直接支持面向对象程序设计的部分,如构造函数.析构函数.虚函数.继承 ...

  10. [置顶] How to create Oracle 11g R2 database manually in ASM?

    Step 1: Specify an Instance Identifier (SID) export ORACLE_SID=maomi Step 2: Ensure That the Require ...