Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.core.stateful;
   18   
   19   import javax.transaction.Status;
   20   import javax.transaction.Transaction;
   21   
   22   import org.apache.openejb.ApplicationException;
   23   import org.apache.openejb.OpenEJBException;
   24   import org.apache.openejb.SystemException;
   25   import org.apache.openejb.ContainerType;
   26   import org.apache.openejb.core.transaction.TransactionContainer;
   27   import org.apache.openejb.core.transaction.TransactionContext;
   28   import org.apache.openejb.core.transaction.TransactionPolicy;
   29   
   30   public class StatefulBeanManagedTxPolicy extends TransactionPolicy {
   31       public StatefulBeanManagedTxPolicy(TransactionContainer container) {
   32           super(Type.BeanManaged, container);
   33           if (container.getContainerType() != ContainerType.STATEFUL) {
   34               throw new IllegalArgumentException("Container is not an StatefulContainer");
   35           }
   36       }
   37   
   38       public void beforeInvoke(Object instance, TransactionContext context) throws SystemException, ApplicationException {
   39           context.callContext.set(Type.class, getPolicyType());
   40           
   41           try {
   42               StatefulInstanceManager instanceManager = ((StatefulContainer)container).getInstanceManager();
   43   
   44               // suspend any transaction currently associated with this thread
   45               // if no transaction ---> suspend returns null
   46               context.clientTx = suspendTransaction(context);
   47   
   48               // Resume previous Bean transaction if there was one
   49               Transaction beanTransaction = instanceManager.getBeanTransaction(context.callContext);
   50               if (beanTransaction != null) {
   51                   context.currentTx = beanTransaction;
   52                   resumeTransaction(context, context.currentTx);
   53               }
   54           } catch (OpenEJBException e) {
   55               handleSystemException(e.getRootCause(), instance, context);
   56           }
   57       }
   58   
   59       public void afterInvoke(Object instance, TransactionContext context) throws ApplicationException, SystemException {
   60           try {
   61               // Get the transaction after the method invocation
   62               context.currentTx = context.getTransactionManager().getTransaction();
   63   
   64               // If it is not complete, suspend the transaction
   65               if (context.currentTx != null) {
   66                   int status = context.currentTx.getStatus();
   67                   if (status != Status.STATUS_COMMITTED && status != Status.STATUS_ROLLEDBACK) {
   68                       suspendTransaction(context);
   69                   } else {
   70                       // transaction is complete, so there is no need to maintain a referecne to it
   71                       context.clientTx = null;
   72                   }
   73               }
   74   
   75               // Update the user transaction reference in the bean instance data
   76               StatefulInstanceManager instanceManager = ((StatefulContainer)container).getInstanceManager();
   77               instanceManager.setBeanTransaction(context.callContext, context.currentTx);
   78           } catch (OpenEJBException e) {
   79               handleSystemException(e.getRootCause(), instance, context);
   80           } catch (javax.transaction.SystemException e) {
   81               handleSystemException(e, instance, context);
   82           } catch (Throwable e) {
   83               handleSystemException(e, instance, context);
   84           } finally {
   85               resumeTransaction(context, context.clientTx);
   86           }
   87       }
   88   
   89       public void handleApplicationException(Throwable appException, boolean rollback, TransactionContext context) throws ApplicationException, SystemException {
   90           if (rollback && context.currentTx != null) markTxRollbackOnly(context.currentTx);
   91           throw new ApplicationException(appException);
   92       }
   93   
   94       public void handleSystemException(Throwable sysException, Object instance, TransactionContext context) throws ApplicationException, SystemException {
   95           logSystemException(sysException, context);
   96   
   97           if (context.currentTx != null) markTxRollbackOnly(context.currentTx);
   98   
   99           discardBeanInstance(instance, context.callContext);
  100   
  101           throwExceptionToServer(sysException);
  102       }
  103   }
  104   

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [javadoc | source]