User: fleury  
  Date: 00/06/02 02:18:47

  Modified:    src/main/org/jboss/ejb/plugins
                        NoPassivationStatefulSessionInstanceCache.java
  Log:
  we try a new synchronization (lack thereof) mechanism for the stateful session.
  I believe that the Exception question is a pressing one... makes the design easy 
though:)
  
  Revision  Changes    Path
  1.2       +170 -164  
jboss/src/main/org/jboss/ejb/plugins/NoPassivationStatefulSessionInstanceCache.java
  
  Index: NoPassivationStatefulSessionInstanceCache.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/ejb/plugins/NoPassivationStatefulSessionInstanceCache.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NoPassivationStatefulSessionInstanceCache.java    2000/05/19 08:26:41     1.1
  +++ NoPassivationStatefulSessionInstanceCache.java    2000/06/02 09:18:46     1.2
  @@ -1,9 +1,9 @@
   /*
  - * jBoss, the OpenSource EJB server
  - *
  - * Distributable under GPL license.
  - * See terms of license at gnu.org.
  - */
  +* jBoss, the OpenSource EJB server
  +*
  +* Distributable under GPL license.
  +* See terms of license at gnu.org.
  +*/
   package org.jboss.ejb.plugins;
   
   import java.rmi.RemoteException;
  @@ -26,165 +26,171 @@
   import org.jboss.ejb.deployment.jBossSession;
   
   /**
  - *   <description> 
  - *      
  - *   @see <related>
  - *   @author Rickard �berg ([EMAIL PROTECTED])
  - *   @version $Revision: 1.1 $
  - */
  +*    <description> 
  +*      
  +*    @see <related>
  +*    @author Rickard �berg ([EMAIL PROTECTED])
  +*   @author <a href="[EMAIL PROTECTED]">Marc Fleury</a>
  +*    @version $Revision: 1.2 $
  +*/
   public class NoPassivationStatefulSessionInstanceCache
  -   implements InstanceCache
  +implements InstanceCache
   {
  -   // Constants -----------------------------------------------------
  -    
  -   // Attributes ----------------------------------------------------
  -   Container con;
  -   
  -   Map active = Collections.synchronizedMap(new HashMap());
  -
  -   // Static --------------------------------------------------------
  -   
  -   // Constructors --------------------------------------------------
  -   
  -   // Public --------------------------------------------------------
  -
  -   /**
  -    *   Set the callback to the container. This is for initialization.
  -    *   The IM may extract the configuration from the container.
  -    *
  -    * @param   c  
  -    */
  -   public void setContainer(Container c)
  -   {
  -      this.con = c;
  -   }
  -   
  -   public void init()
  -      throws Exception
  -   {
  -   }
  -   
  -   public void start()
  -      throws Exception
  -   {
  -   }
  -   
  -   public void stop()
  -   {
  -   }
  -
  -   public void destroy()
  -   {
  -   }
  -   
  -   public synchronized EnterpriseContext get(Object id)
  -      throws RemoteException
  -   {
  -      // TODO: minimize synchronization of IM
  -      
  -      StatefulSessionEnterpriseContext ctx;
  -      InstanceInfo info = null;
  -      while ((ctx = (StatefulSessionEnterpriseContext)active.get(id)) != null)
  -      {
  -         synchronized(ctx)
  -         {
  -            info = (InstanceInfo)ctx.getCacheContext();
  -            if (info.isLocked())
  -               throw new RemoteException("Concurrent call to stateful session is 
not allowed");
  -         }
  -      }
  -      
  -      if (ctx == null) // Not in cache
  -      {
  -         // Get new instance from pool
  -         ctx = (StatefulSessionEnterpriseContext)con.getInstancePool().get();
  -         
  -         // Activate
  -         ctx.setId(id);
  -         
((StatefulSessionContainer)con).getPersistenceManager().activateSession(ctx);
  -         insert(ctx);
  -      } else
  -      {
  -         // Lock the instance
  -         info.lock();
  -      }
  -      
  -      // At this point we own the instance with the given identity
  -//      System.out.println("Got entity:"+ctx.getId());
  -      return ctx;
  -   }
  -
  -   public synchronized void insert(EnterpriseContext ctx)
  -   {
  -      InstanceInfo info = createInstanceInfo((StatefulSessionEnterpriseContext)ctx);
  -      ((StatefulSessionEnterpriseContext)ctx).setCacheContext(info);
  -      info.lock();
  -      active.put(ctx.getId(), ctx);
  -   }
  -   
  -   public void release(EnterpriseContext ctx)
  -   {
  -      // This context is now available for other threads
  -      synchronized(ctx)
  -      {
  -         
((InstanceInfo)((StatefulSessionEnterpriseContext)ctx).getCacheContext()).unlock();
  -//         System.out.println("Release entity:"+ctx.getId());
  -         if 
(!((InstanceInfo)((StatefulSessionEnterpriseContext)ctx).getCacheContext()).isLocked())
  -            ctx.notify();
  -      }
  -   }
  -   
  -   public synchronized void remove(Object id)
  -   {
  -      Object ctx = active.remove(id);
  -      synchronized(ctx)
  -      {
  -         ctx.notifyAll();
  -      }
  -   }
  -   
  -   // Z implementation ----------------------------------------------
  -    
  -   // Package protected ---------------------------------------------
  -    
  -   // Protected -----------------------------------------------------
  -   protected InstanceInfo createInstanceInfo(StatefulSessionEnterpriseContext ctx)
  -   {
  -      return new InstanceInfo(ctx);
  -   }
  -   
  -   // Private -------------------------------------------------------
  -
  -   // Inner classes -------------------------------------------------
  -   class InstanceInfo
  -   {
  -      int locked = 0; // 0 == unlocked, >0 == locked
  -      
  -      StatefulSessionEnterpriseContext ctx;
  -      
  -      InstanceInfo(StatefulSessionEnterpriseContext ctx)
  -      {
  -         this.ctx = ctx;
  -      }
  -      
  -      public void lock()
  -      {
  -         locked++;
  -      }
  -      
  -      public void unlock()
  -      {
  -         locked--;
  -      }
  -      
  -      public boolean isLocked()
  -      {
  -         return locked > 0;
  -      }
  -      
  -      public StatefulSessionEnterpriseContext getContext()
  -      {
  -         return ctx;
  -      }
  -   }
  +     // Constants -----------------------------------------------------
  +     
  +     // Attributes ----------------------------------------------------
  +     Container con;
  +     
  +     Map active = Collections.synchronizedMap(new HashMap());
  +     
  +     // Static --------------------------------------------------------
  +     
  +     // Constructors --------------------------------------------------
  +     
  +     // Public --------------------------------------------------------
  +     
  +     /**
  +     *   Set the callback to the container. This is for initialization.
  +     *   The IM may extract the configuration from the container.
  +     *
  +     * @param   c  
  +     */
  +     public void setContainer(Container c)
  +     {
  +             this.con = c;
  +     }
  +     
  +     public void init()
  +     throws Exception
  +     {
  +     }
  +     
  +     public void start()
  +     throws Exception
  +     {
  +     }
  +     
  +     public void stop()
  +     {
  +     }
  +     
  +     public void destroy()
  +     {
  +     }
  +     
  +     public synchronized EnterpriseContext get(Object id)
  +     throws RemoteException
  +     {
  +             InstanceInfo info = null;
  +             
  +             // Do we have the context in cache?
  +             StatefulSessionEnterpriseContext ctx = 
  +                     (StatefulSessionEnterpriseContext)active.get(id)
  +             
  +             // We have it in cache
  +             if (ctx != null) {
  +                     
  +                     info = (InstanceInfo)ctx.getCacheContext();
  +                     
  +                     if (info.isLocked()) {
  +                     
  +                             //MF DESIGN: talk about this one... I know it is spec 
compliant but it sucks
  +                             throw new RemoteException("Concurrent call to stateful 
session is not allowed");
  +                     }
  +                     
  +                     else {
  +                             
  +                             info.lock();
  +                     }
  +             }
  +                     
  +         // We don't have it in cache
  +             if (ctx == null) {
  +                     
  +                     // Get new instance from pool (bogus in our case)
  +                     ctx = 
(StatefulSessionEnterpriseContext)con.getInstancePool().get();
  +                     
  +                     // Activate
  +                     ctx.setId(id);
  +                     
  +                     
((StatefulSessionContainer)con).getPersistenceManager().activateSession(ctx);
  +                     
  +                     insert(ctx);
  +             }
  +             // The context has the instance as well and the right id
  +             return ctx;
  +     }
  +     
  +     public synchronized void insert(EnterpriseContext ctx)
  +     {
  +             InstanceInfo info = 
createInstanceInfo((StatefulSessionEnterpriseContext)ctx);
  +             ((StatefulSessionEnterpriseContext)ctx).setCacheContext(info);
  +             info.lock();
  +             active.put(ctx.getId(), ctx) ;
  +     }
  +     
  +     public void release(EnterpriseContext ctx)
  +     {
  +             // This context is now available for other threads
  +             
((InstanceInfo)((StatefulSessionEnterpriseContext)ctx).getCacheContext()).unlock();
  +             
  +             // The following code makes sense if we put threads to sleep (not the 
case anymore)
  +             //if 
(!((InstanceInfo)((StatefulSessionEnterpriseContext)ctx).getCacheContext()).isLocked())
  +             //              ctx.notify();
  +             
  +     }
  +     
  +     public synchronized void remove(Object id)
  +     {
  +             Object ctx = active.remove(id);
  +             synchronized(ctx)
  +             {
  +                     ctx.notifyAll();
  +             }
  +     }
  +     
  +     // Z implementation ----------------------------------------------
  +     
  +     // Package protected ---------------------------------------------
  +     
  +     // Protected -----------------------------------------------------
  +     protected InstanceInfo createInstanceInfo(StatefulSessionEnterpriseContext ctx)
  +     {
  +             return new InstanceInfo(ctx);
  +     }
  +     
  +     // Private -------------------------------------------------------
  +     
  +     // Inner classes -------------------------------------------------
  +     class InstanceInfo
  +     {
  +             int locked = 0; // 0 == unlocked, >0 == locked
  +             
  +             StatefulSessionEnterpriseContext ctx;
  +             
  +             InstanceInfo(StatefulSessionEnterpriseContext ctx)
  +             {
  +                     this.ctx = ctx;
  +             }
  +             
  +             public void lock()
  +             {
  +                     locked++;
  +             }
  +             
  +             public void unlock()
  +             {
  +                     locked--;
  +             }
  +             
  +             public boolean isLocked()
  +             {
  +                     return locked > 0;
  +             }
  +             
  +             public StatefulSessionEnterpriseContext getContext()
  +             {
  +                     return ctx;
  +             }
  +     }
   }
  
  
  

Reply via email to