public Object invokeHome(Invocation mi) throws Exception {
    EntityEnterpriseContext ctx = (EntityEnterpriseContext) mi.getEnterpriseContext();
    Transaction tx = mi.getTransaction();

    Object rtn = getNext().invokeHome(mi);

    // An anonymous context was sent in, so if it has an id it is a real instance now
    if (ctx.getId() != null) {

      // it doesn't need to be read, but it might have been changed from the db already.
      ctx.setValid(true);

      if (tx != null) {
        BeanLock lock = container.getLockManager().getLock(ctx.getCacheKey());
        try {
          lock.schedule(mi);
          register(ctx, tx); // Set tx
          lock.endInvocation(mi);
        } finally {
          container.getLockManager().removeLockRef(lock.getId());
        }
      }
    }
    return rtn;
  }
    public void afterCompletion(int status) {
      boolean trace = log.isTraceEnabled();

      // This is an independent point of entry. We need to make sure the
      // thread is associated with the right context class loader
      ClassLoader oldCl = SecurityActions.getContextClassLoader();
      boolean setCl = !oldCl.equals(container.getClassLoader());
      if (setCl) {
        SecurityActions.setContextClassLoader(container.getClassLoader());
      }
      container.pushENC();

      int commitOption =
          ctx.isPassivateAfterCommit()
              ? ConfigurationMetaData.C_COMMIT_OPTION
              : EntitySynchronizationInterceptor.this.commitOption;

      lock.sync();
      // The context is no longer synchronized on the TX
      ctx.hasTxSynchronization(false);
      ctx.setTxAssociation(GlobalTxEntityMap.NONE);
      ctx.setTransaction(null);
      try {
        try {
          // If rolled back -> invalidate instance
          if (status == Status.STATUS_ROLLEDBACK) {
            // remove from the cache
            container.getInstanceCache().remove(ctx.getCacheKey());
          } else {
            switch (commitOption) {
                // Keep instance cached after tx commit
              case ConfigurationMetaData.A_COMMIT_OPTION:
              case ConfigurationMetaData.D_COMMIT_OPTION:
                // The state is still valid (only point of access is us)
                ctx.setValid(true);
                break;

                // Keep instance active, but invalidate state
              case ConfigurationMetaData.B_COMMIT_OPTION:
                // Invalidate state (there might be other points of entry)
                ctx.setValid(false);
                break;
                // Invalidate everything AND Passivate instance
              case ConfigurationMetaData.C_COMMIT_OPTION:
                try {
                  // We weren't removed, passivate
                  // Here we own the lock, so we don't try to passivate
                  // we just passivate
                  if (ctx.getId() != null) {
                    container.getInstanceCache().remove(ctx.getId());
                    container.getPersistenceManager().passivateEntity(ctx);
                  }
                  // If we get this far, we return to the pool
                  container.getInstancePool().free(ctx);
                } catch (Exception e) {
                  log.debug("Exception releasing context", e);
                }
                break;
            }
          }
        } finally {
          if (trace) log.trace("afterCompletion, clear tx for ctx=" + ctx + ", tx=" + tx);
          lock.endTransaction(tx);

          if (trace) log.trace("afterCompletion, sent notify on TxLock for ctx=" + ctx);
        }
      } // synchronized(lock)
      finally {
        lock.releaseSync();
        container.getLockManager().removeLockRef(lock.getId());
        container.popENC();
        if (setCl) {
          SecurityActions.setContextClassLoader(oldCl);
        }
      }
    }
 /** Create a new instance synchronization instance. */
 InstanceSynchronization(Transaction tx, EntityEnterpriseContext ctx) {
   this.tx = tx;
   this.ctx = ctx;
   this.lock = container.getLockManager().getLock(ctx.getCacheKey());
 }