3 Replies Latest reply: Apr 12, 2012 3:41 PM by 927751 RSS

    Transaction inside a stateful session bean

    927751
      I've a stateful session bean that represents a shopping cart.
      It has the following checkout method():

      <pre>
      /* em.getTransaction().begin(); */

      reasons.clear();
      for(CartEntry ce : entries){

      // get the book reference for the cart entry
      Book relbook = books.findById(ce.getBook().getId());

      if( ce.getQuantity()> relbook.getQuantity() ){
      reasons.add("Item not availabel in this quantity");

      // TODO: need to rollback

      /*em.getTransaction().rollback();*/

      throw new Exception();
      } else{
      // update book quantities
      relbook.setQuantity( relbook.getQuantity() - ce.getQuantity() );

      }
      }

      /*em.getTransaction().commit();*/

      // end of conversational state
      cancel();
      </pre>
      --------------------------------------------------------------------------------

      This method checks for the quantity of the items with respect to the quantity in the cart entries.

      How can I perform a transaction here?
      I'd like to rollback it before throwing the exception. And I'd like to commit it at the end,.

      If I use the entity manager to get the transaction, I get this error:
      Exception Description: Cannot use an EntityTransaction while using JTA.
        • 1. Re: Transaction inside a stateful session bean
          927751
          I've managed to by using UserTransaction and by demarcating explicitly the commit/rollback.

          Now my issue is: how can I be sure that, from the time by which I decrement the item quantity in the database to the commit, no other transaction is decrementing the quantity of the same item as well??? Am I protected against this???

          Thank you,
          Roberto
          • 2. Re: Transaction inside a stateful session bean
            gimbal2
            Its sad to see someone using EJB technology and then completely throw all benefits of it out of the window. I highly advise you to look into Container Managed Transactions; learn what they help you to do and then apply it. When you know how, it will put a smile on your face as you'll see the transaction management stuff disappear before your eyes.
            , from the time by which I decrement the item quantity in the database to the commit, no other transaction is decrementing the quantity of the same item as well??? Am I protected against this???
            Concurrency is a hard problem with no clearly defined answers to it other than "you need to design the code to guard against concurrency problems". In this specific case, the database protects against this happening by applying a row lock or a table lock while the transaction is active and you are making modifications. For more information about that, you should consult the documentation relating to your specific DBMS.
            • 3. Re: Transaction inside a stateful session bean
              927751
              My issue with the container-managed transaction is the following:

              In the checkout() method, I decrease the quantity of the items that are ordered.
              But, when I find an order item for which the quantity is greater than the availability, I'd like to throw an exception and having the transaction rolled-back.

              Now, if I throw a checked exception, the transaction does not roll back.
              If I throw a nonchecked exception, the transaction is rolledback but the EJB is no longer available.
              How can I resolve this situation?


              Thank you

              Edited by: robyonrails on Apr 12, 2012 1:40 PM

              I used EJBContext.setRollbackOnly() and it worked!