The Use of Asynchronous Transactions

Posted By : Dhiraj Chauhan | 29-Dec-2022

Java java microservices Spring boot

Loading...

Introduction

The method for asynchronous execution should first be proposed independently to create a class. Write about your approach in class. To indicate that a method will be executed asynchronously, you can annotate it with the @Async keyword, or you can place it on the class's main method head. The methods in this class will show as asynchronous methods called asynchronous methods after adding the @Async annotation, which indicates that all of the methods in this class are executed asynchronously. Therefore, I advise adding asynchronous annotations to the method.

The class you describe must be injected into the spring container in order to become a bean of spring, therefore you must include @Component; otherwise, the class won't be found during execution!

This will synchronously and gladly save time!

When you use it, you'll discover - -!! There appears to be no change, and the situation is also asynchronous! Actually, you did not perform any asynchronous execution at all.

To enable asynchronous, we must add the line @EnableAsync to the main starting class. The time to go asynchronous is now!

So, when utilizing asynchronous, take care to

1. In order to become a Spring Bean, the individually proposed class needs to have the annotation @Component.

2. To initiate the asynchronous moment, add the line @EnableAsync to the main startup class.

Use of asynchronous transactions

When conducting business, we must ensure atomicity and uniformity.

Then you must add @Transactional transaction processing, which performs rollback processing when an exception occurs.

Method A now invokes Method B. The asynchrony of procedure B is for efficiency. Atomicity is necessary to guarantee data consistency.

outcomes in the following cases:

1. @Transactional transaction was added to method A but not method B,

When this happens, the business that was being carried out in A is rolled back, but no rollback is performed in B (remember that B is asynchronous).

When AB rolls back, just A is aberrant.

A is run normally, B does not finish abnormally, and there is no rollback even if B is anomalous.

2. A and B methods now include @Transactional transactions:

A is anomalous at this moment, but after A rolls back, B executes normally.

A executes normally, B rolls back, and only B is odd.

By the way, utilizing asynchronous is the same as employing several execution threads. If method B is invoked while method A is still running, method B will run after method B. Since there is only one thread, method B is asynchronous and executed simultaneously to save time. There are two things going on with this multi-threaded procedure.

Conclusion

Asynchronous threads can still be referred to as asynchronous because all transactions between them are totally separated.

We are a 360-degree ERP development company that specializes in building custom enterprise solutions from the ground-up at cost-effective rates. To learn more about our custom ERP development services, reach us at [email protected].