dain        2005/02/15 19:09:23

  Modified:    modules/core/src/java/org/openejb/transaction
                        BeanPolicy.java ContainerPolicy.java
                        TransactionNotSupportedException.java
  Added:       modules/core/src/java/org/openejb/transaction
                        TransactionNotSupportedLocalException.java
  Log:

  Fixed problem with CMT transactions throwing RolledbackException when an 
instance calls setRollbackOnly()
  Cleaned up handling of UnspecifiedTransactionContext
  Fixed inconsistencies in ContainerPolicy
  Removed the redundant stateful BMT policy
  
  Revision  Changes    Path
  1.8       +39 -82    
openejb/modules/core/src/java/org/openejb/transaction/BeanPolicy.java
  
  Index: BeanPolicy.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/transaction/BeanPolicy.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BeanPolicy.java   15 Feb 2005 03:24:04 -0000      1.7
  +++ BeanPolicy.java   16 Feb 2005 00:09:23 -0000      1.8
  @@ -53,103 +53,60 @@
   import org.apache.geronimo.core.service.InvocationResult;
   import org.apache.geronimo.transaction.context.TransactionContext;
   import org.apache.geronimo.transaction.context.TransactionContextManager;
  +import org.apache.geronimo.transaction.context.UnspecifiedTransactionContext;
   import org.openejb.EJBInvocation;
   
   /**
    * @version $Revision$ $Date$
    */
  -public class BeanPolicy {
  +public class BeanPolicy implements TransactionPolicy {
       private static final Log log = LogFactory.getLog(BeanPolicy.class);
  +    public static final BeanPolicy INSTANCE = new BeanPolicy();
   
  -    // TODO eliminate the two policies... they are exactally the same
  -    public static final TransactionPolicy Stateless = new 
TransactionPolicy() {
  -        public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  -            if (clientContext != null) {
  -                clientContext.suspend();
  -            }
  +    public InvocationResult invoke(Interceptor interceptor, EJBInvocation 
ejbInvocation, TransactionContextManager transactionContextManager) throws 
Throwable {
  +        TransactionContext clientContext = 
transactionContextManager.getContext();
  +        if (clientContext != null) {
  +            clientContext.suspend();
  +        }
  +        try {
  +            UnspecifiedTransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
  +            ejbInvocation.setTransactionContext(beanContext);
               try {
  -                TransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
  -                ejbInvocation.setTransactionContext(beanContext);
  +                InvocationResult result = interceptor.invoke(ejbInvocation);
  +                if (beanContext != transactionContextManager.getContext()) {
  +                    throw new UncommittedTransactionException();
  +                }
  +                beanContext.commit();
  +                return result;
  +            } catch (Throwable t) {
                   try {
  -                    InvocationResult result = 
interceptor.invoke(ejbInvocation);
                       if (beanContext != 
transactionContextManager.getContext()) {
  -                        throw new UncommittedTransactionException();
  -                    }
  -                    beanContext.commit();
  -                    return result;
  -                } catch (Throwable t) {
  -                    try {
  -                        if (beanContext != 
transactionContextManager.getContext()) {
  -                            
transactionContextManager.getContext().rollback();
  -                        }
  -                    } catch (Exception e) {
  -                        log.warn("Unable to roll back", e);
  -                    }
  -                    try {
  -                        beanContext.rollback();
  -                    } catch (Exception e) {
  -                        log.warn("Unable to roll back", e);
  +                        transactionContextManager.getContext().rollback();
                       }
  -                    throw t;
  +                } catch (Exception e) {
  +                    log.warn("Unable to roll back", e);
                   }
  -            } finally {
  -                ejbInvocation.setTransactionContext(clientContext);
  -                transactionContextManager.setContext(clientContext);
  -                if (clientContext != null) {
  -                    clientContext.resume();
  +                try {
  +                    beanContext.rollback();
  +                } catch (Exception e) {
  +                    log.warn("Unable to roll back", e);
                   }
  +                throw t;
               }
  -        }
  -
  -        private Object readResolve() {
  -            return Stateless;
  -        }
  -    };
  -
  -    public static final TransactionPolicy Stateful = new TransactionPolicy() 
{
  -        public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  +        } finally {
  +            ejbInvocation.setTransactionContext(clientContext);
  +            transactionContextManager.setContext(clientContext);
               if (clientContext != null) {
  -                clientContext.suspend();
  +                clientContext.resume();
               }
  -            try {
  -                TransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
  -                ejbInvocation.setTransactionContext(beanContext);
  -                try {
  -                    InvocationResult result = 
interceptor.invoke(ejbInvocation);
  -                    if (beanContext != 
transactionContextManager.getContext()) {
  -                        throw new UncommittedTransactionException();
  -                    }
  -                    beanContext.commit();
  -                    return result;
  -                } catch (Throwable t) {
  -                    try {
  -                        if (beanContext != 
transactionContextManager.getContext()) {
  -                            
transactionContextManager.getContext().rollback();
  -                        }
  -                    } catch (Exception e) {
  -                        log.warn("Unable to roll back", e);
  -                    }
  -                    try {
  -                        beanContext.rollback();
  -                    } catch (Exception e) {
  -                        log.warn("Unable to roll back", e);
  -                    }
  -                    throw t;
  -                }
  -            } finally {
  -                ejbInvocation.setTransactionContext(clientContext);
  -                transactionContextManager.setContext(clientContext);
  -                if (clientContext != null) {
  -                    clientContext.resume();
  -                }
  -            }
  -        }
  -
  -        private Object readResolve() {
  -            return Stateful;
           }
  -    };
  +    }
   
  +    public String toString() {
  +        return "BeanManaged";
  +    }
  +
  +    private Object readResolve() {
  +        return INSTANCE;
  +    }
   }
  
  
  
  1.8       +89 -74    
openejb/modules/core/src/java/org/openejb/transaction/ContainerPolicy.java
  
  Index: ContainerPolicy.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/transaction/ContainerPolicy.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ContainerPolicy.java      15 Feb 2005 03:24:04 -0000      1.7
  +++ ContainerPolicy.java      16 Feb 2005 00:09:23 -0000      1.8
  @@ -60,6 +60,8 @@
   import org.apache.geronimo.transaction.context.InheritableTransactionContext;
   import org.apache.geronimo.transaction.context.TransactionContext;
   import org.apache.geronimo.transaction.context.TransactionContextManager;
  +import org.apache.geronimo.transaction.context.ContainerTransactionContext;
  +import org.apache.geronimo.transaction.context.UnspecifiedTransactionContext;
   import org.openejb.EJBInvocation;
   
   /**
  @@ -79,12 +81,12 @@
   
       private static final class TxNotSupported implements TransactionPolicy {
           public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  -            if (clientContext != null) {
  -                clientContext.suspend();
  +            TransactionContext callerContext = 
transactionContextManager.getContext();
  +            if (callerContext != null) {
  +                callerContext.suspend();
               }
               try {
  -                TransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
  +                UnspecifiedTransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
                   ejbInvocation.setTransactionContext(beanContext);
                   try {
                       InvocationResult result = 
interceptor.invoke(ejbInvocation);
  @@ -101,45 +103,46 @@
                       throw t;
                   }
               } finally {
  -                ejbInvocation.setTransactionContext(clientContext);
  -                transactionContextManager.setContext(clientContext);
  -                if (clientContext != null) {
  -                    clientContext.resume();
  +                ejbInvocation.setTransactionContext(null);
  +                transactionContextManager.setContext(callerContext);
  +                if (callerContext != null) {
  +                    callerContext.resume();
                   }
               }
           }
           public String toString() {
               return "NotSupported";
           }
  -
           private Object readResolve() {
               return ContainerPolicy.NotSupported;
           }
       }
  +
       private static final class TxRequired implements TransactionPolicy {
           public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  -            if (clientContext instanceof InheritableTransactionContext) {
  +            TransactionContext callerContext = 
transactionContextManager.getContext();
  +            if (callerContext instanceof InheritableTransactionContext) {
                   try {
  -                    ejbInvocation.setTransactionContext(clientContext);
  +                    ejbInvocation.setTransactionContext(callerContext);
                       return interceptor.invoke(ejbInvocation);
                   } catch (Throwable t){
  -                    ((InheritableTransactionContext) 
clientContext).setRollbackOnly();
  +                    ((InheritableTransactionContext) 
callerContext).setRollbackOnly();
                       if (ejbInvocation.getType().isLocal()) {
                           throw new 
TransactionRolledbackLocalException().initCause(t);
                       } else {
  -                        throw new 
TransactionRolledbackException().initCause(t);
  +                        // can't set an initCause on a 
TransactionRolledbackException
  +                        throw new 
TransactionRolledbackException(t.getMessage());
                       }
                   } finally {
                       ejbInvocation.setTransactionContext(null);
                   }
               }
   
  -            if (clientContext != null) {
  -                clientContext.suspend();
  +            if (callerContext != null) {
  +                callerContext.suspend();
               }
               try {
  -                TransactionContext beanContext = 
transactionContextManager.newContainerTransactionContext();
  +                ContainerTransactionContext beanContext = 
transactionContextManager.newContainerTransactionContext();
                   ejbInvocation.setTransactionContext(beanContext);
                   try {
                       InvocationResult result = 
interceptor.invoke(ejbInvocation);
  @@ -156,10 +159,10 @@
                       throw t;
                   }
               } finally {
  -                ejbInvocation.setTransactionContext(clientContext);
  -                transactionContextManager.setContext(clientContext);
  -                if (clientContext != null) {
  -                    clientContext.resume();
  +                ejbInvocation.setTransactionContext(null);
  +                transactionContextManager.setContext(callerContext);
  +                if (callerContext != null) {
  +                    callerContext.resume();
                   }
               }
           }
  @@ -171,31 +174,32 @@
               return ContainerPolicy.Required;
           }
       }
  +
       private static final class TxSupports implements TransactionPolicy {
           public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  -            if (clientContext != null) {
  +            TransactionContext callerContext = 
transactionContextManager.getContext();
  +            if (callerContext instanceof InheritableTransactionContext) {
                   try {
  -                    ejbInvocation.setTransactionContext(clientContext);
  +                    ejbInvocation.setTransactionContext(callerContext);
                       return interceptor.invoke(ejbInvocation);
                   } catch (Throwable t){
  -                    if (clientContext instanceof 
InheritableTransactionContext) {
  -                        ((InheritableTransactionContext) 
clientContext).setRollbackOnly();
  -                        if (ejbInvocation.getType().isLocal()) {
  -                            throw new 
TransactionRolledbackLocalException().initCause(t);
  -                        } else {
  -                            // can't set an initCause on a 
TransactionRolledbackException
  -                            throw new 
TransactionRolledbackException(t.getMessage());
  -                        }
  +                    ((InheritableTransactionContext) 
callerContext).setRollbackOnly();
  +                    if (ejbInvocation.getType().isLocal()) {
  +                        throw new 
TransactionRolledbackLocalException().initCause(t);
  +                    } else {
  +                        // can't set an initCause on a 
TransactionRolledbackException
  +                        throw new 
TransactionRolledbackException(t.getMessage());
                       }
  -                    throw t;
                   } finally {
                       ejbInvocation.setTransactionContext(null);
                   }
               }
   
  +            if (callerContext != null) {
  +                callerContext.suspend();
  +            }
               try {
  -                TransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
  +                UnspecifiedTransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
                   ejbInvocation.setTransactionContext(beanContext);
                   try {
                       InvocationResult result = 
interceptor.invoke(ejbInvocation);
  @@ -213,7 +217,10 @@
                   }
               } finally {
                   ejbInvocation.setTransactionContext(null);
  -                transactionContextManager.setContext(null);
  +                transactionContextManager.setContext(callerContext);
  +                if (callerContext != null) {
  +                    callerContext.resume();
  +                }
               }
           }
           public String toString() {
  @@ -224,15 +231,16 @@
               return ContainerPolicy.Supports;
           }
       }
  +
       private static final class TxRequiresNew implements TransactionPolicy {
           public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  +            TransactionContext callerContext = 
transactionContextManager.getContext();
   
  -            if (clientContext != null) {
  -                clientContext.suspend();
  +            if (callerContext != null) {
  +                callerContext.suspend();
               }
               try {
  -                TransactionContext beanContext = 
transactionContextManager.newContainerTransactionContext();
  +                ContainerTransactionContext beanContext = 
transactionContextManager.newContainerTransactionContext();
                   ejbInvocation.setTransactionContext(beanContext);
                   try {
                       InvocationResult result = 
interceptor.invoke(ejbInvocation);
  @@ -249,10 +257,10 @@
                       throw t;
                   }
               } finally {
  -                ejbInvocation.setTransactionContext(clientContext);
  -                transactionContextManager.setContext(clientContext);
  -                if (clientContext != null) {
  -                    clientContext.resume();
  +                ejbInvocation.setTransactionContext(null);
  +                transactionContextManager.setContext(callerContext);
  +                if (callerContext != null) {
  +                    callerContext.resume();
                   }
               }
           }
  @@ -264,29 +272,33 @@
               return ContainerPolicy.RequiresNew;
           }
       }
  +
       private static final class TxMandatory implements TransactionPolicy {
           public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  -            if (clientContext instanceof InheritableTransactionContext) {
  -                try {
  -                    ejbInvocation.setTransactionContext(clientContext);
  -                    return interceptor.invoke(ejbInvocation);
  -                } catch (Throwable t){
  -                    ((InheritableTransactionContext) 
clientContext).setRollbackOnly();
  -                    if (ejbInvocation.getType().isLocal()) {
  -                        throw new 
TransactionRolledbackLocalException().initCause(t);
  -                    } else {
  -                        throw new 
TransactionRolledbackException().initCause(t);
  -                    }
  -                } finally {
  -                    ejbInvocation.setTransactionContext(null);
  +            TransactionContext callerContext = 
transactionContextManager.getContext();
  +
  +            // If we don't have a transaction, throw an exception
  +            if (!(callerContext instanceof InheritableTransactionContext)) {
  +                if (ejbInvocation.getType().isLocal()) {
  +                    throw new TransactionRequiredLocalException();
  +                } else {
  +                    throw new TransactionRequiredException();
                   }
               }
   
  -            if (ejbInvocation.getType().isLocal()) {
  -                throw new TransactionRequiredLocalException();
  -            } else {
  -                throw new TransactionRequiredException();
  +            try {
  +                ejbInvocation.setTransactionContext(callerContext);
  +                return interceptor.invoke(ejbInvocation);
  +            } catch (Throwable t) {
  +                ((InheritableTransactionContext) 
callerContext).setRollbackOnly();
  +                if (ejbInvocation.getType().isLocal()) {
  +                    throw new 
TransactionRolledbackLocalException().initCause(t);
  +                } else {
  +                    // can't set an initCause on a 
TransactionRolledbackException
  +                    throw new TransactionRolledbackException(t.getMessage());
  +                }
  +            } finally {
  +                ejbInvocation.setTransactionContext(null);
               }
           }
           public String toString() {
  @@ -297,25 +309,25 @@
               return ContainerPolicy.Mandatory;
           }
       }
  +
       private static final class TxNever implements TransactionPolicy {
           public InvocationResult invoke(Interceptor interceptor, 
EJBInvocation ejbInvocation, TransactionContextManager 
transactionContextManager) throws Throwable {
  -            TransactionContext clientContext = 
transactionContextManager.getContext();
  -
  -            if (clientContext instanceof InheritableTransactionContext) {
  -                throw new TransactionNotSupportedException();
  -            }
  +            TransactionContext callerContext = 
transactionContextManager.getContext();
   
  -            if (clientContext != null) {
  -                try {
  -                    ejbInvocation.setTransactionContext(clientContext);
  -                    return interceptor.invoke(ejbInvocation);
  -                } finally {
  -                    ejbInvocation.setTransactionContext(null);
  +            // If we have a transaction, throw an exception
  +            if (callerContext instanceof InheritableTransactionContext) {
  +                if (ejbInvocation.getType().isLocal()) {
  +                    throw new TransactionNotSupportedLocalException();
  +                } else {
  +                    throw new TransactionNotSupportedException();
                   }
               }
   
  +            if (callerContext != null) {
  +                callerContext.suspend();
  +            }
               try {
  -                TransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
  +                UnspecifiedTransactionContext beanContext = 
transactionContextManager.newUnspecifiedTransactionContext();
                   ejbInvocation.setTransactionContext(beanContext);
                   try {
                       InvocationResult result = 
interceptor.invoke(ejbInvocation);
  @@ -333,7 +345,10 @@
                   }
               } finally {
                   ejbInvocation.setTransactionContext(null);
  -                transactionContextManager.setContext(null);
  +                transactionContextManager.setContext(callerContext);
  +                if (callerContext != null) {
  +                    callerContext.resume();
  +                }
               }
           }
           public String toString() {
  
  
  
  1.2       +4 -12     
openejb/modules/core/src/java/org/openejb/transaction/TransactionNotSupportedException.java
  
  Index: TransactionNotSupportedException.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/transaction/TransactionNotSupportedException.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TransactionNotSupportedException.java     1 Mar 2004 07:14:43 -0000       
1.1
  +++ TransactionNotSupportedException.java     16 Feb 2005 00:09:23 -0000      
1.2
  @@ -47,24 +47,16 @@
    */
   package org.openejb.transaction;
   
  +import java.rmi.RemoteException;
  +
   /**
  - *
  - *
    * @version $Revision$ $Date$
    */
  -public class TransactionNotSupportedException extends Exception {
  +public class TransactionNotSupportedException extends RemoteException {
       public TransactionNotSupportedException() {
       }
   
  -    public TransactionNotSupportedException(Throwable cause) {
  -        super(cause);
  -    }
  -
       public TransactionNotSupportedException(String message) {
           super(message);
  -    }
  -
  -    public TransactionNotSupportedException(String message, Throwable cause) 
{
  -        super(message, cause);
       }
   }
  
  
  
  1.1                  
openejb/modules/core/src/java/org/openejb/transaction/TransactionNotSupportedLocalException.java
  
  Index: TransactionNotSupportedLocalException.java
  ===================================================================
  /* ====================================================================
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce this list of
   *    conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The name "OpenEJB" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of The OpenEJB Group.  For written permission,
   *    please contact [EMAIL PROTECTED]
   *
   * 4. Products derived from this Software may not be called "OpenEJB"
   *    nor may "OpenEJB" appear in their names without prior written
   *    permission of The OpenEJB Group. OpenEJB is a registered
   *    trademark of The OpenEJB Group.
   *
   * 5. Due credit should be given to the OpenEJB Project
   *    (http://openejb.org/).
   *
   * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the OpenEJB Project.  For more information
   * please see <http://openejb.org/>.
   *
   * ====================================================================
   */
  package org.openejb.transaction;
  
  import javax.ejb.EJBException;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2005/02/16 00:09:23 $
   */
  public class TransactionNotSupportedLocalException extends EJBException {
      public TransactionNotSupportedLocalException() {
      }
  
      public TransactionNotSupportedLocalException(Exception ex) {
          super(ex);
      }
  
      public TransactionNotSupportedLocalException(String message) {
          super(message);
      }
  
      public TransactionNotSupportedLocalException(String message, Exception 
ex) {
          super(message, ex);
      }
  }
  
  
  

Reply via email to