dain        2005/02/12 13:44:00

  Modified:    modules/core/src/java/org/openejb/entity
                        EntityInstanceInterceptor.java
                        EntityInterceptorBuilder.java
  Log:

  Check for an already exisitng ejb context in the transaction context before 
createing a new one.  This eliminates an AssertionError thrown from reentrant 
calls.
  
  Revision  Changes    Path
  1.11      +49 -34    
openejb/modules/core/src/java/org/openejb/entity/EntityInstanceInterceptor.java
  
  Index: EntityInstanceInterceptor.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/entity/EntityInstanceInterceptor.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- EntityInstanceInterceptor.java    14 Oct 2004 07:07:16 -0000      1.10
  +++ EntityInstanceInterceptor.java    12 Feb 2005 18:44:00 -0000      1.11
  @@ -67,10 +67,12 @@
    */
   public final class EntityInstanceInterceptor implements Interceptor {
       private final Interceptor next;
  +    private final Object containerId;
       private final InstancePool pool;
   
  -    public EntityInstanceInterceptor(Interceptor next, InstancePool pool) {
  +    public EntityInstanceInterceptor(Interceptor next, Object containerId, 
InstancePool pool) {
           this.next = next;
  +        this.containerId = containerId;
           this.pool = pool;
       }
   
  @@ -79,28 +81,39 @@
           TransactionContext transactionContext = 
ejbInvocation.getTransactionContext();
           Object id = ejbInvocation.getId();
   
  -        EntityInstanceContext context = (EntityInstanceContext) 
pool.acquire();
  +        boolean newContext = false;
  +        EntityInstanceContext context = null;
  +        // if we have an id then check if there is already a context 
associated with the transaction
  +        if ( id != null) {
  +            context = (EntityInstanceContext) 
transactionContext.getContext(containerId, id);
  +        }
   
  -        context.setTransactionContext(transactionContext);
  -        if (id != null) {
  -            // always activate on the way in....
  -            context.setId(id);
  -            try {
  -                context.ejbActivate();
  -            } catch (Throwable t) {
  -                // problem activating instance - discard it and throw the 
problem (will cause rollback)
  -                pool.remove(context);
  -                throw t;
  -            }
  +        // if we didn't find an existing context, create a new one.
  +        if (context == null) {
  +            context = (EntityInstanceContext) pool.acquire();
  +            newContext = true;
  +
  +            context.setTransactionContext(transactionContext);
  +            if (id != null) {
  +                // always activate on the way in....
  +                context.setId(id);
  +                try {
  +                    context.ejbActivate();
  +                } catch (Throwable t) {
  +                    // problem activating instance - discard it and throw 
the problem (will cause rollback)
  +                    pool.remove(context);
  +                    throw t;
  +                }
   
  -            // associate this instance with the TransactionContext
  -            try {
  -                transactionContext.associate(context);
  -            } catch (NoSuchEntityException e) {
  -                if (ejbInvocation.getType().isLocal()) {
  -                    throw new NoSuchObjectLocalException().initCause(e);
  -                } else {
  -                    throw new NoSuchObjectException(e.getMessage());
  +                // associate this instance with the TransactionContext
  +                try {
  +                    transactionContext.associate(context);
  +                } catch (NoSuchEntityException e) {
  +                    if (ejbInvocation.getType().isLocal()) {
  +                        throw new NoSuchObjectLocalException().initCause(e);
  +                    } else {
  +                        throw new NoSuchObjectException(e.getMessage());
  +                    }
                   }
               }
           }
  @@ -121,18 +134,20 @@
               if (id == null) id = context.getId();
   
               if (id != null) {
  -                // always passivate on the way out...
  -                try {
  -                    context.flush();
  -                    context.ejbPassivate();
  -                } catch (Throwable t) {
  -                    // problem passivating instance - discard it and throw 
the problem (will cause rollback)
  -                    pool.remove(context);
  -                    // throw this exception only if we are not already 
throwing a business exception
  -                    if (!threwException) throw t;
  -                } finally {
  -                    context.setTransactionContext(null);
  -                    transactionContext.unassociate(context.getContainerId(), 
id);
  +                // passivate on the way out if this is a new context
  +                if (newContext) {
  +                    try {
  +                        context.flush();
  +                        context.ejbPassivate();
  +                    } catch (Throwable t) {
  +                        // problem passivating instance - discard it and 
throw the problem (will cause rollback)
  +                        pool.remove(context);
  +                        // throw this exception only if we are not already 
throwing a business exception
  +                        if (!threwException) throw t;
  +                    } finally {
  +                        context.setTransactionContext(null);
  +                        
transactionContext.unassociate(context.getContainerId(), id);
  +                    }
                   }
               }
           }
  
  
  
  1.11      +2 -2      
openejb/modules/core/src/java/org/openejb/entity/EntityInterceptorBuilder.java
  
  Index: EntityInterceptorBuilder.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/entity/EntityInterceptorBuilder.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- EntityInterceptorBuilder.java     7 Nov 2004 08:04:27 -0000       1.10
  +++ EntityInterceptorBuilder.java     12 Feb 2005 18:44:00 -0000      1.11
  @@ -99,7 +99,7 @@
           if (trackedConnectionAssociator != null) {
               firstInterceptor = new 
ConnectionTrackingInterceptor(firstInterceptor, trackedConnectionAssociator);
           }
  -        firstInterceptor = new EntityInstanceInterceptor(firstInterceptor, 
instancePool);
  +        firstInterceptor = new EntityInstanceInterceptor(firstInterceptor, 
containerId, instancePool);
           firstInterceptor = new 
TransactionContextInterceptor(firstInterceptor, transactionContextManager, 
transactionPolicyManager);
           firstInterceptor = new SystemExceptionInterceptor(firstInterceptor, 
ejbName);
           return new TwoChains(firstInterceptor, systemChain);
  
  
  

Reply via email to