EJBException when calling entityManager.getTransaction()

淺唱寂寞╮ 提交于 2019-11-28 00:22:01

问题


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 ?


回答1:


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():

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.

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:

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.

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.

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:

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

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.

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.

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.

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:

  • 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.



回答2:


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.



来源:https://stackoverflow.com/questions/6451846/ejbexception-when-calling-entitymanager-gettransaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!