users@glassfish.java.net

Re: Lifecycle callbacks and enterprise bean's environment

From: <glassfish_at_javadesktop.org>
Date: Mon, 21 Jan 2008 00:31:39 PST

Hi,

Thanks for your response, but it's not exactly what I asked about.

I want to declare a resource on a ejb bean and use the resource in entity's @PostPersist. According to the spec - [b]EJB 3.0 16.11.1.2 Programming Interfaces for Persistence Context References[/b]:

[i]The Bean Provider must use persistence context references to obtain references to a container-managed entity manager configured for a persistence unit as follows:
* Assign an entry in the enterprise bean's environment to the persistence context reference. (See subsection 16.11.1.3 for information on how persistence context references are declared in the deployment descriptor.)
* The EJB specification recommends, but does not require, that all persistence context references be organized in the java:comp/env/persistence subcontexts of the bean’s environment.[/i]

and [b]JPA 1.0 (EJB 3.0 Persistence) 3.5 Entity Listeners and Callback Methods[/b]:

[i]The following rules apply to lifecycle callbacks:
* Lifecycle callbacks can invoke JNDI, JDBC, JMS, and enterprise beans.
* In general, portable applications should not invoke EntityManager or Query operations,
access other entity instances, or modify relationships in a lifecycle callback method.

When invoked from within a Java EE environment, the callback listeners for an entity share the enterprise naming context of the invoking component.[/i]

It means that although not recommended, the callback listener invoked from within a Java EE env should be able to look up a PU declared in an ejb, e.g.:

@PersistenceContext(name="persistence/InventoryAppMgr", unitName="ear-ejb-beanname-ejbPU")
@Stateless(name="PolskoAngielskiSlownik")
public class PolskoAngielskiSlownik implements Slownik {
...
}

as follows:

    @PostPersist
    protected void postPersist() {
        try {
            ((EntityManager)new InitialContext().lookup("java:comp/env/persistence/InventoryAppMgr")).persist(new WpisDoKapownika("Zapisano " + slowo));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

It appears that GlassFish 2.1 b17 and 2.0 b58g don't obey the rule. Why? Is there other way to work it around or should I forget it's possible in GlassFish?

Jacek
http://www.jaceklaskowski.pl
[Message sent by forum member 'jlaskowski' (jlaskowski)]

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