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

The transaction processing method of springboot multiple service calling each other

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian for you to introduce in detail the "springboot multiple service mutual call transaction method", the content is detailed, the steps are clear, the details are handled properly, I hope this "springboot multiple service mutual call transaction method" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Transaction processing in which multiple service calls each other

Today, if you want to call method B of another service in method An of service, both method An and method B have database inserts, and the @ Transaction annotation has been added, but when an exception is thrown in method B, the insert statement in method A can still be executed successfully.

The annotation configuration is as follows: @ Transactional (isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED)

I am puzzled. After looking up the relevant information, the problem still lies in the configuration of @ Transaction annotation, which requires configuration exception rollback.

@ Transactional (isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)

In this way, when an exception is thrown in method B, the operation in A will also be rolled back, and the transaction will play a controlling role.

Spring transaction invocation between Service and Service

Different methods of the same class, A method does not have @ Transactional,B method, @ Transactional,A calls B method, transaction does not work

Principle analysis

When spring scans the bean, it scans for @ Transactional annotations on the method. If so, spring dynamically generates a subclass (that is, the proxy class, proxy) for the bean, which inherits the original bean.

At this point, when the annotated method is called, it is actually called by the proxy class, which starts the transaction before it is called.

However, if the annotated method is called by another method in the same class, then the method is called not through the proxy class, but directly through the original bean, so the transaction will not be started, and what we see is that the @ Transactional annotation is invalid.

/ / Interface interface Service {void A (); void B ();} / / Target class, implement interface class ServiceImpl implements Service {/ / no annotation here @ Override public void A () {this.B () } @ Transactional @ Override public void B () {System.out.println ("execute doNeedTx in ServiceImpl");}} / / proxy class, also implement the same interface class ProxyByJdkDynamic implements Service {/ / contains the target object private Service target Public ProxyByJdkDynamic (Service target) {this.target = target;} / / this method is annotated in the target class for special handling @ Override public void B () {/ / open transaction System.out.println ("- > create Tx here in Proxy") / / call the method of the target object, which has already target.B () in the transaction; / / commit the transaction System.out.println ("

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