[jboss-cvs] JBossAS SVN: r59564 - trunk/ejb3/src/main/org/jboss/ejb3/entity.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 11 21:37:15 EST 2007


Author: bstansberry at jboss.com
Date: 2007-01-11 21:37:13 -0500 (Thu, 11 Jan 2007)
New Revision: 59564

Modified:
   trunk/ejb3/src/main/org/jboss/ejb3/entity/JBCCache.java
   trunk/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java
Log:
[EJBTHREE-798] Port to trunk
Move to JBoss Cache Alpha2

Modified: trunk/ejb3/src/main/org/jboss/ejb3/entity/JBCCache.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/JBCCache.java	2007-01-12 02:36:17 UTC (rev 59563)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/JBCCache.java	2007-01-12 02:37:13 UTC (rev 59564)
@@ -13,10 +13,12 @@
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.cache.Cache;
 import org.hibernate.cache.CacheException;
-import org.hibernate.cache.OptimisticCache;
+import org.hibernate.cache.StandardQueryCache;
+import org.hibernate.cache.UpdateTimestampsCache;
 import org.jboss.cache.Fqn;
 import org.jboss.cache.InvocationContext;
 import org.jboss.cache.Node;
+import org.jboss.cache.Region;
 import org.jboss.cache.config.Option;
 import org.jboss.cache.lock.TimeoutException;
 
@@ -37,6 +39,7 @@
     private final String regionName;
     private final Fqn regionFqn;
     private final TransactionManager transactionManager;
+    private boolean localWritesOnly;
 
     public JBCCache(org.jboss.cache.Cache cache, String regionName, TransactionManager transactionManager) 
     throws CacheException {
@@ -44,6 +47,28 @@
         this.regionName = regionName;
         this.regionFqn = Fqn.fromString( regionName.replace( '.', '/' ) );
         this.transactionManager = transactionManager;
+        if (cache.getConfiguration().isUseRegionBasedMarshalling())
+        {           
+           localWritesOnly = StandardQueryCache.class.getName().equals(regionName);
+           
+           boolean fetchState = cache.getConfiguration().isFetchInMemoryState();
+           try
+           {
+              // We don't want a state transfer for the StandardQueryCache,
+              // as it can include classes from multiple scoped classloaders
+              if (localWritesOnly)
+                 cache.getConfiguration().setFetchInMemoryState(false);
+              
+              // We always activate
+              activateCacheRegion(regionFqn.toString());
+           }
+           finally
+           {
+              // Restore the normal state transfer setting
+              if (localWritesOnly)
+                 cache.getConfiguration().setFetchInMemoryState(fetchState);              
+           }
+        }
     }
 
     public Object get(Object key) throws CacheException {
@@ -67,7 +92,15 @@
 
     public void update(Object key, Object value) throws CacheException {
         try {
-            cache.put( new Fqn( regionFqn, key ), ITEM, value );
+        	if (localWritesOnly) {
+               Option option = new Option();
+               option.setCacheModeLocal(true);
+               cache.getInvocationContext().setOptionOverrides(option);
+               cache.put( new Fqn( regionFqn, key ), ITEM, value );
+            }
+            else {                
+            	cache.put( new Fqn( regionFqn, key ), ITEM, value );
+            }
         }
         catch (Exception e) {
             throw new CacheException(e);
@@ -77,8 +110,16 @@
     public void put(Object key, Object value) throws CacheException {
         Transaction tx = suspend();
         try {
-            //do the failfast put outside the scope of the JTA txn
-            cache.putForExternalRead(new Fqn( regionFqn, key ), ITEM, value);
+        	if (localWritesOnly) {
+               Option option = new Option();
+               option.setCacheModeLocal(true);
+               cache.getInvocationContext().setOptionOverrides(option);
+               //do the failfast put outside the scope of the JTA txn               
+               cache.putForExternalRead(new Fqn( regionFqn, key ), ITEM, value);
+            }
+            else {
+               cache.putForExternalRead(new Fqn( regionFqn, key ), ITEM, value);
+            }
         }
         catch (TimeoutException te) {
             //ignore!
@@ -116,7 +157,15 @@
 
     public void remove(Object key) throws CacheException {
         try {
-            cache.removeNode( new Fqn( regionFqn, key ) );
+           if (localWritesOnly) {
+              Option option = new Option();
+              option.setCacheModeLocal(true);
+              cache.getInvocationContext().setOptionOverrides(option);
+              cache.removeNode( new Fqn( regionFqn, key ) );
+           }
+           else {           
+              cache.removeNode( new Fqn( regionFqn, key ) );
+           }
         }
         catch (Exception e) {
             throw new CacheException(e);
@@ -146,6 +195,11 @@
            opt.setCacheModeLocal(true);
            ctx.setOptionOverrides(opt);
            cache.removeNode( regionFqn );
+           
+           if (cache.getConfiguration().isUseRegionBasedMarshalling() && !isSharedClassLoaderRegion(regionName))
+           {
+              inactivateCacheRegion();
+           }
         }
         catch( Exception e ) {
             throw new CacheException( e );
@@ -214,7 +268,7 @@
     private Set getChildrenNames()
     {
        try {
-          Node base = cache.getChild( regionFqn );
+          Node base = cache.getRoot().getChild( regionFqn );
           return base == null ? null : base.getChildrenNames();
        }
        catch (Exception e) {
@@ -225,4 +279,49 @@
     public String toString() {
         return "JBCCache(" + regionName + ')';
     }
+       
+    private boolean isSharedClassLoaderRegion(String regionName)
+    {
+       return (StandardQueryCache.class.getName().equals(regionName) 
+                || UpdateTimestampsCache.class.getName().equals(regionName));
+    }
+    
+    private void activateCacheRegion(String regionName) throws CacheException
+    {
+       Region region = cache.getRegion(regionFqn, true);
+       if (region.isActive() == false)
+       {
+          try
+          {
+             // Only register the classloader if it's not a shared region.  
+             // If it's shared, no single classloader is valid
+             if (!isSharedClassLoaderRegion(regionName))
+             {
+                region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
+             }
+             region.activate();
+          }
+          catch (Exception e)
+          {
+             throw new CacheException("Problem activating region " + regionFqn, e);
+          }
+       }
+    }
+    
+    private void inactivateCacheRegion() throws CacheException
+    {
+       Region region = cache.getRegion(regionFqn, false);
+       if (region != null && region.isActive())
+       {
+          try
+          {
+             region.deactivate();
+             region.unregisterContextClassLoader();
+          }
+          catch (Exception e)
+          {
+             throw new CacheException("Problem inactivating region " + regionFqn, e);
+          }
+       }        
+    }	
 }

Modified: trunk/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java	2007-01-12 02:36:17 UTC (rev 59563)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java	2007-01-12 02:37:13 UTC (rev 59564)
@@ -32,6 +32,7 @@
  * via a local config resource.
  *
  * @author Gavin King
+ * @author Brian Stansberry
  */
 public class TreeCacheProviderHook implements CacheProvider
 {




More information about the jboss-cvs-commits mailing list