[jboss-cvs] JBossAS SVN: r60615 - trunk/ejb3/src/main/org/jboss/ejb3/cache/simple.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Feb 18 22:47:52 EST 2007


Author: bstansberry at jboss.com
Date: 2007-02-18 22:47:52 -0500 (Sun, 18 Feb 2007)
New Revision: 60615

Modified:
   trunk/ejb3/src/main/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java
Log:
[EJBTHREE-867] Loosen coupling of lifecycle of nested SFSBs
[EJBTHREE-849] Properly handle passivation/activation callbacks for nested SFSBs

Modified: trunk/ejb3/src/main/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java	2007-02-19 03:44:35 UTC (rev 60614)
+++ trunk/ejb3/src/main/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java	2007-02-19 03:47:52 UTC (rev 60615)
@@ -34,7 +34,6 @@
 import org.jboss.ejb3.Pool;
 import org.jboss.ejb3.cache.StatefulCache;
 import org.jboss.ejb3.stateful.StatefulBeanContext;
-import org.jboss.util.id.GUID;
 import org.jboss.logging.Logger;
 
 /**
@@ -45,7 +44,7 @@
  */
 public class SimpleStatefulCache implements StatefulCache
 {
-   private static final Logger log = Logger.getLogger(SimpleStatefulCache.class);
+   private Logger log = Logger.getLogger(SimpleStatefulCache.class);
 
    private Pool pool;
    private CacheMap cacheMap;
@@ -70,15 +69,23 @@
             StatefulBeanContext centry = (StatefulBeanContext) entry.getValue();
             synchronized (centry)
             {
-               if (centry.inUse)
+               if (centry.getCanPassivate())
                {
-                  centry.markedForPassivation = true;
+                  passivate(centry);
+                  // its ok to evict because bean will be passivated.
                }
                else
                {
-                  passivate(centry);
-               }
-               // its ok to evict because bean will be passivated.
+                  centry.markedForPassivation = true;
+                  
+                  if (!centry.isInUse())
+                  {
+                     // Can't passivate but not in use means a child bean is 
+                     // in use.
+                     // It's not ok to evict because bean will not be passivated
+                     removeIt = false;
+                  }
+               }               
             }
          }
          return removeIt;
@@ -110,6 +117,8 @@
                synchronized (cacheMap)
                {
                   if (!running) return;
+                  
+                  boolean trace = log.isTraceEnabled();
                   Iterator it = cacheMap.entrySet().iterator();
                   long now = System.currentTimeMillis();
                   while (it.hasNext())
@@ -120,18 +129,31 @@
                      {
                         synchronized (centry)
                         {                     
-                           if (centry.inUse)
+                           if (centry.getCanPassivate())
                            {
-                              centry.markedForPassivation = true;
+                              if (!centry.getCanRemoveFromCache())
+                              {
+                                 passivate(centry);
+                              }
+                              else if (trace)
+                              {
+                                 log.trace("Removing " + entry.getKey() + " from cache");
+                              }
                            }
                            else
                            {
-                              passivate(centry);
+                              centry.markedForPassivation = true;                              
                            }
-                           // its ok to evict because it will be passivated. 
+                           // its ok to evict because it will be passivated
+                           // or we determined above that we can remove it
                            it.remove();
                         }
                      }
+                     else if (trace)
+                     {
+                        log.trace("Not passivating; id=" + centry.getId() +
+                              " only inactive " + Math.max(0, now - centry.lastUsed) + " ms");
+                     }
                   }
                }
             }
@@ -154,6 +176,7 @@
       CacheConfig config = (CacheConfig) advisor.resolveAnnotation(CacheConfig.class);
       maxSize = config.maxSize();
       sessionTimeout = config.idleTimeoutSeconds();
+      log = Logger.getLogger(getClass().getName() + "." + container.getEjbName());
       log.debug("Initializing SimpleStatefulCache with maxSize: " +maxSize + " timeout: " +sessionTimeout +
               " for " +container.getObjectName().getCanonicalName() );
       timeoutTask = new SessionTimeoutTask("SFSB Passivation Thread - " + container.getObjectName().getCanonicalName());
@@ -207,11 +230,16 @@
       try
       {
          ctx = (StatefulBeanContext) pool.get();
+         
+         if (log.isTraceEnabled())
+         {
+            log.trace("Caching context " + ctx.getId() + " of type " + ctx.getClass());
+         }
          synchronized (cacheMap)
          {
             cacheMap.put(ctx.getId(), ctx);
          }
-         ctx.inUse = true;
+         ctx.setInUse(true);
          ctx.lastUsed = System.currentTimeMillis();
       }
       catch (EJBException e)
@@ -233,11 +261,15 @@
       try
       {
          ctx = (StatefulBeanContext) pool.get(initTypes, initValues);
+         if (log.isTraceEnabled())
+         {
+            log.trace("Caching context " + ctx.getId() + " of type " + ctx.getClass());
+         }
          synchronized (cacheMap)
          {
             cacheMap.put(ctx.getId(), ctx);
          }
-         ctx.inUse = true;
+         ctx.setInUse(true);
          ctx.lastUsed = System.currentTimeMillis();
       }
       catch (EJBException e)
@@ -252,9 +284,14 @@
       }
       return ctx;
    }
-
+   
    public StatefulBeanContext get(Object key) throws EJBException
    {
+      return get(key, true);
+   }
+   
+   public StatefulBeanContext get(Object key, boolean markInUse) throws EJBException
+   {
       StatefulBeanContext entry = null;
       synchronized (cacheMap)
       {
@@ -262,19 +299,38 @@
       }
       if (entry == null)
       {
-         Object bean = pm.activateSession(key);
-         if (bean == null)
+         entry = (StatefulBeanContext) pm.activateSession(key);
+         if (entry == null)
          {
-            throw new NoSuchEJBException("Could not find Stateful bean: " + key);
+            throw new NoSuchEJBException("Could not find stateful bean: " + key);
          }
-         entry = (StatefulBeanContext) bean;
+         
+         // We cache the entry even if we will throw an exception below
+         // as we may still need it for its children and XPC references
+         if (log.isTraceEnabled())
+         {
+            log.trace("Caching activated context " + entry.getId() + " of type " + entry.getClass());
+         }
+         
          synchronized (cacheMap)
          {
             cacheMap.put(key, entry);
          }
       }
-      entry.inUse = true;
-      entry.lastUsed = System.currentTimeMillis();
+      
+      // Now we know entry isn't null
+      if (markInUse)
+      { 
+         if (entry.isRemoved())
+         {
+            throw new NoSuchEJBException("Could not find stateful bean: " + key +
+                                         " (bean was marked as removed");
+         }      
+      
+         entry.setInUse(true);
+         entry.lastUsed = System.currentTimeMillis();
+      }
+      
       return entry;
    }
 
@@ -282,7 +338,7 @@
    {
       synchronized (ctx)
       {
-         ctx.inUse = false;
+         ctx.setInUse(false);
          ctx.lastUsed = System.currentTimeMillis();
          if (ctx.markedForPassivation)
          {
@@ -296,9 +352,21 @@
       StatefulBeanContext ctx = null;
       synchronized (cacheMap)
       {
-         ctx = (StatefulBeanContext) cacheMap.remove(key);
+         ctx = (StatefulBeanContext) cacheMap.get(key);
       }
-      if (ctx != null) pool.remove(ctx);
+      if (ctx != null) 
+      {
+         if (!ctx.isRemoved())
+            pool.remove(ctx);
+         
+         if (ctx.getCanRemoveFromCache())
+         {
+            synchronized (cacheMap)
+            {
+               cacheMap.remove(key);
+            }
+         }
+      }
    }
 
 




More information about the jboss-cvs-commits mailing list