在容器管理的bean中访问事务状态 [英] Accessing transaction status in container managed beans

查看:126
本文介绍了在容器管理的bean中访问事务状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 @Stateless EJB 使用容器管理的事务。有没有办法访问 javax.transaction.UserTransaction 的状态?也就是说,在bean方法内调用 UserTransaction.getStatus()

I have a @Stateless EJB using container managed transaction. Is there a way to access the "status" of javax.transaction.UserTransaction? That is, calling UserTransaction.getStatus() inside the bean methods?

我知道访问 UserTransaction 在容器管理的bean中是禁止的,但我想知道,有没有其他方法可以访问 getStatus()方法?

I know access to UserTransaction is prohibited in container managed beans, but I would like to know, is there any other way to get access to getStatus() method?

推荐答案

我不认为你已经明白了 UserTransaction 类。不存在为您提供当前正在运行的事务的访问权限。它用于启动与容器的事务管理器的任何通信,特别是用于开始和结束bean管理的事务;这就是为什么你不能从容器管理的事务上下文中访问它。

I don't think you've understood the responsibility of the UserTransaction class. It does not exist to provide you with access to the current running transaction. It is used to initiate any communication with the Transaction Manager of the container, especially for beginning and ending bean-managed transactions; that is why you must not access it from a container managed transaction context.


我想知道,有没有其他方法可以得到访问getStatus()方法?

I would like to know, is there any other way to get access to getStatus() method?

否,至少不能使用EJB API。最多可以使用EJB拦截器记录调用EJB方法的事实。您需要非常聪明地跟踪通话中的状态,然后推断事务状态。

No, one cannot, atleast not using the EJB APIs. One can at most, use EJB interceptors to log the fact that EJB methods have been invoked. You'll need to be quite intelligent to track state across calls, and then infer the transaction state.

如果您不反对使用容器特定的API,则可能是能够访问底层事务。 请注意,如果您不知道如何使用它,或者容器禁止您这样做 ,下面列出的方法可能无法正常工作。下面描述的机制是SpringFramework如何访问JTA事务管理器,并且能够创建和管理事务。

If you are not averse to use Container specific APIs, you might be able to access the underlying transaction. Be forewarned, for the approach listed below might not work if you do not know how to use it, or if the container prohibits you from doing so. The mechanism described below is how the SpringFramework gains access to the JTA transaction manager and is able to create and manage transactions.

在Oracle WebLogic的情况下,可以获得参考 TranactionHelper类 ,可用于获取对当前事务,其 status 可以获得。我会指向 Spring框架中JTA的事务SPI源,如果您需要为其他应用程序服务器执行本课程。

In the case of Oracle WebLogic, one can obtain a reference to the TranactionHelper class, which can be used to obtain a reference to the current transaction associated with the thread, whose status can be obtained. I would point to the sources of the Transaction SPI for JTA in the Spring framework, if you need to pursue this course for other application servers.

这篇关于在容器管理的bean中访问事务状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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