> For the complete documentation index, see [llms.txt](https://thomass-organization-48.gitbook.io/swe/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thomass-organization-48.gitbook.io/swe/main/quickstart-1/transaction.md).

# Transaction

Sure! Below are detailed answers to each of the questions listed above. These answers will help you prepare for your Java Spring Boot technical interview on transactions.

***

#### **Basic Concepts**

1. **What is a transaction in the context of databases and Spring Boot?**
   * A transaction is a sequence of operations performed as a single logical unit of work. In Spring Boot, transactions ensure that a group of database operations either all succeed (commit) or all fail (rollback), maintaining data integrity.
2. **What are the ACID properties of a transaction? Explain each.**
   * **Atomicity**: Ensures that all operations within a transaction are treated as a single unit. Either all succeed or none.
   * **Consistency**: Ensures that the database remains in a valid state before and after the transaction.
   * **Isolation**: Ensures that concurrent transactions do not interfere with each other.
   * **Durability**: Ensures that once a transaction is committed, its effects are permanent, even in the event of a system failure.
3. **What is the role of the `@Transactional` annotation in Spring Boot?**
   * The `@Transactional` annotation is used to declare that a method or class should be executed within a transactional context. It ensures that the method is wrapped in a transaction, and it can be configured with attributes like propagation, isolation, and rollback rules.
4. **How does Spring manage transactions?**
   * Spring manages transactions using the `PlatformTransactionManager` interface. It provides a consistent API for transaction management, regardless of the underlying technology (e.g., JDBC, JPA, Hibernate).
5. **What is the difference between local and global transactions?**
   * **Local transactions**: Limited to a single resource (e.g., a single database). Managed by the resource itself (e.g., JDBC or JPA).
   * **Global transactions**: Span multiple resources (e.g., multiple databases or message queues). Managed by a transaction manager like JTA (Java Transaction API).

***

#### **Transaction Management in Spring Boot**

6. **What are the different transaction management strategies supported by Spring?**
   * **Programmatic**: Manually manage transactions using `TransactionTemplate` or `PlatformTransactionManager`.
   * **Declarative**: Use annotations like `@Transactional` to manage transactions declaratively.
7. **How does Spring Boot integrate with transaction management?**
   * Spring Boot auto-configures a `PlatformTransactionManager` based on the dependencies in the classpath (e.g., `DataSourceTransactionManager` for JDBC, `JpaTransactionManager` for JPA).
8. **What is the default transaction management in Spring Boot?**
   * The default is declarative transaction management using the `@Transactional` annotation.
9. **How do you configure a `PlatformTransactionManager` in Spring Boot?**
   * Spring Boot auto-configures it, but you can customize it by defining a bean in your configuration:

     ```java
     @Bean
     public PlatformTransactionManager transactionManager(DataSource dataSource) {
         return new DataSourceTransactionManager(dataSource);
     }
     ```
10. **What is the difference between `JpaTransactionManager` and `DataSourceTransactionManager`?**
    * `JpaTransactionManager`: Used for JPA-based applications. Manages transactions for JPA entities.
    * `DataSourceTransactionManager`: Used for JDBC-based applications. Manages transactions for plain SQL operations.

***

#### **@Transactional Annotation**

11. **What are the attributes of the `@Transactional` annotation?**
    * `propagation`: Defines the transaction propagation behavior.
    * `isolation`: Defines the isolation level of the transaction.
    * `timeout`: Specifies the maximum time (in seconds) the transaction can take.
    * `readOnly`: Indicates whether the transaction is read-only.
    * `rollbackFor`: Specifies which exceptions trigger a rollback.
    * `noRollbackFor`: Specifies which exceptions do not trigger a rollback.
12. **What is transaction propagation? Explain the different propagation behaviors.**
    * **REQUIRED**: Uses the current transaction or creates a new one if none exists.
    * **REQUIRES\_NEW**: Always creates a new transaction, suspending the current one if it exists.
    * **SUPPORTS**: Executes within a transaction if one exists, otherwise non-transactionally.
    * **NOT\_SUPPORTED**: Executes non-transactionally, suspending the current transaction if one exists.
    * **MANDATORY**: Requires an existing transaction; throws an exception if none exists.
    * **NEVER**: Requires no transaction; throws an exception if one exists.
    * **NESTED**: Executes within a nested transaction if a transaction exists.
13. **What is transaction isolation? Explain the different isolation levels.**
    * **READ\_UNCOMMITTED**: Allows dirty reads, non-repeatable reads, and phantom reads.
    * **READ\_COMMITTED**: Prevents dirty reads but allows non-repeatable reads and phantom reads.
    * **REPEATABLE\_READ**: Prevents dirty reads and non-repeatable reads but allows phantom reads.
    * **SERIALIZABLE**: Prevents dirty reads, non-repeatable reads, and phantom reads.
14. **What happens if you call a `@Transactional` method from a non-transactional method?**
    * A new transaction will be created for the `@Transactional` method, as there is no existing transaction.
15. **What happens if you call a `@Transactional` method from another `@Transactional` method with different propagation behaviors?**
    * The behavior depends on the propagation attribute. For example, if the inner method uses `REQUIRES_NEW`, it will suspend the outer transaction and create a new one.

***

#### **Rollback and Exception Handling**

16. **How does Spring handle rollbacks in transactions?**
    * Spring rolls back a transaction if a runtime exception (unchecked) is thrown. Checked exceptions do not trigger a rollback by default.
17. **Which exceptions trigger a rollback by default in Spring transactions?**
    * Unchecked exceptions (subclasses of `RuntimeException`) trigger a rollback by default.
18. **How can you customize rollback behavior for specific exceptions?**
    * Use the `rollbackFor` and `noRollbackFor` attributes of the `@Transactional` annotation:

      ```java
      @Transactional(rollbackFor = CustomException.class)
      ```
19. **What is the difference between `rollbackFor` and `noRollbackFor` in the `@Transactional` annotation?**
    * `rollbackFor`: Specifies exceptions that should trigger a rollback.
    * `noRollbackFor`: Specifies exceptions that should not trigger a rollback.
20. **What happens if an exception is thrown but not caught within a transactional method?**
    * The transaction will be rolled back, and the exception will propagate to the caller.

***

#### **Advanced Topics**

21. **What is the difference between declarative and programmatic transaction management?**
    * **Declarative**: Uses annotations or XML configuration to define transactions.
    * **Programmatic**: Manually manages transactions using APIs like `TransactionTemplate`.
22. **How do you handle distributed transactions in Spring Boot?**
    * Use JTA (Java Transaction API) with a distributed transaction manager like Atomikos or Bitronix.
23. **What is the role of the `TransactionTemplate` in Spring?**
    * `TransactionTemplate` is used for programmatic transaction management. It simplifies the process of executing code within a transaction.
24. **How do you handle transaction timeouts in Spring Boot?**
    * Use the `timeout` attribute of the `@Transactional` annotation:

      ```java
      @Transactional(timeout = 10)
      ```
25. **What are the best practices for using transactions in Spring Boot?**
    * Keep transactions short and focused.
    * Avoid long-running transactions.
    * Use appropriate propagation and isolation levels.
    * Handle exceptions properly to avoid unexpected rollbacks.

***

#### **Common Pitfalls and Debugging**

26. **What are some common mistakes when using transactions in Spring Boot?**
    * Not marking methods as `@Transactional`.
    * Using incorrect propagation settings.
    * Not handling exceptions properly.
27. **How do you debug transaction-related issues in a Spring Boot application?**
    * Enable debug logging for transaction management.
    * Use tools like Spring Boot Actuator to monitor transactions.
28. **What happens if a transactional method is called from within the same class?**
    * The transactional behavior will not work due to proxy limitations. Use self-injection or move the method to another class.
29. **How do you handle transactions in a multi-threaded environment?**
    * Each thread should have its own transaction. Avoid sharing transactional resources across threads.
30. **What is the impact of long-running transactions on application performance?**
    * Long-running transactions can lead to resource contention, locking issues, and reduced performance.

***

#### **Practical Scenarios**

31. **How would you design a service layer to handle transactions in a Spring Boot application?**
    * Use the `@Transactional` annotation at the service layer to encapsulate business logic within transactions.
32. **How do you handle transactions across multiple microservices?**
    * Use distributed transaction patterns like Saga or eventual consistency.
33. **How do you ensure data consistency when working with multiple databases in a single transaction?**
    * Use JTA or implement compensating transactions.
34. **How do you handle transactions in a batch processing scenario?**
    * Use chunk-based processing with Spring Batch and configure transaction boundaries for each chunk.
35. **What would you do if a transaction fails in the middle of a process?**
    * Implement retry logic or use compensating transactions to handle failures.

***

#### **Code-Based Questions**

36. **Write a simple Spring Boot service method that uses the `@Transactional` annotation.**

    ```java
    @Service
    public class OrderService {
        @Autowired
        private OrderRepository orderRepository;

        @Transactional
        public void createOrder(Order order) {
            orderRepository.save(order);
        }
    }
    ```
37. **Explain the following code snippet:**

    ```java
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
    public void updateOrder(Order order) {
        // Business logic
    }
    ```

    * The method will always execute in a new transaction, suspending any existing transaction. The isolation level is set to `READ_COMMITTED`.
38. **How would you handle a scenario where you need to update two different databases in a single transaction?**
    * Use JTA with a distributed transaction manager.
39. **Write a method that uses `TransactionTemplate` for programmatic transaction management.**

    ```java
    @Autowired
    private TransactionTemplate transactionTemplate;

    public void performTransaction() {
        transactionTemplate.execute(status -> {
            // Business logic
            return null;
        });
    }
    ```
40. **How would you handle a scenario where a transaction needs to be rolled back based on a custom condition?**
    * Use `TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()` to manually trigger a rollback.

***

#### **Best Practices**

41. **What are the best practices for using transactions in Spring Boot?**
    * Keep transactions short and focused.
    * Use appropriate propagation and isolation levels.
    * Handle exceptions properly.
42. **When should you avoid using transactions?**
    * Avoid transactions for read-only operations or operations that do not require atomicity.
43. **How do you ensure that your transactional methods are efficient and scalable?**
    * Optimize database queries and avoid long-running transactions.
44. **What are the trade-offs of using `REQUIRES_NEW` propagation?**
    * It creates a new transaction, which can lead to increased resource usage and potential deadlocks.
45. **How do you monitor and optimize transaction performance in a Spring Boot application?**
    * Use monitoring tools like Spring Boot Actuator and database query profiling.

***

Let me know if you need further clarification or additional examples! Good luck with your interview! 🚀
