2 Replies Latest reply on Jan 4, 2010 10:37 AM by blabno

    Session scoped persistence context (SMPC)

    hendinas.hendinas.gmail.com

      Hi All,


      In my application I have a Session scoped SFSB, because the use case aligns with the Session scope, not a conversation. After navigating to another page in the application from the one where the Session bean is first used, the SFSB tries to access some lazy associations, but encounters a LazyInitializationException (actually the code that accesses the associations is in a helper bean).


      Because Im using Seam managed persistence context, and this is associated with the conversation, Im guessing that the LIE is because the 'short running conversation' has ended when I navigated to another page? So I tried making the SMPC session scoped, as I thought this would solve the problem (not sure if its a good idea)


      When I did this (by simply putting scope="SESSION" on the managed-persistence-context in components.xml) When I start JBoss I get a NullPointerException the first time the EntityManager is used. (the EntityManager itself appears to be null).


      This can be recreated with the dvdstore seam example, by changing all @PersistenceContext to @In EntityManager and setting the managed-persistence-context in components.xml to scope="SESSION".


      Is there something else I have to do to make Session scoped SMPC work? Is it possible? or is it a bad idea?

        • 1. Re: Session scoped persistence context (SMPC)
          asookazian
          /**
           * A Seam component that manages a conversation-scoped extended
           * persistence context that can be shared by arbitrary other
           * components.
           * 
           * @author Gavin King
           */
          @Scope(ScopeType.CONVERSATION)
          @BypassInterceptors
          @Install(false)
          public class ManagedPersistenceContext 
             implements Serializable, HttpSessionActivationListener, Mutable, PersistenceContextManager, Synchronization
          {...}



          SMPC is conversation-scoped.

          • 2. Re: Session scoped persistence context (SMPC)
            blabno

            It is a terrible idea from performance point of view. Although you could make your own entity manager factory which would put entity manager in session scope (if you really cannot reconfigure the one Seam provides).


            Your case is that entities are being detatched after first request that retrives them ends (cause entity manager gets closed and next request will use different one). You could :



            1. call entityManager.merge at the begining of each request,

            2. initialize needed associations during first request or mark them as EAGER

            3. do not access associations via #{mySessionScopedEntity.someAssociation} but rather make a list component (i.e. EntityQuery) that will retrive associated items (this is good for viewing associated items, but updating them will outdate your sessionScopedEntity, which you would have to refresh)