UserTransaction 如何传播? [英] How does UserTransaction propagate?

查看:11
本文介绍了UserTransaction 如何传播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 bean 管理事务的无状态 bean,还有一个这样的方法:

I have a stateless bean with bean-managed transactions, and a method like this:

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class ... {

    @Resource 
    private UserTransaction ut;
    @EJB 
    private OtherStatelessBeanLocal other;

    public void invokeSomeMethods() 
        ut.begin();
        ...

        // invoke other bean's methods here.
        other.method();

        ...
        ut.commit();

    }

}

那么 UserTransaction 如何传播到 OtherStatelessBeanLocal bean?

So how does the UserTransaction propagate to the OtherStatelessBeanLocal bean?

推荐答案

UserTransaction 对象是容器提供的对象,它封装了对容器内部使用的 API 调用的访问,特别是 javax.transaction.TransactionManager.TransactionManager 具有诸如 begincommitrollbackjavax.transaction.Transaction getTransaction() 等方法

The UserTransaction object is an object supplied by the container which wraps access to API calls that the container uses internally, specifically javax.transaction.TransactionManager. The TransactionManager has methods like begin, commit, rollback and javax.transaction.Transaction getTransaction()

在幕后,TransactionManager 将使用 ThreadLocal 或类似的技术来跟踪线程的当前事务状态.ThreadLocals 是非常简单的对象,可以很容易地描述为 static HashMap,它使用线程名称作为键,并使用您选择的对象作为值.只要保持在同一个线程中,就可以从调用链中的任何一点获取对象.这是不允许在 Java EE 环境中启动线程的原因之一.

Under the covers, the TransactionManager will use a ThreadLocal or similar technique to track the current transaction state with the thread. ThreadLocals are very simple objects that could easily be described as a static HashMap that uses the thread name as the key and an object of your choosing as the value. As long as you stay in the same thread, you can get the object from any point in the invocation chain. This is one of the reasons it is not allowed to start threads in a Java EE environment.

安全传播以类似的方式工作,就像 JNDI 查找一样,神奇地指向正确的模块或组件的 java:comp/env 命名空间.底线是你不能在没有 ThreadLocals 的情况下实现应用服务器.传播听起来比实际更活跃,但实际上它只是不离开线程的行为,因此容器和所有相关人员仍然可以找到您的东西".

Security propagation works in a similar way, as do JNDI lookups which magically point to the right module's or component's java:comp/env namespace. Bottom line is you cannot implement an app server without ThreadLocals. Propagation sounds more active than it is, when in truth it is simply the act of not leaving the thread so the container and all involved can still find your "stuff".

回到事务管理术语,TransactionManager 将在其 ThreadLocal 中跟踪的对象通常会(直接或间接)实现 事务TransactionSynchronizationRegistry接口.在这两个接口之间,容器具有代表您跟踪当前事务中的 DataSources、EntityManagers 和其他资源所需的所有钩子.这些接口还允许容器提供回调,例如 SessionSynchronization,以及意味着在事务完成后代表您做其他事情,例如刷新/关闭 EntityManager、发送 JMS 待处理消息以及在事​​务过程中保留您的应用程序创建的任何计时器.

Back in transaction management terms, the object that a TransactionManager will track in its ThreadLocal will typically implement (directly or indirectly) both the Transaction and TransactionSynchronizationRegistry interfaces. Between these two interfaces, the container has all the hooks it needs to track DataSources, EntityManagers and other resources in the current transaction on your behalf. These interfaces also allow the container to offer callbacks such as SessionSynchronization, as well as means to do other things on your behalf upon transaction completion such as flushing/closing EntityManagers, sending JMS pending messages, and persisting any Timers created by your app in the course of the transaction.

这篇关于UserTransaction 如何传播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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