调用 entityManager.getTransaction() 时出现 EJBException [英] EJBException when calling entityManager.getTransaction()

查看:22
本文介绍了调用 entityManager.getTransaction() 时出现 EJBException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是微不足道的事情,但我希望得到一些帮助.

This is probably something trivial, but I'd love some help.

我明白了:

 javax.ejb.EJBException: java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager 
 11:54:37,105 ERROR [STDERR] at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:77)
 11:54:37,105 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
 11:54:37,105 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)

什么时候做:

@PersistenceContext(unitName = "someName")
private EntityManager em;

...

final EntityManager entityManager = getEntityManager();
final EntityTransaction tx = entityManager.getTransaction(); // here

谁能告诉我可能是什么原因?

Can anyone tell me what the cause might be ?

推荐答案

在 Java EE 托管上下文中获取对与 EntityManager 关联的 EntityTransaction 实例的引用是非法的.来自EntityManager.getTransaction():

It is illegal to obtain a reference to the EntityTransaction instance associated with the EntityManager in a Java EE managed context. From the Java EE API documentation of EntityManager.getTransaction():

返回资源级别的 EntityTransaction 对象.这可以使用 EntityTransaction 实例串行开始并提交多个交易.

Return the resource-level EntityTransaction object. The EntityTransaction instance may be used serially to begin and commit multiple transactions.

Returns:
    EntityTransaction instance 
Throws:
    IllegalStateException - if invoked on a JTA entity manager

最后一行与此上下文相关.

The last line is pertinent in this context.

当您使用 @PersistenceContext 或 @Inject 批注将 EntityManager 注入部署在应用程序服务器上的 EJB 中时,EntityManager 将由容器管理,而不是由应用程序管理.容器管理的实体管理器必须是 JTA 实体管理器;应用程序管理的实体管理器可以是资源本地实体管理器.这是由 JPA 规范规定的:

When you inject the EntityManager in an EJB deployed on an application server using the @PersistenceContext or @Inject annotations, the EntityManager will be managed by the container and not by the application. A container managed entity manager must be a JTA Entity Manager; application-managed entity managers can be resource-local entity managers. This is dictated by the JPA specification:

一个实体管理器,其底层交易通过控制JTA 被称为 JTA 实体管理器.

An entity manager whose underlying transactions are controlled through JTA is termed a JTA entity manager.

一个实体管理器,其底层交易由通过申请EntityTransaction API 被称为资源本地实体管理器.

An entity manager whose underlying transactions are controlled by the application through the EntityTransaction API is termed a resource-local entity manager.

一个容器管理的实体管理器必须是 JTA 实体经理. JTA实体管理器仅适用于在 Java EE 容器中使用.

A container-managed entity manager must be a JTA entity manager. JTA entity managers are only specified for use in Java EE containers.

从第一点推断(关于 IllegalStateException),您不得为容器注入的 EntityManager 获取 EntityTransaction 引用.但是,如果容器仅注入 EntityManagerFactory,并且您的应用程序通过调用 EntityManagerFactory.getEntityManager 获得 EntityManager 引用,则您可以这样做.

Inferring from the first point (regarding the IllegalStateException), you must not obtain the EntityTransaction reference for container injected EntityManagers. You may however do so, if the container injected only the EntityManagerFactory, and your application obtained the EntityManager reference by invoking EntityManagerFactory.getEntityManager.

另外需要注意的是,调用EntityManager.getTransaction()对于JTA实体管理器来说是没有意义的.这由 JPA 规范在 EntityTransaction 接口的定义中指明:

Additionally, it should be noted that invoking EntityManager.getTransaction() is meaningless for JTA entity managers. This is indicated by the JPA specification, in the definition of the EntityTransaction interface:

EntityTransaction 接口用于控制资源本地实体管理器上的资源事务.

The EntityTransaction interface is used to control resource transactions on resource-local entity managers.

关于管理 JTA 事务本身的话题,如果您需要自己管理事务边界(即使用 bean 管理的事务),请注入 UserTransaction 实例.或者,如果您希望容器管理事务,则只需使用适当的 TransactionalAttribute 值.

On the topic of managing the JTA transaction itself, if you need to manage the transaction boundaries yourself (i.e. use bean-managed transactions), inject the UserTransaction instance. Or if you wish to have the container manage the transaction, then simply annotate the method or the bean, with the appropriate TransactionalAttribute value.

在应用服务器中将资源本地实体管理器(和数据源)与 bean 管理或容器管理的事务一起使用通常不是一个好主意,但可以做到.

It is usually not a good idea to use resource-local entity managers (and data sources) with bean managed or container managed transactions in an application server, but it can be done.

您将在 Hibernate EntityManager 文档.如果您已经对 bean 类或方法进行了注释,则 CMT 会更加简单;您只需要避免调用 getEntityTransaction() 方法即可让 CMT 工作.

You will find a suitable example demonstrating the use of BMTs with injection of the EntityManager in the Hibernate EntityManager documentation. CMTs are even more trivial if you've already annotated your bean classes or methods; you merely have to avoid invoking the the getEntityTransaction() method for CMTs to work.

如果您想进一步了解,我建议您阅读 JPA 2.0 规范的第 7 章,标题为Entity Managers and Persistence Contexts".本章提供的示例演示了:

If you wish to understand further, I would recommend reading Chapter 7 of the JPA 2.0 specification, titled "Entity Managers and Persistence Contexts". The examples provided in the chapter demonstrate:

  • 应该如何在应用服务器(通常是使用它们的地方)中使用 JTA 实体管理器.
  • 如何在应用服务器中使用资源本地实体管理器.
  • 如何在 Java SE 应用程序中使用资源本地实体管理器.

这篇关于调用 entityManager.getTransaction() 时出现 EJBException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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