UserTransaction 如何传播?

发布于 2024-10-01 04:42:50 字数 530 浏览 9 评论 0原文

我有一个带有 bean 管理事务的无状态 bean,以及一个如下所示的方法:

@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?

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();

    }

}

So how does the UserTransaction propagate to the OtherStatelessBeanLocal bean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢你一世 2024-10-08 04:42:50

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

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

安全传播的工作方式与 JNDI 查找类似,它神奇地指向正确模块或组件的 java:comp/env 命名空间。最重要的是,如果没有 ThreadLocals,您就无法实现应用程序服务器。传播听起来比实际情况更活跃,但实际上它只是不离开线程的行为,因此容器和所有相关人员仍然可以找到您的“东西”。

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

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()

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.

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".

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.

负佳期 2024-10-08 04:42:50

根据 EJB 规范,您无法使用编程事务将事务上下文从一个 bean(在本例中为您的主类...)传递到另一个使用编程事务的 bean(在本例中为 other)

Based on EJB specification, you can not pass a transaction context from a bean (in this case your main class ... ) using programmatic transaction into another bean (in this case, other) using programmatic transaction

你在看孤独的风景 2024-10-08 04:42:50

对于 EJB3,您通常使用 @TransactionAttribute 注释定义事务传播。

所有 EJB 3.0 应用程序的默认事务属性都是必需的:

如果客户端在与事务上下文关联时调用企业 Bean 的方法,则容器将在客户端的事务上下文中调用企业 Bean 的方法。

交易类型的文档位于:http://download.oracle .com/javaee/6/api/javax/ejb/TransactionAttributeType.html

注意:持久化上下文和事务传播通常同时发生,但并不总是一起发生 - 请注意。例如,有状态会话 Bean 可能具有扩展持久性上下文

For EJB3 you normally define transaction propagation with the @TransactionAttribute annotation.

The default transaction attribute for all EJB 3.0 applications is REQUIRED:

If a client invokes the enterprise bean's method while the client is associated with a transaction context, the container invokes the enterprise bean's method in the client's transaction context.

The doc's for transaction type are here: http://download.oracle.com/javaee/6/api/javax/ejb/TransactionAttributeType.html

N.B. Persistence context and transaction propagation typically happen together but not always - beware. For example, stateful session beans may have an extended persistence context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文