The code suggested by Tangosol and hibernate forums (
http://www.hibernate.org/132.html )
does not work and must be changed:
I. the package structure from "package net.sf.hibernate.cache;"
to
"package org.hibernate.cache;" in both classes: public class
CoherenceCache and public class CoherenceCacheProvider.
II. The implementation of method
"public void destroy()" of public class CoherenceCache
must be changed as it is wrapping wrong method of
com.tangosol.net.NamedCache - NamedCache.destroy() -
the cache destroyed when the node is shut down,
and hence:
1)All cached data will be lost and all other nodes of the cluster will be affected
2)Session replication would not work.
The appropriate method to use is NamedCache.release() which releases
local resources associated
with this instance of NamedCache without interference with the
whole cache shared across the cluster.
The correct code is:
***********************************************************
***********************************************************
package org.hibernate.cache;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.io.Serializable;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
public class CoherenceCache implements Cache {
private NamedCache cache;
public CoherenceCache(NamedCache cache) {
this.cache = cache;
}
public Object get(Object key) throws CacheException {
return cache.get(key);
}
public void put(Object key, Object value) throws CacheException {
cache.put(key, value);
}
public void remove(Object key) throws CacheException {
cache.remove(key);
}
public void clear() throws CacheException {
cache.clear();
}
public void destroy() throws CacheException {
cache.release();
}
public long nextTimestamp() {
return CacheFactory.getCluster().getTimeMillis();
}
public void lock(Object key) throws CacheException {
cache.lock(key);
}
public void unlock(Object key) throws CacheException {
cache.unlock(key);
}
public int getTimeout() {
return 10000;
}
public void update(Object key, Object value) throws CacheException {
cache.put(key, value);
}
public String getRegionName() {
return cache.getCacheName();
}
public long getSizeInMemory() {
return 0;
}
public long getElementCountInMemory() {
try {
return cache.size();
}
catch (org.hibernate.cache.CacheException ce) {
throw new CacheException(ce);
}
}
public long getElementCountOnDisk() {
return 0;
}
public Map toMap() {
try {
Map result = new HashMap();
Iterator iter = cache.keySet().iterator();
while ( iter.hasNext() ) {
Object key = iter.next();
result.put( key, cache.get( (Serializable) key ) );
}
return result;
}
catch (Exception e) {
throw new CacheException(e);
}
}
}
***********************************************************
***********************************************************
package org.hibernate.cache;
import java.util.Properties;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
public class CoherenceCacheProvider implements CacheProvider {
NamedCache cache;
public CoherenceCacheProvider(){
}
public Cache buildCache(String regionName, Properties properties)
throws CacheException {
cache = CacheFactory.getCache(regionName);
return new CoherenceCache(cache);
}
public long nextTimestamp() {
return CacheFactory.getCluster().getTimeMillis();
}
public void start(Properties properties) throws CacheException {
CacheFactory.ensureCluster();
}
public void stop() {
CacheFactory.shutdown();
}
public boolean isMinimalPutsEnabledByDefault() {
return false;
}
}
Enjoy
Vlad