java - 将 @Resource UserTransaction 注入(inject) EJB Singleton bean 是否安全

标签 java transactions ejb java-ee-6 java-ee-7

Q1: 我正在编写一个使用 BMT 的 EJB Singleton bean,所以我需要在我的 bean 中使用 UserTransaction。我想知道将 @Resource UserTransaction 注入(inject)我的 EJB 单例 bean 是否安全。我不知道容器是否会注入(inject) UserTransaction 的代理。有人可以给我一些想法吗?谢谢!

Q2:

@Resource UserTransaction 和 sessionContext.getUserTransaction() 有什么区别。它们等价吗?

最佳答案

I was wondering that IS IT SAFE to inject @Resource UserTransaction to my EJB singleton bean.

Singleton session bean 是 ejb3.1 specification 中定义的三个 bean 之一。 :无状态,有状态和单例。 因此,对于 Singleton,Container 必须遵循与其他 session bean 相同的契约。

契约(Contract)说:

4.3.3 The SessionContext Interface. If the bean specifies a dependency on the SessionContext interface.., the container must provide the session bean instance with a SessionContext. This gives the session bean instance access to the instance’s context maintained by the container.

.

What are difference between @Resource UserTransaction and sessionContext.getUserTransaction(). Are they equivalent?

是的,他们是。这是规范所说的:

4.3.2 Dependency Injection. Under the EJB 3.1 API, the bean class may acquire the SessionContext interface through dependency injection without having to implement the SessionBean interface. In this case,the Resource annotation (or resource-env-ref deployment descriptor element) is used to denote the bean’s dependency on the SessionContext.

也许你的疑虑来自于同一个单例实例可以被多个并发线程共享, 它将共享相同的 SessionContext 实例。然而。这应该不是问题。 我使用条件形式是因为我还没有尝试过,但是除了可能出现的线程安全相关主题之外,请查看第 117 页的表格 其中明确表示必须支持使用UserTransaction方法

Table 3

Operations Allowed in the Methods of a Singleton Session Bean:

Bean-managed transaction demarcation:

SessionContext methods: UserTransaction methods

关于java - 将 @Resource UserTransaction 注入(inject) EJB Singleton bean 是否安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21410828/

相关文章:

java - 使用代码而不是 beans.xml 注入(inject) @alternative bean

java - EJB 2 无状态 session Bean 中的 Spring session 范围 bean

jakarta-ee - 将 @Transactional 与 EJB 一起使用

java - Gurobi 无法设置或打印(手动)变量值

java - 如何在 Java 程序中访问 SQL count() 查询的值

java - 在Hibernate拦截器的afterTransactionBegin方法中执行查询

WCF、MSMQ 和 2 个队列上的独立事务

Java - 在 .jar 中使用 .txt 文件时出错

java - 如何通过Selenium点击超链接?

sql-server - 何时在 SQL Server 中释放 UPDLOCK?