7 Replies Latest reply on Aug 30, 2009 5:08 PM by henk53

    persistence-context-ref always yields same object instance i

    henk53

      Hi,

      I'm experimenting a little with the different ways of programmatically obtaining an entity manager inside a servlet. I'm running Jboss 5.10 on Mac OS X 10.5.7 using JDK 6.

      I tried the approach of using a persistence-context-ref element in web.xml, basically as outlined here: http://weblogs.java.net/blog/ss141213/archive/2005/12/dont_use_persis_1.html

      My reference looks like this:

      <persistence-context-ref>
       <persistence-context-ref-name>persistence/test</persistence-context-ref-name>
       <persistence-unit-name>testpu</persistence-unit-name>
      </persistence-context-ref>
      


      By itself this seems to work beautifully for doing JNDI lookups in a Servlet or Filter:

      EntityManager entityManager = (EntityManager )new InitialContext().lookup("java:comp/env/persistence/test");
      


      The above code snippet gives me back an instance of the entity manager. However, I discovered with a debugger that this is always the same instance. Whether I do the JNDI lookup multiple times right after each other in the same method, or test what the JNDI lookup returns in multiple simultaneous requests, the result is always an object instance with the same ID (e.g. org.jboss.jpa.tx.TransactionScopedEntityManager@d78f1b).

      When I however bind the entity manager factory into the global JNDI name space using the following property in persistence.xml:

      <property name="jboss.entity.manager.factory.jndi.name" value="java:/test_pu_factory"/>
      


      And lookup that factory using JNDI again and then use that to create an entity manager, the instance is always different.

      As per the advice that an entity manager is not tread-safe (and thus should not be stored in an instance variable of a servlet), I wonder whether it's correct that the JNDI lookup for the entity manager bound via the persistence-context-ref element always returns the same object instance.



        • 1. Re: persistence-context-ref always yields same object instan
          jaikiran

           

          Whether I do the JNDI lookup multiple times right after each other in the same method, or test what the JNDI lookup returns in multiple simultaneous requests, the result is always an object instance with the same ID (e.g. org.jboss.jpa.tx.TransactionScopedEntityManager@d78f1b).


          Hmm, i would have expected it to return different instances in the multiple simultaneous requests usecase.


          • 2. Re: persistence-context-ref always yields same object instan
            henk53

             

            "jaikiran" wrote:
            Whether I do the JNDI lookup multiple times right after each other in the same method, or test what the JNDI lookup returns in multiple simultaneous requests, the result is always an object instance with the same ID (e.g. org.jboss.jpa.tx.TransactionScopedEntityManager@d78f1b).


            Hmm, i would have expected it to return different instances in the multiple simultaneous requests usecase.


            Me too. What I more precisely did was the following simple test:

            1) Put the simple above shown code in a ServletFilter I happened to have on my screen that requested the EM out of JNDI.
            2) Added a break-point to it, requested a page being filtered, inspected the identity of the instance using the Eclipse debugger.
            3) While the request thread was still being suspended, I did a new request (for the same page) in a second tab of the same browser.
            4) Observed the break-point was being hit again, but now in another thread and confirmed that the identity of the instance was the same one as was returned for the currently suspended thread.

            If there is anything else you'd like me to test, please let me know.

            • 3. Re: persistence-context-ref always yields same object instan
              jaikiran

               

              If there is anything else you'd like me to test, please let me know.


              The information you provided should be enough to reproduce this locally. I'll look into this and read a bit more in the spec before updating this thread later this week.


              • 4. Re: persistence-context-ref always yields same object instan
                jaikiran

                Looking into the way this is handled, this appears to be an issue with the web layer where the ENC injections happen only once (during the start). Effectively, the EntityManager is injected once into the ENC of that component. The spec says:

                In general, lookups of objects in the JNDI java: namespace are required to return a new instance of the requested object every time. Exceptions are allowed for the following:

                The container knows the object is immutable (for example, objects of type java.lang.String), or knows that the application cannot change the state of the object.

                The object is defined to be a singleton, such that only one instance of the object may exist in the JVM.

                The name used for the lookup is defined to return an instance of the object that might be shared. The name java:comp/ORB is such a name.


                So this looks like a bug to me. Could you please create an issue in JBAS here https://jira.jboss.org/jira/browse/JBAS

                • 5. Re: persistence-context-ref always yields same object instan
                  henk53

                   

                  "jaikiran" wrote:
                  Looking into the way this is handled, this appears to be an issue with the web layer where the ENC injections happen only once (during the start). Effectively, the EntityManager is injected once into the ENC of that component. The spec says:

                  In general, lookups of objects in the JNDI java: namespace are required to return a new instance of the requested object every time. Exceptions are allowed for the following:

                  The container knows the object is immutable (for example, objects of type java.lang.String), or knows that the application cannot change the state of the object.

                  The object is defined to be a singleton, such that only one instance of the object may exist in the JVM.

                  The name used for the lookup is defined to return an instance of the object that might be shared. The name java:comp/ORB is such a name.


                  So this looks like a bug to me. Could you please create an issue in JBAS here https://jira.jboss.org/jira/browse/JBAS


                  Sure, no problem. Thanks for verifying.

                  • 6. Re: persistence-context-ref always yields same object instan
                    henk53

                    For reference, the JIRA issue was created here: https://jira.jboss.org/jira/browse/JBAS-7218

                    • 7. Re: persistence-context-ref always yields same object instan
                      henk53

                       

                      "henk53" wrote:
                      For reference, the JIRA issue was created here: https://jira.jboss.org/jira/browse/JBAS-7218


                      For the lazy people, the clickable version:

                      https://jira.jboss.org/jira/browse/JBAS-7218