[jboss-cvs] JBossAS SVN: r60644 - branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/cache/simple.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Feb 19 03:51:44 EST 2007


Author: bstansberry at jboss.com
Date: 2007-02-19 03:51:44 -0500 (Mon, 19 Feb 2007)
New Revision: 60644

Modified:
   branches/Branch_4_2/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: branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java
===================================================================
--- branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java	2007-02-19 08:51:12 UTC (rev 60643)
+++ branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/cache/simple/SimpleStatefulCache.java	2007-02-19 08:51:44 UTC (rev 60644)
@@ -45,7 +45,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;
@@ -72,15 +72,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;
@@ -112,6 +120,8 @@
                synchronized (cacheMap)
                {
                   if (!running) return;
+                  
+                  boolean trace = log.isTraceEnabled();
                   Iterator it = cacheMap.entrySet().iterator();
                   long now = System.currentTimeMillis();
                   while (it.hasNext())
@@ -122,18 +132,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");
+                     }
                   }
                }
             }
@@ -156,6 +179,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());
@@ -210,11 +234,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();
          ++createCount;
       }
@@ -237,11 +266,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();
          ++createCount;
       }
@@ -260,6 +293,11 @@
 
    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)
       {
@@ -267,20 +305,39 @@
       }
       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);
          }
          --passivatedCount;
-         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;
    }
 
@@ -288,7 +345,7 @@
    {
       synchronized (ctx)
       {
-         ctx.inUse = false;
+         ctx.setInUse(false);
          ctx.lastUsed = System.currentTimeMillis();
          if (ctx.markedForPassivation)
          {
@@ -302,9 +359,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);
+            }
+         }
+      }
    }
 
    public int getCacheSize()




More information about the jboss-cvs-commits mailing list