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

    EJB3 Facade Design

    Peter Voringer Newbie

      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