views:

1051

answers:

2

I'm aware that JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence) spec states:

"In general, portable applications should not invoke EntityManager or Query operations, access other entity instances, or modify relationships in a lifecycle callback method."

This appears extremely restrictive. We have a situation in which we would like to access the EntityManager from within an EntityListener.

Has anyone come across any adverse effects/pitfulls when using the EntityManager from within a Listener on Jboss/Glassfish, or any other application server for that matter?

+1  A: 

One thing that springs to mind. Since EntityListeners are not EJBs does this mean that the transaction context is not propogated to the EntityListener and thus any database opertation carried out in the Listener is not part of the transaction?

Any input would be very much appreciated!

montyontherun
@montyontherun Hi, According to JPA specification, in Java EE environment, The entity callback methods ARE INVOKED IN THE TRANSACTION AND SECURITY CONTEXTS OF THE CALLING COMPONENT AT THE TIME AT WHICH THE CALLBACK METHOD IS INVOKED. See additional anwer. regards,
Arthur Ronald F D Garcia
+1  A: 

Hi,

In fact, you are right. JPA specification says it and, in that case, it suggests EJB listeners (or Spring AOP in non-JEE environment) instead.

I do not know the purporse of your listener. But only put cross-cutting concerns like logging, authentication, and routing inside it. Hibernate reference documentation, for instance, uses listeners in order to audit log of all object modifications. It could be done by EJB listeners (or Spring AOP in non-JEE environment) once EJB listeners can use entity manager. JEE specification allows it. Therefore, EJB listeners share the the same ENC used by its target EJB and, this way, also share the same persistence context (entity manager).

If you wish, see my profile. I have many ORM related issues.

regards,

Arthur Ronald F D Garcia