通俗地说春天繁殖的例子 [英] Spring Propagation examples in layman's terms

查看:135
本文介绍了通俗地说春天繁殖的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://docs.spring.io/spring/docs/1.2.x/api/org/springframework/transaction/annotation/Propagation.html\">The春天文档做描述事务传播特性了出色的工作。

不过,我想知道是否有任何知名的,真实世界的实例,可以用于描述通俗地说更彻底每个属性的?


解决方案

PROPAGATION_REQUIRED

 类服务{
    @Transactional(传播= Propagation.REQUIRED)
    公共无效DoSomething的(){
        //访问使用DAO数据库
    }
}

当DoSomething的()被调用,它将开始一个新的事务的如果主叫方还没有开始交易

如果该方法的呼叫者已经开始一个事务那么呼叫者的事务用于与任何新的事务被创建(即没有在播放一个事务)。

如果有异常里面的doSomething抛出(),那么它就会被回滚,即主叫方也将看到事务回滚。

当DoSomething的()返回该交易将还没有被COMMITED。它是将提交事务(也可能回滚)调用者。

PROPAGATION_REQUIRES_NEW

 类服务{
    @Transactional(传播= Propagation.REQUIRES_NEW)
    公共无效DoSomething的(){
        //访问使用DAO数据库
    }
}

当DoSomething的()被调用它会的总是的开始一个新的事务。

如果这个方法的调用者已经开始交易(TxnOuter),那么呼叫者的交易的暂停的和一个新的事务(TxnInner)创建(即有两笔交易中播放)。

如果有异常里面的doSomething抛出(),然后TxnInner将回滚,但是从主叫方(TxnOuter)中的暂停交易不受影响。

当DoSomething的()返回无异常,将提交事务(TxnInner)。来电者的交易(TxnOuter)予以恢复,并没有察觉到另一个事务被COMMITED。然后调用者可以提交或回滚TxnOuter它认为合适的。

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

PROPAGATION_NESTED

 类服务{
    @Transactional(传播= Propagation.NESTED)
    公共无效DoSomething的(){
        //访问使用DAO数据库
    }
}

只能如果你的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.

这篇关于通俗地说春天繁殖的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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