FileDocCategorySizeDatePackage
BMTInterceptor.javaAPI DocJBoss 4.2.16320Fri Jul 13 20:53:54 BST 2007org.jboss.ejb3.tx

BMTInterceptor

public class BMTInterceptor extends Object implements org.jboss.aop.advice.Interceptor
Comment
author
Bill Burke
author
Ole Husgaard
version
$Revision: 63572 $

Fields Summary
private TransactionManager
tm
private boolean
isStateless
protected static Logger
log
Constructors Summary
public BMTInterceptor(TransactionManager tm, boolean stateless)


       
   
      this.tm = tm;
      isStateless = stateless;
   
Methods Summary
private voidcheckBadStateful(org.jboss.ejb3.Container container)

      int status = Status.STATUS_NO_TRANSACTION;

      try
      {
         status = tm.getStatus();
      }
      catch (SystemException ex)
      {
         log.error("Failed to get status", ex);
      }

      switch (status)
      {
         case Status.STATUS_COMMITTING :
         case Status.STATUS_MARKED_ROLLBACK :
         case Status.STATUS_PREPARING :
         case Status.STATUS_ROLLING_BACK :
            try
            {
               tm.rollback();
            }
            catch (Exception ex)
            {
               log.error("Failed to rollback", ex);
            }
            String msg = "BMT stateful bean '" + container.getEjbName()
                         + "' did not complete user transaction properly status=" + TxUtils.getStatusAsString(status);
            log.error(msg);
      }
   
private voidcheckStatelessDone(org.jboss.ejb3.Container container, java.lang.Exception ex)

      int status = Status.STATUS_NO_TRANSACTION;

      try
      {
         status = tm.getStatus();
      }
      catch (SystemException sex)
      {
         log.error("Failed to get status", sex);
      }

      switch (status)
      {
         case Status.STATUS_ACTIVE :
         case Status.STATUS_COMMITTING :
         case Status.STATUS_MARKED_ROLLBACK :
         case Status.STATUS_PREPARING :
         case Status.STATUS_ROLLING_BACK :
            try
            {
               tm.rollback();
            }
            catch (Exception sex)
            {
               log.error("Failed to rollback", sex);
            }
         // fall through...
         case Status.STATUS_PREPARED :
            String msg = "Application error: BMT stateless bean " + container.getEjbName()
                         + " should complete transactions before" + " returning (EJB3 13.6.1)";
            log.error(msg);

            // the instance interceptor will discard the instance
            if (ex != null)
            {
               if (ex instanceof EJBException)
                  throw (EJBException)ex;
               else
                  throw new EJBException(msg, ex);
            }
            else throw new EJBException(msg);
      }
   
public java.lang.StringgetName()

      return BMTInterceptor.class.getName();
   
public java.lang.ObjecthandleStateful(org.jboss.aop.joinpoint.Invocation invocation)

      EJBContainerInvocation ejb = (EJBContainerInvocation)invocation;
      Container container = (Container)invocation.getAdvisor();

      StatefulBeanContext ctx = (StatefulBeanContext)ejb.getBeanContext();
      Transaction tx = (Transaction)ctx.getMetaData().getMetaData("TX", "TX");
      if (tx != null)
      {
         ctx.getMetaData().addMetaData("TX", "TX", null, PayloadKey.TRANSIENT);
         tm.resume(tx);
      }
      try
      {
         return invocation.invokeNext();
      }
      finally
      {
         checkBadStateful(container);
         Transaction newTx = tm.getTransaction();
         if (newTx != null)
         {
            ctx.getMetaData().addMetaData("TX", "TX", newTx, PayloadKey.TRANSIENT);
            tm.suspend();
         }
         else
         {
            ctx.getMetaData().addMetaData("TX", "TX", null, PayloadKey.TRANSIENT);
         }
      }
   
public java.lang.ObjecthandleStateless(org.jboss.aop.joinpoint.Invocation invocation)

      Container container = (Container)invocation.getAdvisor();
      boolean exceptionThrown = false;
      try
      {
         return invocation.invokeNext();
      }
      catch (Exception ex)
      {
         exceptionThrown = true;
         checkStatelessDone(container, ex);
         throw ex;
      }
      finally
      {
         try
         {
            if (!exceptionThrown) checkStatelessDone(container, null);
         }
         finally
         {
            tm.suspend();
         }
      }
   
public java.lang.Objectinvoke(org.jboss.aop.joinpoint.Invocation invocation)

      Transaction oldTx = tm.getTransaction();
      if (oldTx != null) tm.suspend();

      try
      {
         if (isStateless) return handleStateless(invocation);
         else return handleStateful(invocation);
      }
      finally
      {
         if (oldTx != null) tm.resume(oldTx);
      }