Version 7

    Retrying transactions


    I got an error part way through the transaction, e.g. a deadlock, transaction timeout,

    something I called did


    or threw a system exception.


    How do I recover from this?


    Handling the exception


    You should let the exception propogate out past the transaction boundary.


    e.g. If you have a stateless session bean that starts the transaction you rethrow

    the error to it and let it rethrow to the caller.


    The transaction is dead, nothing you can do inside the transaction will let you fix it.




    JBoss has a feature that lets you check exceptions to see whether they are retryable.

    You just write some code to examine the exception, example from the testsuite:

      * JBoss, Home of Professional Open Source
      * Copyright 2005, JBoss Inc., and individual contributors as indicated
      * by the @authors tag. See the copyright.txt in the distribution for a
      * full listing of individual contributors.
      * This is free software; you can redistribute it and/or modify it
      * under the terms of the GNU Lesser General Public License as
      * published by the Free Software Foundation; either version 2.1 of
      * the License, or (at your option) any later version.
      * This software is distributed in the hope that it will be useful,
      * but WITHOUT ANY WARRANTY; without even the implied warranty of
      * Lesser General Public License for more details.
      * You should have received a copy of the GNU Lesser General Public
      * License along with this software; if not, write to the Free
      * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
      * 02110-1301 USA, or see the FSF site:
    package org.jboss.test.retry.interfaces;
    import javax.ejb.*;
    import java.rmi.*;
    public class RetryHandler implements org.jboss.ejb.plugins.TxRetryExceptionHandler
       public boolean retry(Exception ex)
          System.out.println("**** testing retry ****");
          if (ex instanceof RetryException)
             RetryException.wasRetried = true;
             return true;
          return false;


    This is configured in


    against the transaction demarcation interceptor.


          <container-configuration extends="Standard Stateless SessionBean">
                <!-- CMT -->
                <interceptor transaction="Container">org.jboss.ejb.plugins.TxInterceptorCMT




    This retry will only work when this is the EJB that started the transaction.


    i.e. It will not try to handle errors if this is just a


    ejb taking part in a transaction started by somebody else.




    You don't use this method for


    mdbs. When the transaction fails, the message is NACKed back to the JMS provider which will then redeliver the message in a new transaction.


    Be careful


    • You don't want to continuously retry a transaction that will always fail

    • You don't want to retry transactions when the error should be propogated back to the client, i.e. only it knows how to recover from the error/problem.



    Referenced by: