FileDocCategorySizeDatePackage
EhCacheProvider.javaAPI DocHibernate 3.2.56748Tue May 30 10:40:54 BST 2006org.hibernate.cache

EhCacheProvider

public class EhCacheProvider extends Object implements CacheProvider
Cache Provider plugin for ehcache-1.2. New in this provider are ehcache support for multiple Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values. Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply by configurion in ehcache.xml.

Use hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider in the Hibernate configuration to enable this provider for Hibernate's second level cache.

When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using the property hibernate.cache.provider_configuration_file_resource_path An example to set an ehcache configuration called ehcache-2.xml would be hibernate.cache.provider_configuration_file_resource_path=/ehcache-2.xml. If the leading slash is not there one will be added. The configuration file will be looked for in the root of the classpath.

Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier is not in the classpath or it will not work.

See http://ehcache.sf.net for documentation on ehcache

author
Greg Luck
author
Emmanuel Bernard

Fields Summary
private static final Log
log
private net.sf.ehcache.CacheManager
manager
Constructors Summary
Methods Summary
public CachebuildCache(java.lang.String name, java.util.Properties properties)
Builds a Cache.

Even though this method provides properties, they are not used. Properties for EHCache are specified in the ehcache.xml file. Configuration will be read from ehcache.xml for a cache declaration where the name attribute matches the name parameter in this builder.

param
name the name of the cache. Must match a cache configured in ehcache.xml
param
properties not used
return
a newly built cache will be built and initialised
throws
CacheException inter alia, if a cache of the same name already exists


                                                                                              
            
	    try {
            net.sf.ehcache.Cache cache = manager.getCache(name);
            if (cache == null) {
                log.warn("Could not find configuration [" + name + "]; using defaults.");
                manager.addCache(name);
                cache = manager.getCache(name);
                log.debug("started EHCache region: " + name);
            }
            return new EhCache(cache);
	    }
        catch (net.sf.ehcache.CacheException e) {
            throw new CacheException(e);
        }
    
public booleanisMinimalPutsEnabledByDefault()

		return false;
	
private java.net.URLloadResource(java.lang.String configurationResourceName)

		URL url = ConfigHelper.locateConfig( configurationResourceName );
        if (log.isDebugEnabled()) {
            log.debug("Creating EhCacheProvider from a specified resource: "
                    + configurationResourceName + " Resolved to URL: " + url);
        }
        return url;
    
public longnextTimestamp()
Returns the next timestamp.

        return Timestamper.next();
    
public voidstart(java.util.Properties properties)
Callback to perform any necessary initialization of the underlying cache implementation during SessionFactory construction.

param
properties current configuration settings.

		if (manager != null) {
            log.warn("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
                    " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider." +
                    " If this behaviour is required, consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider.");
            return;
        }
        try {
            String configurationResourceName = null;
            if (properties != null) {
                configurationResourceName = (String) properties.get( Environment.CACHE_PROVIDER_CONFIG );
            }
            if ( StringHelper.isEmpty( configurationResourceName ) ) {
                manager = new CacheManager();
            } else {
                URL url = loadResource(configurationResourceName);
                manager = new CacheManager(url);
            }
        } catch (net.sf.ehcache.CacheException e) {
			//yukky! Don't you have subclasses for that!
			//TODO race conditions can happen here
			if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " +
                    "CacheManager using the diskStorePath")) {
                throw new CacheException("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
                    " between repeated calls to buildSessionFactory. Consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider."
						, e );
            } else {
                throw e;
            }
        }
	
public voidstop()
Callback to perform any necessary cleanup of the underlying cache implementation during SessionFactory.close().

		if (manager != null) {
            manager.shutdown();
            manager = null;
        }