users@glassfish.java.net

Re: EntityManager Lookup via jndi

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Fri, 19 Dec 2008 12:35:59 -0800

You should be able to look up EMs from JNDI in Glassfish too. However,
it is not possible to connect to different databases from same application.

glassfish_at_javadesktop.org wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>