java UserTransaction 如何传播?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4118353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:50:12  来源:igfitidea点击:

How does UserTransaction propagate?

javajakarta-eeejbjtabean-managed-transactions

提问by Truong Ha

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

我有一个带有 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();

    }

}

So how does the UserTransactionpropagate to the OtherStatelessBeanLocalbean?

那么如何UserTransaction传播到OtherStatelessBeanLocalbean呢?

回答by David Blevins

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

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

Under the covers, the TransactionManager will use a ThreadLocalor similar technique to track the current transaction state with the thread. ThreadLocals are very simple objects that could easily be described as a static HashMapthat 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.

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

Security propagation works in a similar way, as do JNDI lookups which magically point to the right module's or component's java:comp/envnamespace. 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".

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

Back in transaction management terms, the object that a TransactionManager will track in its ThreadLocal will typically implement (directly or indirectly) both the Transactionand TransactionSynchronizationRegistryinterfaces. 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.

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

回答by vishy

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

基于 EJB 规范,您不能使用编程事务将事务上下文从使用编程事务的 bean(在本例中为您的主类 ...)传递到另一个使用编程事务的 bean(本例中为其他)

回答by johnstok

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

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

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

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

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.

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

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

交易类型的文档在这里: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.

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