Considerations and usage scenarios of transaction management @ Transactional in springboot

  • 2021-07-18 07:53:20
  • OfStack

Using @ Transactional in service layer can realize the mechanism of triggering transaction rollback when database error occurs.

Note:

Spring annotation-based declarative things @ Transactional only roll back runtime exceptions (java. lang. RuntimeException and its subclasses) and Error by default. To support things, the database engine uses InnoDB. @ Transactional can only be applied to public methods. For other non-public methods, if @ Transactional is marked, no error will be reported, but the method has no transaction function.

Specific usage scenarios:

In the service method, the try-catch display is not used to handle exceptions, and it is directly throw new runtimeexcetpion() Transaction rollback can be implemented try-catch is used in the service method, but it needs to be added in catch throw new runtimeexcetpion() Transaction rollback can be implemented Note that when synchronized is added to the method, because the scope of the lock is smaller than the scope of the transaction, the scope of the lock should be modified to ensure that the scope of the lock is larger than the scope of the transaction.

Summarize


Related articles: