users@glassfish.java.net

EntityManager Lookup via jndi

From: <glassfish_at_javadesktop.org>
Date: Wed, 10 Dec 2008 21:10:23 PST

How can I obtain the EntityManager by doing a JNDI lookup in Glassfish?

This is possible in JBoss by setting
< property name="jboss.entity.manager.jndi.name" value="java:/EntityManager001"/>

But I am not able to find out how to do it in Glassfish.

Our requirement is the following:

We have multiple databases ( databases can be added/deleted). At run time depending on the user logged in, we have to decide to which database an entity should be persisted .
So, we can not use annotations as it needs changing of the code whenever a database is added/deleted along with the persistence.xml.

We were able to achieve this in JBoss by using
< property name="jboss.entity.manager.jndi.name" value="java:/EntityManager001"/> in the persistence unit definition and at run time looking up for that entity manager via a utility method from the session bean.
The following is the utility method.

public EntityManager getEntityManager(String entityManagerName) throws Exception {
           EntityManager em = null;
        try {
            em = (EntityManager)(new InitialContext()).lookup(entityManagerName);
        } catch (Exception e) {
            throw e;
        }
        return em;
    }

Any idea how to achieve this in Glassfish?
[Message sent by forum member 'ksvcu' (ksvcu)]

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