[jboss-cvs] JBossAS SVN: r60705 - branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/entity.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Feb 20 01:04:29 EST 2007
Author: bstansberry at jboss.com
Date: 2007-02-20 01:04:29 -0500 (Tue, 20 Feb 2007)
New Revision: 60705
Modified:
branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java
Log:
Support both optimistic and pessimistic locking.
Modified: branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java
===================================================================
--- branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java 2007-02-20 05:18:06 UTC (rev 60704)
+++ branches/Branch_4_2/ejb3/src/main/org/jboss/ejb3/entity/TreeCacheProviderHook.java 2007-02-20 06:04:29 UTC (rev 60705)
@@ -30,33 +30,64 @@
import org.hibernate.cache.CacheProvider;
import org.jboss.cache.TreeCacheMBean;
import org.jboss.ejb3.tx.TxUtil;
+import org.jboss.logging.Logger;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.MBeanServerLocator;
/**
- * Support for a standalone JBossCache (TreeCache) instance. The JBossCache is configured
- * via a local config resource.
- *
+ * Support for integration as a 2nd level cache with an already existing
+ * JBoss Cache (TreeCache) instance. The ObjectName of the cache is
+ * provided via the <code>hibernate.treecache.mbean.object_name</code>
+ * configuration property.
+ * <p/>
+ * This class supports both optimistic and pessimistic locking, providing
+ * instances of <code>org.hibernate.cache.OptimisticCache</code> if the
+ * underlying JBoss Cache is configured for optimistic locking.
+ *
* @author Gavin King
* @author Brian Stansberry
*/
public class TreeCacheProviderHook implements CacheProvider
{
-
+ /**
+ * Name of the Hibernate configuration property used to provide
+ * the ObjectName of the JBoss Cache instance.
+ */
+ public static final String HIBERNATE_CACHE_OBJECT_NAME_PROPERTY =
+ "hibernate.treecache.mbean.object_name";
+
+ /**
+ * Default ObjectName for the JBoss Cache instance that will be used
+ * if {@link HIBERNATE_CACHE_OBJECT_NAME_PROPERTY} is not provided.
+ */
+ public static final String DEFAULT_MBEAN_OBJECT_NAME = "jboss.cache:service=EJB3EntityTreeCache";
+
+ protected Logger log = Logger.getLogger(getClass());
+
private org.jboss.cache.TreeCache cache;
-
+ private boolean optimistic;
+
/**
* Construct and configure the Cache representation of a named cache region.
*
* @param regionName the name of the cache region
* @param properties configuration settings
- * @return The Cache representation of the named cache region.
+ * @return The Cache representation of the named cache region. If the
+ * underlying JBoss Cache is configured for optimistic locking,
+ * the returned object will also implement org.hibernate.cache.OptimisticCache.
* @throws org.hibernate.cache.CacheException
* Indicates an error building the cache region.
*/
public Cache buildCache(String regionName, Properties properties) throws CacheException
{
- return new JBCCache(cache, regionName, TxUtil.getTransactionManager());
+ if (optimistic)
+ {
+ return new OptimisticJBCCache(cache, regionName);
+ }
+ else
+ {
+ return new JBCCache(cache, regionName, TxUtil.getTransactionManager());
+ }
}
public boolean isMinimalPutsEnabledByDefault()
@@ -70,9 +101,12 @@
}
/**
- * Prepare the underlying JBossCache TreeCache instance.
+ * Find the underlying JBoss Cache TreeCache instance.
*
- * @param properties All current config settings.
+ * @param properties All current config settings.
+ * If {@link #HIBERNATE_CACHE_OBJECT_NAME_PROPERTY} is provided,
+ * the value will be the expected name of the cache; otherwise
+ * {@link #DEFAULT_MBEAN_OBJECT_NAME} will be used.
* @throws org.hibernate.cache.CacheException
* Indicates a problem preparing cache for use.
*/
@@ -80,9 +114,20 @@
{
try
{
- ObjectName mbeanObjectName = new ObjectName((String) properties.get("hibernate.treecache.mbean.object_name"));
+ String cacheName = (String) properties.get(HIBERNATE_CACHE_OBJECT_NAME_PROPERTY);
+ if (cacheName == null)
+ {
+ cacheName = DEFAULT_MBEAN_OBJECT_NAME;
+ }
+ ObjectName mbeanObjectName = new ObjectName(cacheName);
TreeCacheMBean mbean = (TreeCacheMBean) MBeanProxyExt.create(TreeCacheMBean.class, mbeanObjectName, MBeanServerLocator.locateJBoss());
cache = mbean.getInstance();
+ if ("OPTIMISTIC".equals(cache.getNodeLockingScheme()))
+ {
+ optimistic = true;
+ log.debug("JBoss Cache is configured for optimistic locking; " +
+ "provided Cache implementations will also implement OptimisticCache");
+ }
}
catch (Exception e)
{
@@ -93,5 +138,10 @@
public void stop()
{
}
+
+ public boolean isOptimistic()
+ {
+ return optimistic;
+ }
}
More information about the jboss-cvs-commits
mailing list