Java – executes code before and after the @ transactional method

We have a spring - based application whose service layer is annotated with @ transactional We need to run code before and after some transaction methods for the following reasons:

>We need to synchronize access to the method according to the key Threads need to be blocked before the transaction starts. > If the transaction succeeds, we need to publish a message on the queue

The options appear to be:

>Create a class using a method similar to the service that can run the @ transactional method in the synchronization block, check the return, and then publish the message (a separate class is required due to AOP proxy problems) The service calls the service. It's not good. It feels like a solution. > Write an aspect to surround @ transactional AOP, which can synchronize and publish messages May work, but would rather avoid AOP. > Move the transaction down to the domain layer Due to the way domain methods are reused in different workflows, the current implementation is not desirable or even feasible. > Code the transaction manually in the service method and discard @ transactional

I think this is a fairly common requirement Maybe I missed an option 5, which is obvious!

Solution

Unless you have some specific reasons to avoid AOP, I think I will choose 2 Your question is a typical example that AOP can use, and the results look pretty good This is a good example of how to implement it (if you haven't read it yet): advising transactional operations

If AOP is really not an option, I will choose the 'other' option proposed by @ Lawrence McAlpin

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>