前天郭BOSS给我分发压力测试后一个新的任务,进行hibernate的二级缓存配置。

    我们系统采用的配置为spring2.5.6+hibernate3.3.2+JbossCache3.3.2GA,之前我对hibernate一点都不懂,开始我的零基础学习hibernate配置二级缓存。

    首先在applicationContext.xml进行配置(主要是为配置hibernate的sessionFactory,增加一些属性)。

 

  1. <context:annotation-config />  
  2.  
  3.     <bean id="sessionFactory" 
  4.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
  5.         
  6.    ..... 
  7.         <property name="hibernateProperties"> 
  8.             <props>
  9.                 <prop key="hibernate.cache.use_second_level_cache">true</prop> 
  10.                 <prop key="hibernate.cache.use_query_cache">true</prop> 
  11.                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.TreeCacheProvider</prop> 
  12.                 <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.jbc2.SharedJBossCacheRegionFactory</prop> 
  13.             </props> 
  14.         </property> 
  15.     </bean> 

在 添加第一句<context:annotation-config />时,就遇到了一些问题,需要在配置文件头添加上hibernate事务的context属性(可能还有其他的tx,aop,util属性)。这四个属性都能有用,分别为“启用二级缓存”,“使用查询缓存”,“缓存的provider类”,“缓存工厂的实现类”。

      接下来,在classpath路径下,添加treecache.xml(注意大小写)。(由于我不想再添加新的属性,所以使用treecache名字。注意使用其他名字,需要配置,sorry,i don't know)classpath我试了半天也没找到,后来在Boss的帮助下,从发布目录下寻找到classes文件夹,对应的web应用中的文件夹就为classpath路径。在其中添加即可。

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <server> 
  3.   <classpath codebase="./lib" archives="jboss-cache.jar, jgroups.jar"/> 
  4.   <mbean code="org.jboss.cache.TreeCache" 
  5.   name="jboss.cache:service=CustomTreeCache"> 
  6.   <depends>jboss:service=Naming</depends> 
  7.   <depends>jboss:service=TransactionManager</depends> 
  8.   <attribute name="TransactionManagerLookupClass"> 
  9.   org.jboss.cache.GenericTransactionManagerLookup</attribute> 
  10.   <attribute name="NodeLockingScheme">OPTIMISTIC</attribute> 
  11.   <attribute name="IsolationLevel">REPEATABLE_READ</attribute> 
  12.   <attribute name="CacheMode">REPL_SYNC</attribute> 
  13.   <attribute name="UseReplQueue">false</attribute> 
  14.   <attribute name="ReplQueueInterval">0</attribute> 
  15.   <attribute name="ReplQueueMaxElements">0</attribute> 
  16.   <attribute name="FetchInMemoryState">false</attribute> 
  17.   <attribute name="SyncReplTimeout">20000</attribute> 
  18.   <attribute name="LockAcquisitionTimeout">15000</attribute> 
  19.   <attribute name="EvictionPolicyClass"></attribute> 
  20.   <attribute name="UseRegionBasedMarshalling">false</attribute> 
  21.   </mbean> 
  22. </server> 

这里尽是一个简单的sample,没有设置群集,只是结构如此而已。具体属性需根据需求调整。

    最后,添加实体cache和查询cache(查询cache能够很好提高应用程序的性能)

实体cache很简单,只需要在实体上添加

  1. @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)  

查询cache有些复杂,需要注意

1)减少select语句中的字段,从而降低访问数据库的数据量,实现手段为利用Query的iterate()方法。
Query的iterate()方法首先检索ID字段,然后根据ID字段到hibernate的第一级缓存以及第二级缓存中查找匹配的Customer对象,如果存在,就直接把它加入到查询结果集中,否则就执行额外的select语句,根据ID字段到数据库中检索该对象。
Query query = session.createQuery("from Customer where age<30");
Iterator result = query.iterate();

2)在sql语句下面添加

  1. query.setCacheable(true); 

3)hibernate提供了3种和查询相关的缓存区域。

我们可以通过使用JBOSS 的web-console视图观察缓存命中率,miss率。

 

spring+hibernate+Jboss treeCache配置二级缓存(一)_Hibernate_03

接下来,要进行群集的配置!go on!