外行人的 Spring Propagation 示例 [英] Spring Propagation examples in layman's terms

查看:16
本文介绍了外行人的 Spring Propagation 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 文档做了一个描述事务传播属性的出色工作.

然而,我想知道是否有任何众所周知的、真实的例子可以用外行的术语更彻底地描述这些属性中的每一个?

解决方案

PROPAGATION_REQUIRED

class 服务 {@Transactional(propagation=Propagation.REQUIRED)公共无效doSomething(){//使用 DAO 访问数据库}}

当 doSomething() 被调用时,它将开始一个新的事务如果调用者还没有开始一个事务.

如果这个方法的调用者已经开始了一个事务,那么调用者的事务就会被使用并且不会创建新的事务(即有一个事务在进行中).

如果在 doSomething() 中抛出异常,那么它会被回滚,这意味着调用者也会看到事务被回滚.

当 doSomething() 返回时,交易尚未提交.调用者将提交事务(或可能回滚).

PROPAGATION_REQUIRES_NEW

class 服务 {@Transactional(propagation=Propagation.REQUIRES_NEW)公共无效doSomething(){//使用 DAO 访问数据库}}

当 doSomething() 被调用时,它总是开始一个新的事务.

如果此方法的调用者已经启动了一个事务 (TxnOuter),那么调用者的事务将暂停并创建一个新事务 (TxnInner)(即有两个事务在进行中).

如果在 doSomething() 中抛出异常,则 TxnInner 将回滚,但来自调用方 (TxnOuter) 的暂停"事务不受影响.

当 doSomething() 没有异常返回时,它将提交事务 (TxnInner).调用者的事务 (TxnOuter) 将被恢复并且不知道另一个事务已提交.然后调用者可以在它认为合适的时候提交或回滚 TxnOuter.

需要注意的重要一点是,数据库将 TxnOuter 和 TxnInner 视为完全独立的事务,因此是两个独立的提交.

PROPAGATION_NESTED

class 服务 {@Transactional(propagation=Propagation.NESTED)公共无效doSomething(){//使用 DAO 访问数据库}}

NESTED 只能在您的 JDBC 驱动程序和/或数据库支持 JDBC 保存点

当 doSomething() 被调用时,它将开始一个新的事务如果调用者还没有开始一个事务.

如果这个方法的调用者已经开始了一个事务,那么调用者的事务就会被使用并且不会创建新的事务(即有一个事务在进行中).但是,当输入 doSomething() 时,事务上会标记一个保存点".

如果在 doSomething() 中抛出异常,那么事务可以部分回滚到保存点".调用者将继续进行交易.

当 doSomething() 没有异常返回时,调用者将提交整个事务(或回滚).

需要注意的重要一点是数据库只查看一个事务,并且只有一个提交.

The Spring docs do a fantastic job of describing transactional propagation properties.

However, I was wondering if there are any well-known, real-world examples available which describe each of these properties more thoroughly in layman's terms?

解决方案

PROPAGATION_REQUIRED

class Service {
    @Transactional(propagation=Propagation.REQUIRED)
    public void doSomething() {
        // access a database using a DAO
    }
}

When doSomething() is called it will start a new transaction if the caller has not already started a transaction.

If the caller of this method has already started a transaction then the callers' transaction is used and no new transaction is created (i.e. there is one transaction in play).

If an exception is thrown inside doSomething() then it will be rolled back, meaning that the caller will also see the transaction rolled back.

When doSomething() returns the transaction will not have been commited yet. It is the caller that will commit the transaction (or possibly rolled-back).

PROPAGATION_REQUIRES_NEW

class Service {
    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void doSomething() {
        // access a database using a DAO
    }
}

When doSomething() is called it will always start a new transaction.

If the caller of this method has already started a transaction (TxnOuter) then the callers' transaction is suspended and a new transaction (TxnInner) is created (i.e. there are two transactions in play).

If an exception is thrown inside doSomething() then TxnInner will be rolled back, but the "suspended" transaction from the caller (TxnOuter) is unaffected.

When doSomething() returns without an Exception it will commit the transaction (TxnInner). The caller's transaction (TxnOuter) will be resumed and be unaware that another transaction was commited. The caller can then commit or roll-back TxnOuter as it sees fit.

The important point to note is that the Database views TxnOuter and TxnInner as completely independant transactions, and therefore two independant commits.

PROPAGATION_NESTED

class Service {
    @Transactional(propagation=Propagation.NESTED)
    public void doSomething() {
        // access a database using a DAO
    }
}

NESTED can only be used if your JDBC driver and/or database supports JDBC savepoints

When doSomething() is called it will start a new transaction if the caller has not already started a transaction.

If the caller of this method has already started a transaction then the callers' transaction is used and no new transaction is created (i.e. there is one transaction in play). However a "savepoint" is marked on the transaction when doSomething() is entered.

If an Exception is thrown inside doSomething() then the transaction can be partially rolled back the transaction to the "savepoint". The caller will continue with the transaction.

When doSomething() returns without an Exception it is the caller who will commit the entire transaction (or roll back).

The important point to note is that the Database views only one transaction and there is only one commit.

这篇关于外行人的 Spring Propagation 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆