Version 7

    Retrying transactions

     

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

    something I called did

    setRollbackOnly()

    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.

     

    Retrying

     

    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
      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      * 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: http://www.fsf.org.
      */
    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

    META-INF/[standard-]jboss.xml

    against the transaction demarcation interceptor.

     

          <container-configuration extends="Standard Stateless SessionBean">
             <container-name>Retry</container-name>
             <container-interceptors>
                <interceptor>org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor</interceptor>
                <interceptor>org.jboss.ejb.plugins.LogInterceptor</interceptor>
                <interceptor>org.jboss.ejb.plugins.SecurityInterceptor</interceptor>
                <!-- CMT -->
                <interceptor transaction="Container">org.jboss.ejb.plugins.TxInterceptorCMT
                   <retry-handlers>
                      <handler>org.jboss.test.retry.interfaces.RetryHandler</handler>
                   </retry-handlers>
                </interceptor>
    etc.
    

     

    Usage

     

    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

    REQUIRED

    ejb taking part in a transaction started by somebody else.

     

    MDB

     

    You don't use this method for

    REQUIRED

    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: