Get the App
SLTechnology News&Howtos  ›  Development  › 

The transaction processing method of springboot multiple service calling each other

Shulou Source: shulou.com Published: 2022-06-01 11:54:48 10月03日 Update

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 ("

Tags: Method transaction annotation target agent object process multiple function interface article configuration content time this method but or problem different proper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info MariaDB Microsoft NVidia