外行术语中的 Spring 传播示例
Spring 文档做一个描述事务传播属性的出色工作.
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
}
}
当 doSomething() 被调用时,它会开始一个新的事务如果调用者还没有开始一个事务.
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).
如果在 doSomething() 中抛出异常,那么它将被回滚,这意味着调用者也会看到事务被回滚.
If an exception is thrown inside doSomething() then it will be rolled back, meaning that the caller will also see the transaction rolled back.
当 doSomething() 返回时,事务还没有提交.提交事务(或可能回滚)的是调用者.
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
}
}
当 doSomething() 被调用时,它会总是开始一个新的事务.
When doSomething() is called it will always start a new transaction.
如果这个方法的调用者已经开始了一个事务(TxnOuter),那么调用者的事务被挂起并且一个新的事务(TxnInner)被创建(即有两个事务在进行中).
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).
如果在 doSomething() 中抛出异常,则 TxnInner 将被回滚,但来自调用者 (TxnOuter) 的暂停"事务不受影响.
If an exception is thrown inside doSomething() then TxnInner will be rolled back, but the "suspended" transaction from the caller (TxnOuter) is unaffected.
当 doSomething() 返回时没有异常,它将提交事务 (TxnInner).调用者的事务 (TxnOuter) 将被恢复并且不知道另一个事务已提交.然后,调用者可以根据需要提交或回滚 TxnOuter.
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.
需要注意的重要一点是,数据库将 TxnOuter 和 TxnInner 视为完全独立的事务,因此是两个独立的提交.
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
}
}
只有当您的 JDBC 驱动程序和/或数据库支持 JDBC 保存点
NESTED can only be used if your JDBC driver and/or database supports JDBC savepoints
当 doSomething() 被调用时,它会开始一个新的事务如果调用者还没有开始一个事务.
When doSomething() is called it will start a new transaction if the caller has not already started a transaction.
如果这个方法的调用者已经开始了一个事务,那么调用者的事务被使用并且没有新的事务被创建(即有一个事务在运行).但是,当输入 doSomething() 时,事务上会标记一个保存点".
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.
如果在 doSomething() 中抛出异常,则事务可以部分回滚到保存点".调用者将继续进行交易.
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.
当 doSomething() 返回时没有异常,调用者将提交整个事务(或回滚).
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.
相关文章