UserTransaction 如何传播?
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UserTransaction 对象是容器提供的对象,它包装了对容器内部使用的 API 调用的访问,特别是 javax.transaction.TransactionManager。
TransactionManager
具有begin
、commit
、rollback
和javax.transaction.Transaction getTransaction() 等方法
在幕后,TransactionManager 将使用 ThreadLocal 或类似的技术来跟踪线程的当前事务状态。 ThreadLocals 是非常简单的对象,可以轻松地描述为静态 HashMap,它使用线程名称作为键,使用您选择的对象作为值。只要留在同一个线程中,就可以从调用链中的任何点获取对象。这也是Java EE环境中不允许启动线程的原因之一。
安全传播的工作方式与 JNDI 查找类似,它神奇地指向正确模块或组件的
java:comp/env
命名空间。最重要的是,如果没有 ThreadLocals,您就无法实现应用程序服务器。传播听起来比实际情况更活跃,但实际上它只是不离开线程的行为,因此容器和所有相关人员仍然可以找到您的“东西”。回到事务管理术语,TransactionManager 在其 ThreadLocal 中跟踪的对象通常会(直接或间接)实现 Transaction 和 TransactionSynchronizationRegistry 接口。在这两个接口之间,容器具有代表您跟踪当前事务中的 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. TheTransactionManager
has methods likebegin
,commit
,rollback
andjavax.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
DataSource
s,EntityManager
s 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.根据 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
对于 EJB3,您通常使用 @TransactionAttribute 注释定义事务传播。
所有 EJB 3.0 应用程序的默认事务属性都是必需的:
交易类型的文档位于: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:
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.