[Java] Points to note when using transaction annotations (@Transactional) in SpringBoot

Points to note when using transaction annotations (@Transactional) in SpringBoot

What is @Transactional

@TransactionalIt is an annotation provided by the Spring framework for declaring transaction boundaries and configuring transaction properties. A transaction is a set of units of work consisting of a series of operations that either all execute successfully or all fail. Transaction processing is a key means to ensure data integrity and consistency, especially when dealing with complex business logic and multiple database operations.
When using annotations in a Spring application @Transactional, Spring will automatically handle the opening, submission, and rollback of transactions based on the configured transaction manager. This allows developers to focus on business logic without caring about the underlying details of transactions.

Points to note when using transaction annotations (@Transactional)

  1. Scope: Make sure to add @Transactionalannotations on classes or methods that need to use transactions. When used at the class level, it applies to all public methods; when used at the method level, it only applies to the annotated methods.
  2. Ensure that the transaction manager is configured correctly: If there are multiple data sources or transaction managers, the transaction manager used needs to be specified correctly. You can @Transactionaladd the transactionManager attribute in the annotation to specify a specific transaction manager.
  3. Propagation behavior (Propagation): Understand different propagation behaviors, such as REQUIRED, REQUIRES_NEW, SUPPORTS, etc., in order to correctly configure transaction propagation behavior according to business needs. The default propagation behavior is REQUIRED.
  4. Isolation level (Isolation): Set the appropriate transaction isolation level according to business needs, such as READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, SERIALIZABLE. By default, SpringBoot uses the default isolation level of the database.
  5. Read-only transaction (readOnly): For methods that only perform query operations , you can @Transactionalset it to read-only (readOnly=true) to improve performance. This prompts the transaction manager to use read-only optimizations where possible.
  6. Timeout setting (timeout): You can set a timeout period for the transaction to prevent the transaction from blocking for a long time. If the transaction does not complete within the specified time, the transaction will be rolled back and an exception will be thrown.
  7. Rollback rules (rollbackFor and noRollbackFor): Know when to rollback a transaction. By default, SpringBoot rolls back transactions on runtime exceptions (RuntimeException) and errors (Error). Rollback rules can be customized using the rollbackFor and noRollbackFor attributes.
    Supplement: SpringBoot transaction rollback rules
  8. Proxy mode: SpringBoot supports two transaction proxy modes, JDK dynamic proxy and CGLIB proxy. By default, SpringBoot uses the JDK dynamic proxy if the target class implements the interface; otherwise, it uses the CGLIB proxy. It is important to note that calling a method inside a proxy class may not trigger a transaction.
  9. AOP order: If you use @Transactionalother custom AOP interceptors at the same time in the project, you need to pay attention to the execution order of the interceptors. You can control the execution order of interceptors by setting the order attribute.
  10. Database support: Make sure the database you are using supports transactions. Most relational databases support transactions, but some NoSQL databases may not.

Class level: When @Transactionalannotations are applied to a class, all public methods of that class will be subject to transaction management. This means that these methods will be executed in a new transaction context.

Method level: When @Transactionalannotations are applied to a specific method, only that method will be subject to transaction management. This allows you to set different transaction properties for different methods, such as propagation behavior, isolation level, etc.

When used @Transactional, you can configure transaction-related characteristics through its properties, such as: propagation behavior (propagation), isolation level (isolation), read-only transaction (readOnly), timeout setting (timeout), rollback rules (rollbackFor and noRollbackFor )wait. This makes transaction processing more flexible and controllable.

If there are any mistakes, please let me know!
When reprinting or quoting the content of this article, please indicate the source and original author: Juzu Qingzhong;

Guess you like

Origin blog.csdn.net/weixin_44510587/article/details/129918036