2019独角兽企业重金招聘Python工程师标准>>>

在使用spring事物通常会配置的Service层 ,当我们调用 一个service成的方法的时候 ,这个方法在执行的过程中对数据的操作会保存在一个事物中,在调用该方法时,要么都成功,要么都失败。当在service中 还调用的其他Service方法 ,那么 其他方法中的事物 应该如何处理呢 ,这就延伸出一个概念 事物的传播特性

  • REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
  • SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
  • MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
  • REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
  • NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
  • NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
  • NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与REQUIRED类似的操作。拥有多个可以回滚的保存点,内部回滚不会对外部事务产生影响。只对DataSourceTransactionManager有效

以上 是spring 事物对应的的7总传播特性

PROPGATION_REQUIRED: 这个配置项的意思是说当我调用service层的方法的时候开启一个事务(具体调用那一层的方法开始创建事务,要看你的aop的配置),那么在调用这个service层里面的其他service的时候,如果当前方法产生了事务就用当前方法产生的事务,否则就创建一个新的事务。这个工作使由Spring来帮助我们完成的。

对应你源码:

public enum Propagation {

/**
     * Support a current transaction, create a new one if none exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>This is the default setting of a transaction annotation.
     */
    REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),

/**
     * Support a current transaction, execute non-transactionally if none exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>Note: For transaction managers with transaction synchronization,
     * PROPAGATION_SUPPORTS is slightly different from no transaction at all,
     * as it defines a transaction scope that synchronization will apply for.
     * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)
     * will be shared for the entire specified scope. Note that this depends on
     * the actual synchronization configuration of the transaction manager.
     * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
     */
    SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),

/**
     * Support a current transaction, throw an exception if none exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),

/**
     * Create a new transaction, suspend the current transaction if one exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>Note: Actual transaction suspension will not work on out-of-the-box
     * on all transaction managers. This in particular applies to JtaTransactionManager,
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available it to it (which is server-specific in standard J2EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),

/**
     * Execute non-transactionally, suspend the current transaction if one exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>Note: Actual transaction suspension will not work on out-of-the-box
     * on all transaction managers. This in particular applies to JtaTransactionManager,
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available it to it (which is server-specific in standard J2EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),

/**
     * Execute non-transactionally, throw an exception if a transaction exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    NEVER(TransactionDefinition.PROPAGATION_NEVER),

/**
     * Execute within a nested transaction if a current transaction exists,
     * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
     * <p>Note: Actual creation of a nested transaction will only work on specific
     * transaction managers. Out of the box, this only applies to the JDBC
     * DataSourceTransactionManager when working on a JDBC 3.0 driver.
     * Some JTA providers might support nested transactions as well.
     * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
     */
    NESTED(TransactionDefinition.PROPAGATION_NESTED);

private final int value;

Propagation(int value) { this.value = value; }

public int value() { return this.value; }

}

转载于:https://my.oschina.net/u/198077/blog/1542197

spring 事物的传播特性相关推荐

  1. spring事务的传播特性

    所谓事务传播行为就是多个事务方法相互调用时,事务如何在这些方法间传播.Spring 支持 7 种事务传播行为: PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在 ...

  2. 事物的传播特性以及事物的隔离界别

    propagation 英 [ˌprɒpə'ɡeɪʃn] 美 [ˌprɑpəˈɡeʃən] 二.传播行为 propagation required 1.传播要求:如果当前没有事务,就创建一个新事务,如 ...

  3. spring中事务传播特性

    转载:若水三千-LOVE 所谓事务传播行为就是多个事务方法相互调用时,事务如何在这些方法间传播.Spring 支持 7 种事务传播行为(Transaction Propagation Behavior ...

  4. spring事务的隔离级别和传播特性详解(附实例)

    spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager.对于编程式事务 ...

  5. Spring事务开启方法 传播特性 隔离界别

    1 Spring事务开启方法 1 在Spring配置文件中开启事务并配置事务的类 2 在需要的需要的方法上 或者类上标注@Transactional 2 @Transactional 属性 1 row ...

  6. spring手动控制事务开启_“上帝视角”图解Spring事务的传播机制原理

    转载:https://mp.weixin.qq.com/s/odP1DKgRtXsCcAKxwGahug 数据库事务的"抓手" 数据库的事务功能已经由数据库自身实现,它留给用户的就 ...

  7. mysql的传播特性_spring事务传播特性和mysql事务隔离级别

    spring事务的传播特性--7种 REQUIRED 支持当前事务,如果没有事务会创建一个新的事务 SUPPORTS 支持当前事务,如果没有事务的话以非事务方式执行 MANDATORY(强制性) 支持 ...

  8. 什么是事务的传播特性?(转载)

    原文链接:http://longdechuanren.iteye.com/blog/646497 我们都知道事务的概念,那么事务的传播特性是什么呢?(此处着重介绍传播特性的概念,关于传播特性的相关配置 ...

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

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

最新文章

  1. 分布式技术追踪 2017年第十二期
  2. BZOJ-2748: [HAOI2012]音量调节 (傻逼背包DP)
  3. Java查询spark中生成的文件,spark原代码生成方法
  4. 附录:更多列表操作命令
  5. 钱 | 钱,钱,钱,钱,钱,钱,钱!钱啊钱!(配音乐)
  6. WPF:DataGrid可过滤、多语言
  7. django上课笔记6-MVC,MTV架构-中间件-初识Form组件
  8. form表单target的用法
  9. Mac升级emacs26
  10. CodeForces - 877C
  11. aspose 转pdf表格大小乱了_关于Aspose.Words转PDF简体中文排版问题申明
  12. 【PI调节】对PI调节的一些认识
  13. win7系统计算机怎么更改密码,Win7系统怎么设置和删除电脑开机密码?
  14. 手机app网易邮箱服务器设置,网易邮箱默认开通IMAP服务
  15. 无人机快速三维建模平台
  16. 【论文学习】《A Survey on Neural Speech Synthesis》
  17. 软件工程---5.系统建模
  18. 在Windows上如何安装和彻底卸载Adobe Flash Player教程
  19. 【数学建模】2022数维杯比赛(模拟退火优化算法、NSII求解)大规模新型冠状病毒疫情最优应对策略研究(Matlab代码实现)
  20. Win11如何设置软件快捷方式?

热门文章

  1. Java集合框架中Map接口的使用
  2. windows 2003系统目前最完善最完美的安全权限方案(转)
  3. 你了解 Performance Timeline Level 2 吗?
  4. asp.net动态添加控件学习
  5. 解读微软开源MMLSpark:统一的大规模机器学习生态系统
  6. trunk、svi和以太通道
  7. gitlab邮箱验证 邮箱提醒设置
  8. Android 个人学习笔记- 导入android项目,无法自动生成R文件的解决方法
  9. Kmeans聚类算法分析(转帖)
  10. GBin1分享:jQuery新手技巧之“避免过多使用$('.someclass')”