hibernate最强大的莫过于缓存功能,配上分布式的memcache应该性能飞快,结果各种麻烦
基本配置网上都有,就不说了
hibernate3缓存的配置
<prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop>
这种方式只能应用与hibernate3
这种是hibernate4
hibernate.cache.region.factory_class=com.googlecode.hibernate.memcached.MemcachedRegionFactory
而这个class只有在hibernate-memcache 1.5的版本才有,这个版本是专门给hibernate4使用的,如果你使用了1.6版本反而不支持了,1.5版本的jar包我没找到,直接下载源码导入项目吧,https://github.com/dzone/hibernate-memcached
我没有打成jar包,直接把代码引入到项目了,然后就是各种红色提示错误
首先是com.danga.MemCached.MemCachedClient提示找不到,找这个包我也找了半天,也不知道谁写的,如果没需求其实可以直接删了,无非就是一个memcache的客户端,我一直都是用的spy
这是maven配置
<dependency>
<groupId>com.danga</groupId>
<artifactId>java-memcached</artifactId>
<version>2.6.3</version>
</dependency>
这个完事了就是spy的客户端,又是各种恶心,开始用的spymemcache2.8版本的,然后项目里各种问题,而且总是提示HashAlgorithm.NATIVE_HASH 这个属性找不到,我想可能版本太高了吧,换成了2.6的,又是各种问题,最后终于发现只有2.7的才能保证使用
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.7</version>
</dependency>
注意,我没有使用hibernate4.3.0 final,这个版本和spring3.2.5有冲突,各种解决不了,换成了hibernate4.2
我所有的maven配置
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.expression</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.test</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.orm</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.danga</groupId>
<artifactId>java-memcached</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>20040616</version>
</dependency>
最后启动测试,终于看见了这个日志信息,真不容易
12 [main] INFO com.googlecode.hibernate.memcached.MemcachedCacheProvider - Starting MemcachedClient...
2014-03-25 15:32:36.407 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/192.168.70.12:11217, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2014-03-25 15:32:36.411 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@34d4dd8a
来源:oschina
链接:https://my.oschina.net/u/943305/blog/212402