Commonly used transactional attributes

Category : Spring Boot | Sub Category : Spring Boot | By Prasad Bonam Last updated: 2023-07-17 06:30:14 Viewed : 487


Commonly used transactional attributes:

In Spring, transactional attributes are used to control the behavior and characteristics of transactions managed by the Spring framework. These attributes define how transactions should be initiated, propagated, committed, or rolled back in different scenarios. Transactional attributes are typically applied using annotations such as @Transactional in Spring Boot applications. Here are some commonly used transactional attributes:

  1. propagation - Controls how transactions are propagated when a method is invoked within an existing transaction. Possible values include:

    • REQUIRED: The method should run within an existing transaction. If no transaction exists, a new one will be created.
    • REQUIRES_NEW: The method should always run in a new transaction. If a transaction exists, it will be suspended until the method completes.
    • SUPPORTS: The method can run within a transaction if one exists, or it can run without a transaction.
    • NOT_SUPPORTED: The method should run without a transaction. If a transaction exists, it will be suspended until the method completes.
    • MANDATORY: The method should run within an existing transaction. If no transaction exists, an exception will be thrown.
  2. isolation - Specifies the isolation level of the transaction, which determines how the transaction interacts with concurrent transactions. Common values include:

    • DEFAULT: The default isolation level determined by the underlying data source.
    • READ_UNCOMMITTED: Allows dirty reads, non-repeatable reads, and phantom reads.
    • READ_COMMITTED: Allows non-repeatable reads and phantom reads, but not dirty reads.
    • REPEATABLE_READ: Prevents dirty reads and non-repeatable reads, but phantom reads are still possible.
    • SERIALIZABLE: Provides the highest level of isolation by preventing all concurrency-related anomalies.
  3. readOnly - Indicates whether the transaction is read-only or not. A read-only transaction can improve performance and enable certain optimizations.

  4. timeout - Specifies the maximum time in seconds that a transaction should be allowed to run before timing out and being rolled back.

  5. rollbackFor/noRollbackFor - Defines exceptions that should trigger a transaction rollback or exceptions that should not trigger a rollback.

These attributes can be applied at the method or class level using the @Transactional annotation. For example:

java
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, readOnly = true, timeout = 30) public void performTransactionalOperation() { // Transactional method implementation }

It is important to understand and choose the appropriate transactional attributes based on the specific requirements of your application and the behavior you want for your transactions.



Search
Sub-Categories
Related Articles

Leave a Comment: