dain        2005/02/25 20:02:39

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

  A context may have it's id set to null by a remove method, so we ned to keep 
the original id around so it can be used to unassociate a context which has 
been destroyed due to a system exception.
  
  Revision  Changes    Path
  1.14      +37 -30    
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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- EntityInstanceInterceptor.java    25 Feb 2005 23:06:09 -0000      1.13
  +++ EntityInstanceInterceptor.java    26 Feb 2005 01:02:39 -0000      1.14
  @@ -82,9 +82,31 @@
   
       public InvocationResult invoke(final Invocation invocation) throws 
Throwable {
           EJBInvocation ejbInvocation = (EJBInvocation) invocation;
  +        TransactionContext transactionContext = 
ejbInvocation.getTransactionContext();
  +        Object id = ejbInvocation.getId();
  +
  +        // get the context
  +        EntityInstanceContext ctx = null;
  +
  +        // if we have an id then check if there is already a context 
associated with the transaction
  +        if ( id != null) {
  +            ctx = (EntityInstanceContext) 
transactionContext.getContext(containerId, id);
  +            // if we have a dead context, the cached context was discarded, 
so we need clean it up and get a new one
  +            if (ctx != null && ctx.isDead()) {
  +                transactionContext.unassociate(ctx);
  +                ctx = null;
  +            }
  +        }
  +
  +        // if we didn't find an existing context, create a new one.
  +        if (ctx == null) {
  +            ctx = (EntityInstanceContext) pool.acquire();
  +            ctx.setId(id);
  +            ctx.setPool(pool);
  +            ctx.setTransactionContext(transactionContext);
  +        }
   
  -        // initialize the context and set it into the invocation
  -        EntityInstanceContext ctx = getInstanceContext(ejbInvocation);
  +        // set the instanct into the invocation
           ejbInvocation.setEJBInstanceContext(ctx);
   
           // check reentrancy
  @@ -96,7 +118,8 @@
               }
           }
   
  -        TransactionContext transactionContext = 
ejbInvocation.getTransactionContext();
  +        // associates the context with the transaction, this may result an a 
load that throws
  +        // and NoSuchEntityException, which needs to be converted to the a 
NoSuchObject[Local]Exception
           InstanceContext oldContext = null;
           try {
               oldContext = transactionContext.beginInvocation(ctx);
  @@ -107,42 +130,26 @@
                   throw new NoSuchObjectException(e.getMessage());
               }
           }
  +
  +        // send the invocation down the chain
           try {
               InvocationResult result = next.invoke(invocation);
               return result;
           } catch(Throwable t) {
               // we must kill the instance when a system exception is thrown
               ctx.die();
  -            transactionContext.unassociate(ctx);
  +            // id may have been set during create
  +            if (id == null) {
  +                id = ctx.getId();
  +            }
  +            // if we have an id unassociate the context
  +            if (id != null) {
  +                transactionContext.unassociate(containerId, id);
  +            }
               throw t;
           } finally {
               ejbInvocation.getTransactionContext().endInvocation(oldContext);
               ejbInvocation.setEJBInstanceContext(null);
           }
  -    }
  -
  -    private EntityInstanceContext getInstanceContext(EJBInvocation 
ejbInvocation) throws Throwable {
  -        TransactionContext transactionContext = 
ejbInvocation.getTransactionContext();
  -        Object id = ejbInvocation.getId();
  -        EntityInstanceContext ctx = null;
  -
  -        // if we have an id then check if there is already a context 
associated with the transaction
  -        if ( id != null) {
  -            ctx = (EntityInstanceContext) 
transactionContext.getContext(containerId, id);
  -            // if we have a dead context, the cached context was discarded, 
so we need clean it up and get a new one
  -            if (ctx != null && ctx.isDead()) {
  -                transactionContext.unassociate(ctx);
  -                ctx = null;
  -            }
  -        }
  -
  -        // if we didn't find an existing context, create a new one.
  -        if (ctx == null) {
  -            ctx = (EntityInstanceContext) pool.acquire();
  -            ctx.setId(id);
  -            ctx.setPool(pool);
  -            ctx.setTransactionContext(transactionContext);
  -        }
  -        return ctx;
       }
   }
  
  
  

Reply via email to