users@glassfish.java.net

Re: Lifecycle callbacks and enterprise bean's environment

From: <glassfish_at_javadesktop.org>
Date: Wed, 23 Jan 2008 09:00:36 PST

This may not have anything to do with your problem, but from the spec section 3.5:

[i]The following rules apply to lifecycle callbacks:
• In general, portable applications should not invoke EntityManager or Query operations,
access other entity instances, or modify relationships in a lifecycle callback method.[19]
[/i]

Also, if you are probably running into the case where the transaction that is invoking your lifecycle callback is outside of the invocation of your EJB (ie. the container is invoking the transaction commit after your EJB method has returned) in which case it is really not clear from the spec if the EJB ENC JNDI environment is still accessible. As a test, try putting a "em.flush()" call inside your EJB method and then see if your JNDI references are present in your lifecycle callback.

I would still be leary of using the EntityManager in the lifecycle callback however. It seems a shame that you cannot access other entities besides the one the callback is for as this limits the usefulness of the lifecycle callbacks. In an application that I am working on now, I had to resort to using JMS to send a message to a MDB to perform the task that I wanted to do in my lifecycle callback. Now that I think of it, I probably could have invoked another stateless session bean that had a Transaction.REQUIRES_NEW on it but I did not think of it then.
[Message sent by forum member 'bbergquist' (bbergquist)]

http://forums.java.net/jive/thread.jspa?messageID=255392