4 Replies Latest reply on Sep 5, 2006 5:55 AM by killme2008

    Problem with lazy initialization - no session or session was

    korg

      Hi,

      previously we used hibernate to manage our transactions and we were responsible of closing sessions in our session beans, so we closed it only when the thread that called the session bean was killed. this way, it was possible for another bean (in another deployed package in jboss) to retrieve entity bean and access fields that were lazy initialized.

      We changed our way of doing things to use EJB EntityManager, but since then it's impossible to access fields that have been lazy initialized. I have entity beans and session beans deployed in a .ejb3 file and business logic beans in another .ejb3 file. One of my BL bean calls a session bean to retrieve a list of entities and when I try to access fields that are lazy initialized, I get the folowing error:

      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.cardinal.dal.entity.infrastructure.PhysicalLocation.a
      ppAccessDeviceVws, no session or session was closed


      I read somewhere that this is the case when I try to access those fields outside of the classloader boundaries, so I tried to deploy my application in an ear file containing all my .ejb3 files and everything required. When I deploy it, I still get the same exception mentionned above.

      Is there a way to access lazy initialized entities outside of my session beans? Is there a way to tell JBoss "hey, this application is in the same class loader than this one", so the session would remain open?

      Thx a lot for help!

        • 1. Re: Problem with lazy initialization - no session or session
          scraatz

          Are you accessing your lazy collections within a single Transaction? If not, that could be your problem...

          • 2. Re: Problem with lazy initialization - no session or session
            a_titov82

            I get this exception when I try to request collection of related entities (using EJB3 relations). When I try to access from local client (session bean) everything is ok. But when I try to access from remote client I get the exception...

            Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: my.Entity1.relatedCollection, no session or session was closed
             at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
             at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
             at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
             at org.hibernate.collection.PersistentBag.size(PersistentBag.java:222)
             at my.TestClient.main(TestClient.java:31)


            Is it possible to access to the collection from remote client at all?

            • 3. Re: Problem with lazy initialization - no session or session
              killme2008

              you have to try "Extended Persistence Contexts and Application Transactions",see the tutorial of jboss-ejb3-doc ,just like:

              @PersistenceContext(type=PersistenceContextType.EXTENDED) EntityManager em;


              by dennis

              • 4. Re: Problem with lazy initialization - no session or session
                killme2008

                 

                "killme2008" wrote:
                you have to try "Extended Persistence Contexts and Application Transactions",read the tutorial of jboss-ejb3-doc ,just like:

                @PersistenceContext(type=PersistenceContextType.EXTENDED) EntityManager em;


                by dennis