hibernate最強大的莫過於緩存功能,配上分佈式的memcache應該性能飛快,結果各類麻煩java
基本配置網上都有,就不說了mysql
hibernate3緩存的配置
git
<prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop>github
這種方式只能應用與hibernate3spring
這種是hibernate4sql
hibernate.cache.region.factory_class=com.googlecode.hibernate.memcached.MemcachedRegionFactoryexpress
而這個class只有在hibernate-memcache 1.5的版本纔有,這個版本是專門給hibernate4使用的,若是你使用了1.6版本反而不支持了,1.5版本的jar包我沒找到,直接下載源碼導入項目吧,https://github.com/dzone/hibernate-memcached緩存
我沒有打成jar包,直接把代碼引入到項目了,而後就是各類紅色提示錯誤maven
首先是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 queue2014-03-25 15:32:36.411 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@34d4dd8a