[英]Executing code before and after @Transactional method
We have a Spring based application with a service layer which is annotated with @Transactional.我们有一个基于 Spring 的应用程序,其服务层使用@Transactional 进行注释。 We need to have code run before and after some transactional methods for the following reasons:
我们需要在一些事务方法之前和之后运行代码,原因如下:
The options seem to be:选项似乎是:
I would imagine this is a fairly common requirement.我想这是一个相当普遍的要求。 Probably I am missing an option 5, which is the obvious one!
可能我错过了选项 5,这是显而易见的选项!
I think I'd go with 2 unless you have some specific reasons to avoid AOP.我想我会用 2 go 除非你有一些具体的理由来避免 AOP。 Your problem is a classic example of where AOP can be used and it looks pretty good in the result.
您的问题是可以使用 AOP 的经典示例,结果看起来相当不错。 Here is a nice example of how to implement that (if you didn't read that already): Advising transactional operations
这是一个很好的例子来说明如何实现它(如果你还没有读过): Advising transactional operations
If AOP is really not an option, I'd go the 'Otherwise' option proposed by @Lawrence McAlpin.如果 AOP 真的不是一个选项,我会 go @Lawrence McAlpin 提出的“否则”选项。
Check out TransactionSynchronization callback interface.查看TransactionSynchronization回调接口。 Spring can natively inform you what is happening with your transaction.
Spring 可以本地通知您交易发生了什么。
I would use a TransactionTemplate (your option 4) and programatically control the scope of the transaction in situations like this.在这种情况下,我会使用TransactionTemplate (您的选项 4)并以编程方式控制事务的 scope。
Otherwise, you could move the logic in your method out into a separate service, make that new service @Transactional, remove the @Transactional from the current method, and then surround the call to the new service with your pre- and post-transaction logic.否则,您可以将方法中的逻辑移到单独的服务中,将该新服务设置为 @Transactional,从当前方法中删除 @Transactional,然后使用您的事务前和事务后逻辑围绕对新服务的调用. I've taken this approach as well, but I prefer programmatic transaction management for requirements like this, as I think it's cleaner and, as you mentioned, services calling services (that are only ever needed by the first service) just feels like a hackish workaround.
我也采用了这种方法,但是对于这样的需求,我更喜欢程序化事务管理,因为我认为它更干净,而且,正如您所提到的,服务调用服务(只有第一个服务需要)感觉就像是 hackish解决方法。
if the key is being passed as part of the method call, then you can use java ReentrantLock to do the job.. its much simpler & cleaner.如果密钥作为方法调用的一部分传递,那么您可以使用 java ReentrantLock来完成这项工作。它更简单、更干净。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.