春天 - @Transactional - 后台会发生什么? [英] Spring - @Transactional - What happens in background?

查看:134
本文介绍了春天 - @Transactional - 后台会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用 @Transactional 注释方法时实际发生了什么?
当然,我知道Spring会在交易中包装该方法。

I want to know what actually happens when you annotate a method with @Transactional? Of course, I know that Spring will wrap that method in a Transaction.

但是,我有以下疑问:


  1. 我听说Spring创建了一个代理类?有人可以用更多深度来解释这一点。 实际存在于该代理类中的是什么?实际班级会发生什么?我怎样才能看到Spring创建的代理类

  2. 我还在Spring文档中读到:

  1. I heard that Spring creates a proxy class? Can someone explain this in more depth. What actually resides in that proxy class? What happens to the actual class? And how can I see Spring's created proxied class
  2. I also read in Spring docs that:




注意:由于此机制基于代理,因此只会截获通过代理进入的外部方法调用。这意味着'自调用',即目标对象中调用目标对象的其他方法的方法,即使被调用的方法标有 @,也不会在运行时导致实际的事务。交易

Note: Since this mechanism is based on proxies, only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

来源: http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html

为什么外部方法调用只在Transaction下而不是自调用方法?

推荐答案

这是一个很大的话题。 Spring参考文档专门用了多章。我建议您阅读面向方面的编程交易,因为Spring的声明性交易支持使用AOP基础。

This is a big topic. The Spring reference doc devotes multiple chapters to it. I recommend reading the ones on Aspect-Oriented Programming and Transactions, as Spring's declarative transaction support uses AOP at its foundation.

但是在非常高的层次上,Spring会为类本身或成员声明 @Transactional 的类创建代理。代理在运行时几乎不可见。它为Spring提供了一种方法,可以在方法调用到被代理对象之前,之后或周围注入行为。事务管理只是可以挂钩的行为的一个例子。安全检查是另一个。您也可以提供自己的日志,例如日志记录。因此,当您使用 @Transactional 注释方法时,Spring会动态创建一个代理,该代理实现与您正在注释的类相同的接口。当客户端调用您的对象时,会拦截调用并通过代理机制注入行为。

But at a very high level, Spring creates proxies for classes that declare @Transactional on the class itself or on members. The proxy is mostly invisible at runtime. It provides a way for Spring to inject behaviors before, after, or around method calls into the object being proxied. Transaction management is just one example of the behaviors that can be hooked in. Security checks are another. And you can provide your own, too, for things like logging. So when you annotate a method with @Transactional, Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.

顺便说一句,EJB中的事务工作方式类似。

Transactions in EJB work similarly, by the way.

正如您所观察到的那样,仅通过代理机制当来自某个外部对象的调用进入时,它会工作当您在对象中进行内部调用时,您实际上正在通过引用进行调用,该引用会绕过代理。但是,有办法解决这个问题。我在此论坛帖子中解释了一种方法,其中我使用了 BeanFactoryPostProcessor 在运行时将代理实例注入自引用类。我将此引用保存为名为 me 的成员变量。然后,如果我需要进行内部调用,需要更改线程的事务状态,我通过代理指示调用(例如 me.someMethod()。)论坛帖子解释更多详情。请注意, BeanFactoryPostProcessor 代码现在会有所不同,因为它是在Spring 1.x时间帧中写回来的。但希望它会给你一个想法。我有一个我可能提供的更新版本。

As you observed, through, the proxy mechanism only works when calls come in from some external object. When you make an internal call within the object, you're really making a call through the "this" reference, which bypasses the proxy. There are ways of working around that problem, however. I explain one approach in this forum post in which I use a BeanFactoryPostProcessor to inject an instance of the proxy into "self-referencing" classes at runtime. I save this reference to a member variable called "me". Then if I need to make internal calls that require a change in the transaction status of the thread, I direct the call through the proxy (e.g. "me.someMethod()".) The forum post explains in more detail. Note that the BeanFactoryPostProcessor code would be a little different now, as it was written back in the Spring 1.x timeframe. But hopefully it gives you an idea. I have an updated version that I could probably make available.

这篇关于春天 - @Transactional - 后台会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆