zoukankan      html  css  js  c++  java
  • Hibernate4配置memcached

    最近项目需要用到memcached做hibernate的二级缓存,网上多是使用hibernate3配置,项目使用hibernate4,因配置上有所变化,记录配置过程备忘

    测试系统环境为windows XP

    1、下载memcached的windows安装版,我的是memcached-win32-1.4.4-14.zip,解压到某个目录,如:D:mySoftwarememcached14414

    在控制台输入D:mySoftwarememcached14414memcached.exe -d install 安装memcached

    D:mySoftwarememcached14414memcached.exe -d start 启动memcached

    2、导入依赖包:

    commons-codec-1.10.jar

    hibernate3-memcached-1.5.jar(hibernate3使用此包)

    hibernate4-memcached-1.0.jar(hibernate4使用此包)

    memcached-2.5.jar

    slf4j-api-1.6.1.jar

    spy-2.4.jar

    资源下载地址:http://download.csdn.net/detail/zhanyingf15/8477973

    3、修改配置文件

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">cn.xxx.util.Dialet</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop> 
    <!-- 缓存配置 -->
    <prop key="hibernate.cache.use_query_cache">true</prop><!-- 开启缓存 -->
    <prop key="hibernate.cache.use_second_level_cache">true</prop><!-- 开启二级缓存 -->
    <!-- hibernate3 二级缓存提供类
    <prop key="hibernate.cache.provider_class">
      com.googlecode.hibernate.memcached.MemcachedCacheProvider
    </prop>
    -->
    <!-- hibernate4 二级缓存提供类-->
    <prop key="hibernate.cache.region.factory_class">
      com.googlecode.hibernate.memcached.MemcachedRegionFactory
    </prop>
    <prop key="hibernate.cache.use_structured_entries">true</prop><!-- 结构化存储 -->
    <prop key="hibernate.max_fetch_depth">3</prop>
    <prop key="hibernate.memcached.servers">localhost:11211</prop><!-- 二级缓存服务器位置和端口 -->
    
    <!-- 二级缓存前缀名称,很多文章都是ehcache,我测试改成 memcache也没问题-->
    <prop key="hibernate.cache.region_prefix">quality.cache.ehcache</prop>
    <prop key="hibernate.memcached.operationTimeout">5000</prop><!-- 操作超时时间,单位ms -->
    <prop key="hibernate.memcached.cacheTimeSeconds">20</prop><!-- 缓存失效时间,单位秒 -->
    </props>
    </property>

    4、修改实体映射文件

    <hibernate-mapping package="cn.xxx.po">
     <class name="Game" table="game">
      <cache usage="read-write"/>  <!--可根据情况设为read-only等值-->
      <id column="GameID" name="gameID" type="integer">
      <generator class="native"/>
      </id> 
      <property column="GameIntroduce" name="gameIntroduce" type="string"/>
     </class>
    </hibernate-mapping>

    5、开启查询缓存

    Query query  = getSession().createQuery(queryString);
    query.setCacheable(true);
     

    这样就可以使用memcached强大的缓存功能了

    经测试启动正常,测试数据库mysql,数据量为150万,测试方法是使用模糊查询找出一个类型为text的字段中包含“12”字符的记录

    不用memcached下使用hibernate hql和JDBC均耗时1100ms左右,使用memcached做hibernate二级缓存后初始查询耗时在1100ms左右

    后续查询最低0ms,最高32ms,大多数维持在16ms左右。

    在服务器程序运行过程中结束memcached服务再进行查询,myeclipse控制台持续打印出以下错误

    2015-03-06 14:17:03.546 WARN net.spy.memcached.MemcachedConnection:  Closing, and reopening {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=3, #iq=0, topRop=null, topWop=net.spy.memcached.protocol.ascii.GetOperationImpl@18437e5, toWrite=0, interested=0}, attempt 2.
    2015-03-06 14:17:11.562 INFO net.spy.memcached.MemcachedConnection:  Reconnecting {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=7, #iq=0, topRop=null, topWop=net.spy.memcached.protocol.ascii.GetOperationImpl@18437e5, toWrite=0, interested=0}
    2015-03-06 14:17:12.484 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@1e171e9
    2015-03-06 14:17:12.484 INFO net.spy.memcached.MemcachedConnection:  Reconnecting due to failure to connect to {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=7, #iq=0, topRop=null, topWop=net.spy.memcached.protocol.ascii.GetOperationImpl@18437e5, toWrite=0, interested=0}
    java.net.ConnectException: Connection refused: no further information
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:701)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:322)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:219)
    at net.spy.memcached.MemcachedClient.run(MemcachedClient.java:1591)

    查询过程陷入等待,耗时135047ms后查询到数据。

    在安装memcached的服务器宕机后,二级缓存将不可用,查询过程慢到一个无法忍受的地步,时间大量耗费在重连memcached服务上,暂未找到解决办法

    本文地址:http://www.cnblogs.com/wangjiajun/p/4332495.html 

    参考文章http://blog.sina.com.cn/s/blog_67b9ad8d01011xsd.html

  • 相关阅读:
    outlook express 发不出邮件问题
    当您更改为一个值该值不是有效的启动参数对于群集实例的 SQL Server 2000 或 SQL Server 2005 的 SQL Server 服务不能启动
    (转)为gridview“删除”列添加确认对话框
    关于开心网
    Windows 群集(一)
    你们公司有软件实验室吗?
    数据安全性小结
    请教:如何限制C++.net托管组件在设计时不能运行?
    test:请不要访问
    将WDL(华康)等电子文件转换为PDF后转换其它格式文件的方法
  • 原文地址:https://www.cnblogs.com/wangjiajun/p/4332495.html
Copyright © 2011-2022 走看看