0 Replies Latest reply on Nov 29, 2012 4:29 AM by Miguel Figueiredo

    EntityManager from JNDI: persistence.xml vs @PersistenceContext

    Miguel Figueiredo Newbie

      Hello,

       

      As far as I know there are two ways of registering an entity manager in JNDI:

       

      1. According to JBoss documentation the property jboss.entity.manager.jndi.name declared in persistence.xml "gives you a transaction scoped entity manager you can interact with".

      2. We can also use a stateless bean's @PersistenceContext(name="jndiName") annotation to set an entity manager in JNDI.

       

      I not sure about all the differences between these two ways of doing this.

       

      For instance when using the @PersistenceContext(name="jndiName") annotation on one stateless bean, and try to get the entity manager using JNDI from another stateless bean that doesn't have the @PersistenceContext, I get a javax.naming.NameNotFoundException.

      I guess that the entity manager that is registered in JNDI can't be seen by any other bean except for the one that the declared a persistence context with that jndi name.

      This doesn't happen when using jboss.entity.manager.jndi.name, which seems to expose an entity manager for everyone.

       

      Questions:

       

      1. When using the @PersistenceContext annotation, what happens when two beans use the same JNDI name? Which entity manager is registered in JNDI? Is this something to be avoided?

      2. Are there any other diferences between the two ways of doing this (e.g. performance)?

      3. What is the recommended way?

       

      Best regards,

      Miguel