12 Replies Latest reply on Aug 1, 2007 11:12 AM by mangri3000

    Persistence context propagation.

    iradix

      I am coming across a bit of a design dillema and I am curious about what others are doing.

      The issue is that within a long running conversation I might have several SFSB components handling logical chunks of whatever is getting done. Those beans often communicate with each other through the bijection of Entity Beans. The problem is when I switch from one SFSB to another, the PersistenceContext will change and if I create/update an object that references an injected entity, I'll get a detached object exception.

      It seems like what I need is a way to force my application to use a conversation scoped PersistenceContext and I'm not sure of the best way to implement that. I remember from the docs that you can use a seam managed EntityManager, but will that still follow the same rules for context propagation to SLSB DAOs? Anybody have any experience with this issue?

      -Dave

        • 1. Re: Persistence context propagation.
          pmuir

          I use Seam Managed EntityManager for this and it works as you suggest.

          • 2. Re: Persistence context propagation.
            gavin.king

            exactly

            • 3. Re: Persistence context propagation.
              iradix

              Hmm... This doesn't seem to be working the way I hoped it would. Let me explain in a bit more detail.

              I would really rather not tie the SLSB DAOs to Seam so that they are as reusable between projects as possible, therefore they are using the plain vanilla @PersistenceContext annotation. When I invoke a DAO from a SFSB which contains a Seam managed EntityManager the persistence context does not seem to propagate as evidenced by the LazyInitializationException that I'm getting in the subsequent JSF page.

              Since my stateful beans are almost always project specific I have no problem with using Seam enhancements within them. The DAOs are a different story though. Is there any way to mix and match the two types of persistence?

              • 4. Re: Persistence context propagation.
                iradix

                Not sure if this is helpful or not, but the DAOs are injected using the @EJB annotation.

                • 5. Re: Persistence context propagation.
                  gavin.king

                  If you have a SFSB with an extended PC, and it calls a SLSB in the same JTA transaction, the extended PC will be propagated. (At least according to the EJB spec.)

                  I am the original author of these rules, so I'm pretty sure I remember correct ;-)

                  • 6. Re: Persistence context propagation.
                    iradix

                    I have no doubt that you are the man Gavin :) However is a seam managed entity manager considered an extended persistence context? What I've done to my code is to change:

                    @PersistenceContext(type=EXTENDED)
                    EntityManager entityManager;

                    to:

                    @In(create=true)
                    EntityManager entityManager;

                    along with the other relevant configuration for Seam managed persistence and now the persistence context does not appear to be propagating to the SLSBs. If it was I don't see how I could get the LazyInitializationException.

                    I have little doubt that I am either doing something wrong or not understanding something correctly, but I'm a bit stumped at the moment as to where I've gone wrong.

                    • 7. Re: Persistence context propagation.
                      mangri3000

                       

                      "iradix" wrote:
                      What I've done to my code is to change:

                      @PersistenceContext(type=EXTENDED)
                      EntityManager entityManager;

                      to:

                      @In(create=true)
                      EntityManager entityManager;

                      along with the other relevant configuration for Seam managed persistence and now the persistence context does not appear to be propagating to the SLSBs.


                      I experience exactly the same problem.

                      Is there a way to get the SMPC be propagated to SLSBs?

                      • 8. Re: Persistence context propagation.
                        pmuir

                        Any component within a conversation will have the same PC injected using @In.

                        • 9. Re: Persistence context propagation.
                          mangri3000

                           

                          "pete.muir@jboss.org" wrote:
                          Any component within a conversation will have the same PC injected using @In.


                          I guess that requires that all my SLSB be seam components annotated with @Name.
                          I wouldn't like to do that with my DAO components (SLSBs).

                          Isn't it possible to attach the SMPC to the transaction (seam managed as well) and propagate it to my SLSB like an ordinary extended persistence context ?


                          • 10. Re: Persistence context propagation.
                            pmuir

                            Well you can't inject a SMPC into a non Seam component. Seam has no control over where @PersistenceContext comes from.

                            • 11. Re: Persistence context propagation.
                              christian.bauer

                              Your DAOs can have a getPersistenceContext() method in which you do Component.getInstance("entityManager").

                              • 12. Re: Persistence context propagation.
                                mangri3000

                                I try to explain a little more.

                                Our system has two interfaces, a web-ui and a messaging based interface to another system.

                                Messaging requests come this way:
                                MessageController (MDB) -> Core (SLSB and Entities) -> DAOs (SLSBs) -> Hibernate

                                UI requests go the other way:
                                Web-UI -> Application (Seam components) -> Core -> DAOs -> Hibernate

                                DAOs use entity managers injected by "@PersistenceContext". I cannot use @In because otherwise the messaging scenario wouldn't work.

                                For messaging requests I need a transaction-scoped PC which I get for free by the default EJB PC propagation.

                                For the Web-Client requests I need a conversation-scoped PC. I thougth I could simply attach a SMPC or BMPC to every transaction (EntityManager.joinTransaction) in the application-layer and do not have to change the layers below.

                                Maybe there is not such a simple solution.