Hibernate施用WebSphere分布式缓存(DistributedMap)的实现
Hibernate使用WebSphere分布式缓存(DistributedMap)的实现
约定
本文的实现环境是
hibernate 3.3.1
WebSphere 6.1.0.21
其他版本应该可以有相同或类似路径和实现方式(具体请查看其用户手册)
具体步骤
- 登陆控制台
http://serverIP:9060/ibm/console
- 控制台路径:
资源 ==> 高速缓存实例 ==> 对象高速缓存实例
- 新建对象缓存
名称: HibernateCache
JNDI名称: services/cache/hibernate
高速缓存大小: 20000(根据需要而定)
缺省优先级别:1
- 应用并保存到主配置
- 创建hibernate 的Cache实现类
package com.achievo.framework.cache; import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.hibernate.cache.Cache; import org.hibernate.cache.CacheException; import org.hibernate.cache.Timestamper; import com.ibm.websphere.cache.DistributedMap; /* * @author xhm(hyamine) */ public class AchievoCache implements Cache { private final DistributedMap distMap; private final String regionName; public AchievoCache(DistributedMap distMap,String regionName) { this.distMap = distMap; this.regionName = regionName; } public void clear() throws CacheException { distMap.clear(); } public void destroy() throws CacheException { } public Object get(Object key) throws CacheException { return distMap.get(key); } public long getElementCountInMemory() { return distMap.size(); } public long getElementCountOnDisk() { return 0; } public String getRegionName() { return regionName; } public long getSizeInMemory() { return -1; } public int getTimeout() { return Timestamper.ONE_MS * 60000; //ie. 60 seconds } public void lock(Object key) throws CacheException { } public long nextTimestamp() { return Timestamper.next(); } public void put(Object key, Object value) throws CacheException { distMap.put(key, value); } public Object read(Object key) throws CacheException { return distMap.get(key); } public void remove(Object key) throws CacheException { distMap.remove(key); } public Map toMap() { return Collections.unmodifiableMap(distMap); } public void unlock(Object key) throws CacheException { } public void update(Object key, Object value) throws CacheException { put(key, value); } public String toString() { return "DistributedMap(" + regionName + ')'; } }
- 创建hibernate的CacheProvider实现类
package com.achievo.framework.cache; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; import org.hibernate.cache.Cache; import org.hibernate.cache.CacheException; import org.hibernate.cache.CacheProvider; import org.hibernate.cache.Timestamper; import com.ibm.websphere.cache.DistributedMap; /* * @author xhm(hyamine) */ public class AchievoCacheProvider implements CacheProvider{ private static DistributedMap distMap; public Cache buildCache(String regionName, Properties properties) throws CacheException { if(distMap == null) { try { distMap = (DistributedMap)new InitialContext().lookup("java:comp/env/cache/hibernate"); } catch (NamingException e) { e.printStackTrace(); throw new CacheException(e); } } return new AchievoCache(distMap,regionName); } public boolean isMinimalPutsEnabledByDefault() { return false; } public long nextTimestamp() { return Timestamper.next(); } public void start(Properties properties) throws CacheException { } public void stop() { } }
- hibernate缓存配置
hibernate.cache.use_second_level_cache ==> true
hibernate.cache.provider_class ==> com.achievo.framework.cache.AchievoCacheProvider
- 资源引用
ibm-web-bnd.xmi
<?xml version="1.0" encoding="UTF-8"?> <webappbnd:WebAppBinding xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:webappbnd="webappbnd.xmi" xmi:id="WebAppBinding_1237946146690" virtualHostName="default_host"> <webapp href="WEB-INF/web.xml#WebApp_1237946146690"/> <resRefBindings xmi:id="ResourceRefBinding_1238122581168" jndiName="services/cache/hibernate"> <bindingResourceRef href="WEB-INF/web.xml#ResourceRef_1238122581168"/> </resRefBindings> </webappbnd:WebAppBinding>
web.xml
<resource-ref id="ResourceRef_1238122581168"> <res-ref-name>cache/hibernate</res-ref-name> <res-type>com.ibm.websphere.cache.DistributedMap</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref>
参考文献
- http://www.ibm.com/developerworks/websphere/techjournal/0609_alcott/0609_alcott.html
- http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/tdyn_distmap.html