4 Replies Latest reply on Feb 19, 2009 4:38 PM by Rick Kilcoyne

    Seam App Design

    Rick Kilcoyne Newbie

      I'm working on an app that utilizes SLSB service classes that encapsulate any interaction with the database back-end via JPA.


      One issue I keep running into with this design is that when called from Asynchronous events, my Service SLSBs don't have access to the extended persistence context and I start getting LIEs.


      Is there any solution to wrapping an asynchronous method in an extended persistence context that propagate to my Service beans? As it stands now, I have to assume that there won't be an extended persistence context available when writing my service methods which results in less elegant and maintainable code.


      Thanks,
      Rick

        • 1. Re: Seam App Design
          Stuart Douglas Master

          @Asynchronous events do not have access to the conversation or session scope, which makes it very hard to get access to the extended persistence context.


          In practice even if you did manage to get access to it through some hack you have to consider that EntityManagers are not thread safe, so you would have to serialize all your asynchronous requests anyway (and all normal http requests in the same conversation), which defeats the purpose of running them asynchronously and would result in way less manageable code than the alternative.

          • 2. Re: Seam App Design
            Bernard Labno Master

            EJB3 specification says you cannot use extended persistent context in SLSB nor in MDB.


            Apart from extended context from EJB3 there is Seam Managed Persistence Context, which you may access from all components.


            Your problem might be that the async method is not surrounded with transaction, which does not distrub you from retriving entities via EntityManager but they become detatched right after that.

            • 3. Re: Seam App Design
              Stuart Douglas Master

              Do not try and use the seam managed persistence context in an async method for the following reasons:


              - You do not have access to the conversation context, you would have to pass it in as a method parameter


              - If you do get access to it then what happens if the user ends the conversation before the async method has finished (extremely likely if there is not long running conversation)? The entityManager will be closed and everthing will come to a halt.


              - As I said before it is not thread safe, and will probably cause all kinds of fun bugs (you can get thread safe entitymanagers by wrapping them in a proxy that serialises all requests, but this still just feels like a bad idea).


              async methods should almost always use a transaction scoped entityManager

              • 4. Re: Seam App Design
                Rick Kilcoyne Newbie

                Got it -- thanks Gentlemen!