Methods of Transaction Propagation: Understanding the Types and Their Uses

Tuanh.netTuanh.net
4 min read

1. What is Transaction Propagation?

Transaction propagation determines how transactions are handled across different method calls or services. It defines whether a method should join an existing transaction, create a new one, or participate in a specific way.

1.1 REQUIRED: Join an Existing Transaction or Create a New One

The REQUIRED propagation type is the default and most commonly used. When a method with REQUIRED propagation is called, it will join an existing transaction if one is present. If no transaction exists, a new transaction will be created.

Code Example:

@Service
public class TransactionService {

@Transactional(propagation = Propagation.REQUIRED)
public void performTransactionalOperation() {
// Business logic here
}
}

Demo Result:

Scenario 1: If performTransactionalOperation() is called from another transactional method, it will join the existing transaction.

Scenario 2: If called from a non-transactional context, it will start a new transaction.

1.2 REQUIRES_NEW: Always Create a New Transaction

With REQUIRES_NEW, a new transaction is always started, even if there is an existing transaction. This can be useful when you want certain operations to be independent of the current transaction.

Code Example:

@Service
public class TransactionService {

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void performIndependentOperation() {
// Business logic here
}
}

Scenario: Even if performIndependentOperation() is called within an existing transaction, it will execute in a separate transaction. Changes made in this method are committed or rolled back independently of the outer transaction.

1.3 MANDATORY: Must Join an Existing Transaction

The MANDATORY propagation type requires that a transaction must be present. If no transaction is active, an exception will be thrown. This is used when a method should not be executed without an existing transaction.

Code Example:

@Service
public class TransactionService {

@Transactional(propagation = Propagation.MANDATORY)
public void performOperationWithMandatoryTransaction() {
// Business logic here
}
}

Scenario: If performOperationWithMandatoryTransaction() is called from a non-transactional context, it will throw an exception. If called from within an existing transaction, it will participate in it.

1.4 NEVER: Execute Without a Transaction

The NEVER propagation type specifies that a method must not run within a transaction. If a transaction is active, an exception will be thrown.

Code Example:

@Service
public class TransactionService {

@Transactional(propagation = Propagation.NEVER)
public void executeWithoutTransaction() {
// Business logic here
}
}

Scenario: If executeWithoutTransaction() is called within a transaction, it will throw an exception. If called from a non-transactional context, it will execute normally.

2. How to Choose the Right Propagation Type?

Choosing the right transaction propagation type depends on the specific requirements of your application. Here are some guidelines:

2.1 When to Use REQUIRED

  • Use Case: Most common scenarios where you want methods to either participate in an existing transaction or create a new one if none exists.
  • Example: Updating a user’s profile in a service that should be committed along with other operations in the same transaction.

2.2 When to Use REQUIRES_NEW

  • Use Case: When certain operations need to be committed independently of the main transaction.
  • Example: Logging user actions in a separate transaction to ensure log entries are preserved even if the main transaction fails.

2.3 When to Use MANDATORY

  • Use Case: When you need to ensure that a method is always executed within an existing transaction.
  • Example: Validation or critical operations that must be part of an ongoing transaction.

2.4 When to Use NEVER

  • Use Case: When you need to ensure that a method never participates in a transaction.
  • Example: Read-only operations that should not be affected by transaction management.

3. Conclusion

Understanding the different types of transaction propagation is essential for effective transaction management in Java applications. By choosing the appropriate propagation type, you can ensure that your application handles transactions in the most effective way, meeting both consistency and performance requirements.

Feel free to comment below if you have any questions or need further clarification on transaction propagation types!

Read more at : Methods of Transaction Propagation: Understanding the Types and Their Uses

0
Subscribe to my newsletter

Read articles from Tuanh.net directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tuanh.net
Tuanh.net