Transaction Attributes
In Enterprise applications, if a method begins a transaction and access another method, in this context, another method execution is going on the same Transaction or in any new Transaction is completely depending on the Transaction Propagation behavior what we are using.
Spring Framewrk has provided very good suport for Propagation Behavior in the form of the following constants from “org.springframework. transaction.TransactionDefinition”.
- PROPAGATION_REQUIRED
- PROPAGATION_REQUIRES_NEW
- PROPAGATION_SUPPORTS
- PROPAGATION_NOT_SUPPORTED
- PROPAGATION_MANDATORY
- PROPAGATION_NEVER
- PROPAGATION_NESTED
1. PROPAGATION_REQUIRED
If a method1 is running in a transaction and invokes method2 then method2 will be executed in the same transaction. If method1 is not associated with any Transaction then Container will create new Transaction to execute Method2.
2. PROPAGATION_REQUIRES_NEW
If a method1 is running in a transaction and invokes method2 then container will suspend the current Transaction temporarily and creates new Transaction for method2. After executing method2 transaction then method1 transaction will continue. If Method1 is not associated with any transaction then container will start a new transaction before starts new method.
3. PROPAGATION_MANDATORY
If a method1 is running in a transaction and invokes method2 then method2 will be executed in the same transaction. If method1 is not associated with any Transaction then Container will raise an exception like “TransactionThrowsException”.
4. PROPAGATION_SUPPORTS
If a method1 is running in a transaction and invokes method2 then method2 will be executed in the same transaction. If method1 is not associated with any Transaction then Container does not start new Transaction before running method2.
5. PROPAGATION_NOT_SUPPORTED
If a method1 is running in a transaction and invokes method2 then Container Suspends the Mehod1 transaction before invoking Method2. When Method2 has completed, container resumes Method1 transaction. If Mehod1 is not associated with Transaction then Container does not start new Transaction before executing Method2.
6. PROPAGATION_NEVER
If a method1 is running in a transaction and invokes method2 then Container throws an Exception like RemoteException. I mthod1 is not associated with any transaction then Container will not start new Transaction for Method2.
7. PROPAGATION_NESTED
Indicates that the method should be run with in a nested transaction if an existed transaction is in progress.
To set the above Propagation Behavior to TransactionTemplate then we have to use the following method.
public void setPropagationBehaviour(int value)
E.g
txTemplate.setPropagationBehaviour(TransactionDefinition.PROPAGATION_EQUIRES_NEW);