13 Replies Latest reply on May 26, 2006 3:28 PM by echon

    EJB3 Facade Design

    echon

      Hi,

      i am currently working on something like a portal-system. One of the
      demands is a facade to the persistence layer, because the use cases of
      the modules (portlets) can not be foreknown. I also want to give out peristent (NOT detached) EntityBean (POJOs) i could work
      without LazyExceptions. Because of this i cannot use (unfortunatelly) Seam, because Seam uses Statefull SessionBeans for each UseCase and so it is not possible to implemt my demands with Seam.

      In caveatemptor (Hibernate) i found a nice Pattern for EJB3.0 DAOs which
      i really like. In my case i anotate the for example UserDAOImpl with @Stateful and give the bean an ExtendedEntityManager to have long Transactions and so give the controll when to start (loockup Bean) and stop (call a DAO method with @Remove) the transaction over to the web-app.

      This works fine, but creates a very unlovely problem i want to demonstrate on an example:


      // Get a UserDAO and GroupDAO Statefully SessionBeans
      UserDAO udao=DAOFactory.getUserDAO();
      GroupDAO gdao=DAOFactory.getGroupDAO();
      
      .. make changes to some changes to persistent Group Entities
      .. make changes to some changes to persistent User Entities
      
      udao.remove();
      gdao.remove();
      


      Problem here is that udao and gdao have their own EntityManager and
      their own Transation so each remove() is atomar, but both remove() are not atomar, because they do not share the EntityManager.

      A really cool way would be to bind the EntityManger (and the Transaction behind) to something like a Context as used in Seam. The conclusion of this would be something like a Manager for the EntityManager and share the EntityManager for DAOs in the same Context.

      Is this a good idea and how could this be implemented?

      Regards

      Peter


        • 1. Re: EJB3 Facade Design
          bill.burke

          I like your idea of a context, post on the seam dev forum about this. Seems there should be a transactional context that you can add/remove things from.

          In the meantime, something you might want to think about:

          @Stateful
          public class MyBean ... {

          @PersistenceContext(unitName="mypu", type=EXTENDED) myPC;

          @EJB AnotherStateful nested;

          }

          @Stateful
          public class AnotherStatefulBean implements AnotherStateful {
          @PersistenceContext(unitName="myPU", type=EXTENDED) myPC;
          }

          ----

          Since MyBean.nested is injected into MyBean these two stateful beans are married to eachother and share the same lifecycle and same persistence contexts!

          Bill

          • 2. Re: EJB3 Facade Design
            echon

            thx. for the realy quick answer.

            So if i inject a EJB they share the same lifecycle and persistence context.
            Matching this with the Context-Idea your MyBean represents a Context who knows about all other DAO StatefullBeans (your AnotherStatefulBean) like (UserDAO, GroupDAO, ... a lot more) which are automatically generated when the Context is created. I think this is a little bit inefficient.

            Interesting would be the other side. When i want to have a GroupDAO from Factory by getGroupDAO(int contexttype), the factory looksup if the right Context exists (or create it) and then get a GroupDAO Stateful SB and tell it the context. To synchronize i have to call perhaps factory.flush(int contexttype) or factory.getContext(int contexttype).flush().
            I also thought about Implementing the Context like a wrapper of ExtendedEntityManager and give it over to the dao.

            Regards

            Peter

            • 3. Re: EJB3 Facade Design
              bill.burke

              only injected nested SFSBs will share same lifecycle/pc. SLSBs only share PC if called in the same transaction.

              • 4. Re: EJB3 Facade Design
                echon

                Is it possible to giveout the EntityManager of a STSB, if i have removed Container-Isolation or would this end in a HorribleException?

                • 5. Re: EJB3 Facade Design
                  bill.burke

                  horrible exception or undefiend behavior.

                  • 6. Re: EJB3 Facade Design
                    bill.burke

                    you can also inject at @PersistenceUnit and create an entity from an EMF. This entity manager would be extended.

                    • 7. Re: EJB3 Facade Design
                      echon

                      It is really not possible to share an EntityManager between SFSB, or would this be only a problem if clustering?

                      So the conclusion would be something like a Command-Pattern, where
                      the Context SFSB is the Command "Executor" who has an EntityManager and my DAOs only create Command Implementations and transfer them to the Context SFSB?

                      Regards

                      Peter

                      • 8. Re: EJB3 Facade Design
                        echon

                        to continue this example..

                        With this idea i would have a Contexts-Object with LocalThread attributes (Context is a SFSB) which are initialized via a ServletFilter who
                        gets the Context from ServletContext, ServletSession and creates a new for the Request Context. Furthermore i have a SessionListener and ApplicationListerner who creates Context SFSB for Session and Application Context.

                        • 9. Re: EJB3 Facade Design
                          echon

                          Something additional to


                          Since MyBean.nested is injected into MyBean these two stateful beans are married to eachother and share the same lifecycle and same persistence contexts!"


                          If they were injected or also if i get them from JNDI in my Context SFSB?

                          Regards

                          Peter

                          • 10. Re: EJB3 Facade Design
                            echon

                            ok.. silly question.. they are not married to eachother if get by JNDI

                            • 11. Re: EJB3 Facade Design
                              echon

                              any further hints?

                              • 12. Re: EJB3 Facade Design
                                echon

                                ok.. i've tested a lot yesterday and found out, that injected EJBs only share Lifecycle, but not the Transaction.

                                If i have 3 SFSB where the first inject the 2 other and make changes to DB via these 2 injected the db transaction is not used for both injected.
                                If the first injected Bean synchronizes successfully with DB and the second not the entries created in the first are not rolledback.

                                So is this a bug or is this the normal behaviour?

                                Regards

                                Peter

                                • 13. Re: EJB3 Facade Design
                                  echon

                                  any comments?