What is @Transactional

person shubham sharmafolder_openJAVA, Spring Bootlocal_offer, access_time November 22, 2024

@Transactional is an annotation in Spring Framework that provides declarative transaction management. It is used to define the transactional behavior of a method or class, ensuring that database operations within the annotated scope are executed as part of a single transaction.

Key Features of @Transactional

  1. Atomicity: Ensures that all operations within a transaction are treated as a single unit of work—either all succeed or all fail.
  2. Declarative Transaction Management: No need to write boilerplate code for managing transactions; Spring handles it automatically.
  3. Rollback: Automatically rolls back the transaction if an exception occurs during execution.
  4. Integration with Persistence Frameworks: Works seamlessly with JPA, Hibernate, JDBC, and other persistence frameworks.

Common Usage

  1. On Methods:
    When applied to a method, the transaction begins when the method starts and commits or rolls back when the method finishes.

  2. On Classes:
    When applied to a class, all methods in the class inherit the transactional behavior.


How Does It Work?

Spring wraps the annotated method in a proxy. The proxy intercepts method calls and:

  1. Begins a transaction before the method execution.
  2. Commits the transaction if the method completes successfully.
  3. Rolls back the transaction if an exception occurs.

Key Attributes of @Transactional

  1. propagation: Defines how the transaction is propagated across method calls.

    • REQUIRED (default): Uses the existing transaction or creates a new one if none exists.
    • REQUIRES_NEW: Suspends the current transaction and creates a new one.
    • NESTED: Creates a nested transaction.

  2. isolation: Sets the isolation level for the transaction, controlling how changes in one transaction are visible to others.

    • READ_UNCOMMITTED, READ_COMMITTED (default), REPEATABLE_READ, SERIALIZABLE.

  3. readOnly: Optimizes transactions that perform only read operations (e.g., skips locking in some databases).

  4. timeout: Sets the maximum duration for a transaction in seconds. If exceeded, the transaction is rolled back.

  5. rollbackFor and noRollbackFor: Specify exceptions that should or should not trigger a rollback.


Example: Using @Transactional in a Service

  • If email is null, the exception triggers a rollback, and the user is not saved to the database.

Rollback Behavior

By default:

  • Checked Exceptions: Do not trigger a rollback.
  • Runtime Exceptions (unchecked): Trigger a rollback.

You can customize this behavior using rollbackFor and noRollbackFor.


Summary

@Transactional in Spring Boot simplifies transaction management by:

  • Automatically handling transaction start, commit, and rollback.
  • Supporting custom configurations like propagation, isolation, and timeouts.
  • Integrating seamlessly with Spring Data, JPA, and Hibernate.

This declarative approach eliminates boilerplate code and ensures consistency and data integrity in database operations.

warningComments are closed.