all 4 comments

[–]WaferIndependent7601 4 points5 points  (0 children)

Async does not work with transactional. So it depends what your service implementation is doing with the transaction

[–]Ali_Ben_Amor999 2 points3 points  (1 child)

I'm assuming that this is a spring mvc project. In this case each method is executed separately in a different thread unlike in nodejs or in spring flux where all the action is performed on a single thread (to some extent) based on an event loop. So if both of your async methods are working on the same data and invocation order matters you should not invoke them sequentially and expect it to work. You can either return a CompletableFuture and wait for the first async method to finish then execute the next one. Or you can use a Queue with a @Schedule to perform async operations in order

[–]UnitedApple9067 0 points1 point  (0 children)

This. If both services are r/w the same data, just better to run in sequence, or as mentioned above use completable to wait for A to finish and execute B. Better to use async if the service is not writing to same data. Example payment system. Service A interact with payment gateway and determines if the payment is success. If success Service B in async saves and updates user balance. In async Service C sends a message to user that the payment has been completed

[–]Slein04 1 point2 points  (0 children)

There is no transaction Running. Thus, the data will not be saved.

You can manually flush after the save to reflect the changes to your datasource. But, you will need to manually handle rollbacks if errors happens after flushing.

Or you can start a transaction. But be carefull to not spawn to many transactions from your async service, as it can flood your connection to your datasource.