java 调用 entityManager.getTransaction() 时出现 EJBException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6451846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EJBException when calling entityManager.getTransaction()
提问by Simeon
This is probably something trivial, but I'd love some help.
这可能是微不足道的事情,但我希望得到一些帮助。
I get:
我得到:
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)
when doing:
做的时候:
@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 ?
谁能告诉我可能是什么原因?
回答by Vineet Reynolds
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():
在 Java EE 托管上下文中获取对与 EntityManager 关联的 EntityTransaction 实例的引用是非法的。来自EntityManager.getTransaction()的 Java EE API 文档:
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
返回资源级 EntityTransaction 对象。EntityTransaction 实例可以连续使用以开始和提交多个事务。
Returns: EntityTransaction instance Throws: IllegalStateException - if invoked on a JTA entity manager
The last line is pertinent in this context.
在这种情况下,最后一行是相关的。
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:
当您使用 @PersistenceContext 或 @Inject 批注将 EntityManager 注入部署在应用程序服务器上的 EJB 中时,EntityManager 将由容器管理,而不是由应用程序管理。容器管理的实体管理器必须是 JTA 实体管理器;应用程序管理的实体管理器可以是资源本地实体管理器。这是由 JPA 规范规定的:
An entity manager whose underlying transactions are controlled through JTA is termed a JTA entity manager.
An entity manager whose underlying transactions are controlled by the application through the EntityTransaction API is termed a resource-local entity manager.
A container-managed entity manager must be a JTA entity manager.JTA entity managers are only specified for use in Java EE containers.
其底层事务通过 JTA 控制的实体管理器称为 JTA 实体管理器。
其底层事务由应用程序通过 EntityTransaction API 控制的实体管理器称为资源本地实体管理器。
容器管理的实体管理器必须是 JTA 实体管理器。JTA 实体管理器仅指定用于 Java EE 容器。
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
.
从第一点推断(关于 IllegalStateException),您不得为容器注入的 EntityManager 获取 EntityTransaction 引用。但是,如果容器仅注入 EntityManagerFactory,并且您的应用程序通过调用EntityManagerFactory.getEntityManager
.
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:
另外,应该注意的是,调用EntityManager.getTransaction()
对于 JTA 实体管理器是没有意义的。这由 JPA 规范在 EntityTransaction 接口的定义中指明:
The EntityTransaction interface is used to control resource transactions on resource-local entity managers.
EntityTransaction 接口用于控制资源本地实体管理器上的资源事务。
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 TransactionalAttributevalue.
关于管理JTA事务本身的话题,如果需要自己管理事务边界(即使用bean管理的事务),注入UserTransaction
实例。或者,如果您希望容器管理事务,则只需使用适当的TransactionalAttribute值注释方法或 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.
在应用程序服务器中将资源本地实体管理器(和数据源)与 bean 管理或容器管理的事务一起使用通常不是一个好主意,但可以做到。
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.
您将在Hibernate EntityManager 文档中找到一个合适的示例,该示例演示了通过注入 EntityManager 来使用 BMT 。如果您已经对 bean 类或方法进行了注释,则 CMT 会更加简单;您只需要避免调用getEntityTransaction()
CMT 工作的方法。
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:
如果您想进一步了解,我建议您阅读 JPA 2.0 规范的第 7 章,标题为“实体管理器和持久性上下文”。本章提供的示例演示了:
- how JTA entity managers ought to be used in an application server (which is typically the place where they are used).
- how resource-local entity managers may be used in an application server.
- how resource-local entity managers can be used in a Java SE application.
- JTA 实体管理器应该如何在应用程序服务器(通常是使用它们的地方)中使用。
- 如何在应用服务器中使用资源本地实体管理器。
- 如何在 Java SE 应用程序中使用资源本地实体管理器。
回答by atamanroman
You don't need to instantiate the EntityManager
by hand, your container does that for you because of the
@PersistenceContext
annotation. Also, you don't need to begin the transaction manually, it's also provided by your containter. Just use your em
field and forget about the other ones.
您不需要EntityManager
手动实例化,您的容器会因为@PersistenceContext
注释而为您执行此操作
。此外,您不需要手动开始交易,它也由您的容器提供。只需使用您的em
领域,而忘记其他领域。