Spring 事物传播特性

这是Spring官方的定义 一共有7种 摘自源码省略了一部分

public interface TransactionDefinition {int PROPAGATION_REQUIRED = 0;int PROPAGATION_SUPPORTS = 1;int PROPAGATION_MANDATORY = 2;int PROPAGATION_REQUIRES_NEW = 3;int PROPAGATION_NOT_SUPPORTED = 4;int PROPAGATION_NEVER = 5;int PROPAGATION_NESTED = 6;
}

ROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS -- 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY -- 支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW -- 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED -- 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER -- 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED -- 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作

前面6个都非常好理解,只有 PROPAGATION_NESTED 这个传播特性容易让人搞混淆,所以这里特别说明一下。

PROPAGATION_REQUIRES_NEW starts a new, independent "inner" transaction for the given scope. This transaction will be committed or rolled back completely independent from the outer transaction, having its own isolation scope, its own set of locks, etc. The outer transaction will get suspended at the beginning of the inner one, and resumed once the inner one has completed. Such independent inner transactions are for example used for id generation through manual sequences, where the access to the sequence table should happen in its own transactions, to keep the lock there as short as possible. The goal there is to avoid tying the sequence locks to the (potentially much longer running) outer transaction, with the sequence lock not getting released before completion of the outer transaction. PROPAGATION_NESTED on the other hand starts a "nested" transaction, which is a true subtransaction of the existing one. What will happen is that a savepoint will be taken at the start of the nested transaction. íf the nested transaction fails, we will roll back to that savepoint. The nested transaction is part of of the outer transaction, so it will only be committed at the end of of the outer transaction. Nested transactions essentially allow to try some execution subpaths as subtransactions: rolling back to the state at the beginning of the failed subpath, continuing with another subpath or with the main execution path there - all within one isolated transaction, and not losing any previous work done within the outer transaction. For example, consider parsing a very large input file consisting of account transfer blocks: The entire file should essentially be parsed within one transaction, with one single commit at the end. But if a block fails, its transfers need to be rolled back, writing a failure marker somewhere. You could either start over the entire transaction every time a block fails, remembering which blocks to skip - or you mark each block as a nested transaction, only rolling back that specific set of operations, keeping the previous work of the outer transaction. The latter is of course much more efficient, in particular when a block at the end of the file fails. 

主要看绿色这段话,大概意思是说 PROPAGATION_NESTED 开始一个 "嵌套的" 事务,  它是已经存在事务的一个真正的子事务. 潜套事务开始执行时,  它将取得一个 savepoint. 如果这个嵌套事务失败, 我们将回滚到此 savepoint. 潜套事务是外部事务的一部分, 只有外部事务结束后它才会被提交.

说到这大家可能已经清楚这个传播特性是干啥的了,其实就是利用了数据库的还原点的原理,底层还是通过设置Connection.setSavepoint() 与数据库打交道。

    ServiceA {/*** 事务属性配置为 PROPAGATION_REQUIRED*/void methodA() {try {//事务属性配置为 PROPAGATION_NESTEDServiceB.methodB(); //如果methodB()方法出现异常,则回滚方法B的所有SQL操作} catch (SomeException) {// 执行其他业务, 如 ServiceC.methodC();
            }}}

转载一遍文章讲解的也很详细

http://www.iteye.com/topic/35907

Spring 事物传播特性相关推荐

  1. 什么是事务、事务特性、事务隔离级别、spring事务传播特性

    1.什么是事务: 事务是指程序中的一个操作序列.其特点是:该序列的所有操作要么全部成功完成,要么只要有一个操作失败,则该序列所有操作都将被撤销.这也是事务的原子性(要么成功,要么失败). 2.事务特性 ...

  2. Spring事务传播特性实例解析

    背景介绍 目前系统正在进行代码重构前期预研工作,目标采用spring控制事务以减少开发代码量,提高开发效率.同时避免开发人员编码控制事务所带来的链接没有释放,事务没有提交,出现异常事务没有回滚的Bug ...

  3. Spring事务传播特性与事物隔离级别

    红花易衰似郎意,水流无限似侬愁.--刘禹锡<竹枝词> 传播特性: 传播行为 意义 PROPAGATION_MANDATORY 表示该方法必须运行在一个事务中.如果当前没有事务正在发生,将抛 ...

  4. Spring事务传播特性实例解析(以及如何使用注解形式事务)

    原文地址:http://blog.csdn.net/yoara/article/details/16114853 原文地址的文章,写的demo会误导读者,所以在原文地址文章的基础上对原作者的demo进 ...

  5. 七种Spring事务传播特性

    Spring中通过Propagation来设置事务的传播属性的,在这个属性中提供了我们其中关于事务传播的特性: 1.     PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务, ...

  6. 关于spring 事物传播性的研究

    spring的一大特色就是数据库事务管理方便,我们在代码中编写代码时,看不到事务的使用,关键是spring 使用了AOP进行事务拦截. 这篇文章主要介绍spring的事务传播性. 1.为什么要介绍这个 ...

  7. SPRING事务传播特性

    PROPAGATION_REQUIRED–支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPORTS–支持当前事务,如果当前没有事务,就以非事务方式执 ...

  8. Spring事物详解和传播行为

    事物的四大特性ACID 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完全不起作用. 一致性(Consistency):一旦事务完成(不管成 ...

  9. mysql数据库事务传播特性_什么是事务的传播特性?

    我们都知道事务的概念,那么事务的传播特性是什么呢?(此处着重介绍传播特性的概念,关于传播特性的相关配置就不介绍了,可以查看spring的官方文档) 在我们用SSH开发项目的时候,我们一般都是将事务设置 ...

最新文章

  1. 提高C++性能的编程技术笔记:编码优化+测试代码
  2. vs2010往oracle 10g中插入数据
  3. EVT DVT PVT TP PP MP分别指哪个阶段
  4. article之api文档
  5. Python使用传输层安全协议TLS/SSL实现信息加密传输
  6. IDEA中Maven项目中界面右边的Maven Projects窗口找不到不出来
  7. HDU1274 展开字符串【文本处理】
  8. 3x3矩阵怎么求逆矩阵_表象变换的幺正算符怎么定义的? 为何说算符与态可以看作矩阵?...
  9. Android事件传递机制【Touch事件】
  10. bzoj 3672 购票 点分治+dp
  11. 成都公办计算机学校,成都计算机学校公立-成都公立的计算机学校有哪
  12. 详解二叉树的非递归遍历
  13. 信息学奥赛一本通C++语言-----1142:单词的长度
  14. mongodb联合查询
  15. 计算组合数的三种方式
  16. solr使用shards提示403
  17. 天猫双11背后的阿里技术
  18. excel表格怎样汇总
  19. 【PTA】【C语言】复盘练习——编程题
  20. Macbook忘记密码的解决方法

热门文章

  1. 面向对象笔试题练习一
  2. OpenStack 计算节点删除
  3. Object.observe将不加入到ES7
  4. P1357 花园 (矩阵快速幂+ DP)
  5. 15_新闻客户端_展示文字内容完成
  6. html中radio,checkbox值的获取、赋值、注册事件
  7. Java设计模式----策略模式(Strategy)
  8. apache+php windows下配置
  9. 设计模式建议学习顺序
  10. 带有控制按钮的图片滚动