简体   繁体   English

UserTransaction如何传播?

[英]How does UserTransaction propagate?

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 UserTransaction propagate to the OtherStatelessBeanLocal bean? 那么UserTransaction如何传播到OtherStatelessBeanLocal bean?

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 . UserTransaction对象是容器提供的对象,它包装对容器在内部使用的API调用的访问,特别是javax.transaction.TransactionManager The TransactionManager has methods like begin , commit , rollback and javax.transaction.Transaction getTransaction() TransactionManager具有begincommitrollbackjavax.transaction.Transaction getTransaction()

Under the covers, the TransactionManager will use a ThreadLocal or similar technique to track the current transaction state with the thread. 在封面下,TransactionManager将使用ThreadLocal或类似技术来跟踪线程的当前事务状态。 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. ThreadLocals是非常简单的对象,很容易被描述为static HashMap ,它使用线程名称作为键,并将您选择的对象用作值。 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. 这是不允许在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/env namespace. 安全传播以类似的方式工作,JNDI查找也神奇地指向正确的模块或组件的java:comp/env命名空间。 Bottom line is you cannot implement an app server without ThreadLocals. 底线是你没有ThreadLocals就无法实现app服务器。 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. 回到事务管理术语,TransactionManager将在其ThreadLocal中跟踪的对象通常(直接或间接)实现TransactionTransactionSynchronizationRegistry接口。 Between these two interfaces, the container has all the hooks it needs to track DataSource s, EntityManager s and other resources in the current transaction on your behalf. 在这两个接口之间,容器具有代表您在当前事务中跟踪DataSourceEntityManager和其他资源所需的所有钩子。 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. 这些接口还允许容器提供回调,例如SessionSynchronization ,以及在事务完成时代表您执行其他操作的方法,例如刷新/关闭EntityManagers,发送JMS挂起消息以及在应用程序中保留应用程序创建的任何计时器交易。

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

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

NB Persistence context and transaction propagation typically happen together but not always - beware. NB持久性上下文和事务传播通常一起发生但不总是 - 要小心。 For example, stateful session beans may have an extended persistence context . 例如,有状态会话bean可能具有扩展的持久性上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM