7 Replies Latest reply on Jul 9, 2004 5:55 AM by aloubyansky

    Catching TransactionRolledbackException

    pgb

      Hello,
      I have a session bean with a method
      void updateCountry(CountryValue value)

      This method will instantiate a Country bean (with the primary key) and then call the setCountryValue method on the Country bean.

      Country is mapped as CMP, and the table has some unique constraints.
      If the data fails to be inserted on the DB (i.e. the unique constraint fails), I get a TransactionRolledbackException logged on the screen.

      My question is:
      How can I catch that exception in the updateCountry method?
      I have a catch for FinderException, NamingException and Exception, and it is not catching it. It seems to me that the exception is being thrown inside JBoss, and I cannot get it on the beans. How can I achieve that?
      My setCountryValue method is abstract, and is the one that XDoclet automatically generates for me.

      Thanks in advance!

        • 1. Re: Catching TransactionRolledbackException
          kabirkhan

          Could you check that the unique constraint is not being validated before the insert/update?

          • 2. Re: Catching TransactionRolledbackException
            pgb

            I could check that, however it will mean duplicating the unique constraints both on the DB and on the business layer (Java), and I wanted to take advantage of the DB checking of the contraints, and catching the exception.

            Is that possible?

            Thank you!

            • 3. Re: Catching TransactionRolledbackException
              aloubyansky

              You could updateCountry in RequiresNew and catch the exception.

              • 4. Re: Catching TransactionRolledbackException
                pgb

                It is still not working with the RequiresNew...
                Here's the relevant code (for another bean called Area instead of country):

                updateArea method on a session bean:

                /**
                 *
                 * @param value
                 *
                 * @ejb.interface-method
                 * view-type = "both"
                 * @ejb.transaction
                 * type = "RequiresNew"
                 *
                 */
                 public void updateArea(AreaValue value) {
                 try {
                 System.out.println("updateArea");
                 AreaLocal area = AreaUtil.getLocalHome().findByPrimaryKey(value.getPrimaryKey());
                 area.setAreaValue(value);
                 } catch (FinderException e) {
                 // TODO Auto-generated catch block
                 System.out.println("finder exception");
                 e.printStackTrace();
                 } catch (NamingException e) {
                 // TODO Auto-generated catch block
                 System.out.println("naming exception");
                 e.printStackTrace();
                 } catch (Exception e) {
                 System.out.println("exception!");
                 e.printStackTrace();
                 System.out.println("end exception");
                 }
                 }
                


                setAreaValue on AreaBean:

                 /**
                 *
                 * @param value
                 *
                 * @ejb.interface-method
                 * @ejb.transaction
                 * type = "Required"
                 */
                 public abstract void setAreaValue(AreaValue value);
                


                Whenever a unique constraint on the DB fails, I get a TransactionRolledBackException on the JBoss console and I'm not able to catch it...

                Thanks again!

                • 5. Re: Catching TransactionRolledbackException
                  aloubyansky

                  Show the code that calls updateArea method. updateArea will fail with rollback exception, not that you will be able to catch it inside updateArea.

                  • 6. Re: Catching TransactionRolledbackException
                    pgb

                    This is the calling code:

                     protected final ActionForward executeSave(ActionMapping mapping, BaseCRUDForm form, HttpServletRequest request, HttpServletResponse response) throws CRUDBusinessAccessException {
                     if (form.isNew()) {
                     return executeCreate(mapping, form, request, response);
                     } else {
                     ActionForward retValue = null;
                     try {
                     retValue = executeUpdate(mapping, form, request, response);
                     } catch (TransactionRolledbackException e) {
                     System.out.println("could not update");
                     } catch (RemoteException e) {
                     System.out.println("remote exception");
                     e.printStackTrace();
                     System.out.println("fin");
                     }
                     return retValue;
                     }
                     }
                    

                    and in a subclass (the code above is a template method)
                     protected ActionForward executeUpdate(ActionMapping mapping, BaseCRUDForm form, HttpServletRequest request, HttpServletResponse response) throws RemoteException {
                     AreaValue value = new AreaValue();
                     convertFormToValueObject((AreaForm) form, value);
                     GeneralManager gBean = (GeneralManager) getBean();
                    
                     gBean.updateArea(value);
                    
                     return navigateBack().getActionForward(true);
                     }
                    


                    I'm getting this error logged to the console:
                    10:48:07,532 ERROR [LogInterceptor] TransactionRolledbackException in method: pu
                    blic abstract void com.mark2k.siac.services.interfaces.GeneralManager.updateArea
                    (com.mark2k.siac.domain.comitentes.model.AreaValue) throws java.rmi.RemoteExcept
                    ion, causedBy:
                    org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImp
                    l [FormatId=257, GlobalId=barney//37, BranchQual=] status=STATUS_NO_TRANSACTION;
                     - nested throwable: (javax.ejb.EJBException: Store failed; CausedByException is
                    :
                     ORA-00001: unique constraint (MAT.MA_UK) violated
                    


                    I'm using remote interfaces for the session beans, and local interfaces for the entity beans.

                    Thanks again!

                    • 7. Re: Catching TransactionRolledbackException
                      aloubyansky

                      Try to catch just Exception, not TransactionRolledbackException. It might come wrapped into some JBoss exception.