The principle of

Transactional annotation is a Transactional annotation used in declarative transaction management programming. Spring AOP constructs transaction management implementation statements before and after annotation modification methods, so developers can replace a series of repetitive coding tasks such as transaction start and transaction close with a single annotation.

Add the position

  • Interface implementation class or interface implementation method, but not in the interface class.

  • Access rights: the public method is what matters. The @Transactional annotation should only be applied to public methods because of the nature of Spring AOP.

  • System design: Place tags on methods that require transaction management, rather than on all interface implementation classes: read-only interfaces do not require transaction management. Configuring @Transactional requires AOP to intercept and process transactions, which may affect system performance.

The wrong case

1. Call in the same class

public class A { public void methodA() { methodB(); } @transactional public void methodB() {Transactional public void methodB()}Copy the code

The above example is the wrong Spring AOP-based interception mechanism that would ignore transactions. Solutions are as follows:

@Service@AllArgsConstructorpublic class A { private B b; public void methodA() { b.methodB(); Transactional public void methodB() {Transactional public void methodB() {Transactional public void methodB() {Copy the code

Note that class B here does not use @autowrire; the constructor is automatically injected using Lombok’s @allargsconstructor generation.

2. The @transactional modifier is not public

Public class TransactionalMistake {@transactional private void method() {public class TransactionalMistake {@transactional private void method() {Copy the code

This is what Spring AOP based annotations are supposed to do. This is the easiest, just change the method access type to public.

3. Different data sources

public class TransactionalMistake { @Transactional public void createOrder(Order order) { orderRepo1.save(order); orderRepo2.save(order); }}Copy the code

OrderRepo1 and orderRepo2 in the above example are two different data sources that are connected. By default, such transactions across data sources will not succeed. If you want to implement transactions between multiple data sources, you can introduce JTA.

4. The rollback configuration is incorrect

By default, only RuntimeException and Error are rolled back. If they and their descendants are not abnormal, they will not roll back. If it is not a RuntimeException, but you also want to trigger a rollback, you can use the rollbackFor attribute to specify the exception to rollback.

public class TransactionalMistake {

    @Transactional(rollbackFor = XXXException.class)
    public void method() throws XXXException {

    }

}
Copy the code

5. Database engine does not support transactions

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
Copy the code

The spring.jpa.database-platform configuration here is mainly used to set the dialect used by Hibernate. Mysql > MySQL5InnoDBDialect mysql > MySQL5InnoDBDialect mysql > MySQL5InnoDBDialect mysql > MySQL5InnoDBDialect mysql > MySQL5InnoDBDialect mysql > MySQL5InnoDBDialect mysql > MySQL5InnoDBDialect The MyISAM storage engine has no transactions.

Click the card below/wechat search, follow the public account “Tianyu Creative Music” (ID: GH_CC865e4C536b)

I heard that praise and attention to this number have found a beautiful little sister yo and after the year will enter a million ah!!

This article uses the article synchronization assistant to synchronize