Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problems encountered in using @ Transactional annotations in SpringBoot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to solve the problems encountered in the use of @ Transactional annotations in SpringBoot". The content is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to solve the problems encountered in the use of @ Transactional annotations in SpringBoot".

Problems encountered with @ Transactional annotations

1. It is not recommended to add @ Transactional annotation to the interface. Generally, you can add @ Transactional to the service class tag.

2. @ Transactional annotations can only be applied to public visibility methods. If applied to protected, private, or package visibility methods, no errors will be reported, but transactions will not work either

3. By default, spring will roll back the uncheck exception; if it is an checked exception, it will not be rolled back. You can add @ Transactional (rollbackFor=Exception.class) to roll back the checked exception.

Uncheck exception: an exception in java that will be derived from Error or RuntimeException (such as null pointer, 1Universe 0)

Checked exception: other exceptions inherited from java.lang.Exception are collectively referred to as Checked Exception, such as IOException, TimeoutException, etc. 4. The database engine needs to support transaction management. If it is mysql, note that the table should use the transaction engine, such as innodb. If it is myisam, the transaction will not work.

5. In the same class, the methodA () method has no @ Transactional annotation, and the method with @ Transactional annotation is called internally. The transaction of the method methodB () with @ Transactional annotation is ignored, no new transaction will be started, and no rollback will occur. For example:

@ Servicepublic class TransactionService {public void methodA () {this.methodB ();} @ Transactionalpublic void methodB () {}} reason

Spring uses dynamic proxy (AOP) to manage and slice bean, which generates a proxy object for each class. Aspect logic can be triggered only when calls are made between proxy objects. Under the AOP proxy of Spring, the target method is managed by the proxy object generated by Spring only if the target method is called externally.

Detailed explanation

Spring's transaction management is implemented through AOP, and its AOP is implemented through cglib for non-final classes, that is, a subclass of the current class is generated as a proxy class, and then when the method under it is called, it will determine whether the method has @ Transactional annotation, if so, it will open a new transaction and implement transaction management through dynamic proxies (intercepting method calls, executing transactions, etc.).

When methodB () is called in methodA (), it is not a proxy object used, but a normal javabean, resulting in this.methodB () not being a code object, resulting in @ Transactional failure, that is, finding that there is no @ Transactional annotation on methodA (), so the whole AOP proxy process (transaction management) will not occur.

Solution.

1. Separate the two methods into different classes

Add the annotation @ Transactional to the class name

3. Add the annotation @ Transactional to the methodA () method. MethodB () does not add a comment, and the transactions of both methods will take effect when methodB () is called, because the default transaction propagation property of methodA () is PROPAGATION_REQUIRED, and methodB () will be added to methodA ().

@ Servicepublic class TransactionService {@ Transactionalpublic void methodA () {this.methodB ();} public void methodB () {}}

4. Get the proxy object of this object, and then call it. Specific operations are as follows:

In the context of Spring-content.xml, add configuration:

In TransactionService, use (transactionService) (AopContext.currentProxy ()) to get the proxy class of TransactionService, then call the transaction method, force through the proxy class, and activate the transaction aspect.

Springboot comment transactional invalidation

1. If an exception is caught in the method, the method transaction will not be rolled back.

Method An in this class (with transaction annotations) calls method B in other classes (with transaction annotations, this class is in the container). An exception is caught in method B, and transaction failure cannot be rolled back. If an exception is thrown in catch in method B, it can be rolled back normally.

Method An in this class (with transaction annotations) calls method B in other classes (with transaction annotations, this class is in the container). An exception is caught in method A, and the exception is detected by spring transaction mechanism and rolled back.

Therefore, it is best to throw an exception in the business layer catch for the control layer to catch.

2. The transaction in spring is a proxy mode. In this class, method A calls method B in this class. If there is no transaction comment on method An and there is transaction comment on method B, then there is an exception in method B, the transaction of method B will not be rolled back, and method A will not be rolled back without transaction comments.

3. If method A has transaction comments, method B of this class is called, and the exception of method B can be rolled back as a whole.

4. Method An in this class calls method B in other classes (with transaction comments, this class is in the container). Method B has an exception and can be rolled back.

The above is all the contents of this article entitled "how to solve the problems encountered in using @ Transactional annotations in SpringBoot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report