EJBException when calling entityManager.getTransaction()
Asked Answered
I

2

8

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 ?

Interrogate answered 23/6, 2011 at 9:20 Comment(0)
A
19

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.
Act answered 23/6, 2011 at 9:47 Comment(3)
Thanks very much for the exhaustive answer.Interrogate
@Simeon, you're welcome. Unfortunately, my original answer was ambiguous in a few places, and might have been interpreted incorrectly. I've updated it to include where JTA and resource-local entity managers can be used, and what restrictions would therefore apply on the EntityManager.getTransaction() method. It ought to be more clear now.Act
No it cleared things up perfectly, thanks again. I actually stored this answer for later reference :) I managed to do what I want with @TransationAttribute, but now I understand why entityManager.getTransaction(); won't work.Interrogate
D
3

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.

Dromedary answered 23/6, 2011 at 9:53 Comment(1)
I'm trying to persist a Set<Entity> and I wan't to have something like tx.begin(); // iterate and persist the set; tx.commit();. Is there a way to do this without a user managed transaction ?Interrogate

© 2022 - 2024 — McMap. All rights reserved.