0 Replies Latest reply on May 16, 2006 11:45 AM by tsar_bomba

    unclear about transaction in this case...

    tsar_bomba Newbie

      I've got a simple storefront app that has a few stateless EJBs that act as DAO objects, basically. The app also has a single stateful bean which acts as a session for the shopping cart, it's called ShoppingCartBean.

      I've got it all working just fine but I'm not happy w/ the final checkout action where all order (entities) data is persisted and the SFSB is terminated w/ @Remove.

      I want to make it transactional but I'm unclear how I should do this, really. I'm using CMT entirely, no BMT in any of this.

      Each of the stateless beans declares an EntityManager and the methods there in use it to query/persist data...really straight-forward stuff.

      Now, if I take my SFSB and declare an EntityManager in it...could I make the methods I call from the injected stateless beans transactional?

      Here's an example of what I'm imagining:

      @Stateful
      //@Local(ShoppingCart.class)
      public class ShoppingCartBean
       implements Serializable, ShoppingCartLocal, ShoppingCartRemote
      {
       @PersistenceContext
       private EntityManager em;
       private Order order;
       private Customer customer;
       @EJB Authenticate authBean;
       @EJB Orders orderBean;
      
       public void completeOrder()
       {
       //begin transaction
       this.em.getTransaction().begin();
      
       //get Order in session
       Order order = this.getOrder();
      
       //Customer/User in session
       Customer customer = order.getCustomer();
       User user = customer.getUser();
      
       //Address entities
       Address billing = this.getBillingAddress();
       Address shipping = this.getShippingAddress();
      
       //persist User
       this.authBean.saveUser(user);
      
       //persist Customer
       this.orderBean.saveCustomer(customer);
      
       //commit transaction
       this.em.getTransaction().commit();
       }
      }
      


      Is this the proper way to go about this? What are the ramifications for injecting the other EJBs and the EntityManager in a stateful bean?

      All advice is greatly appreciated, thank you!