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.
来源:https://stackoverflow.com/questions/25076718/spring-propagation-examples-in-laymans-terms