spring整合hibernate事务管理的四种方式,以及事务的传播行为和隔离级别介绍

来源:互联网 发布:c 简单编程 编辑:程序博客网 时间:2024/05/21 14:48
开篇先写结论:
Hibernate4 想使用 ehcache 时做二级缓存时,不使用 EHCache 提供的:hibernate.cache.region.factory_class
请无视 EHcache 网站上的 document , 那是针对 Hibernate 3.X 的.
Hibernate 4.X 有自己对其他 Cache 框架的支持.

PS: 如果按照原来方式配置,可能会出现以下异常:
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cache/TimestampsRegion

Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.EntityRegion

导入<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />就报错 :说找不到

org.springframework.cache.ehcache.EhCacheManagerFactoryBean这个  

原因是少了spring-context-support.jar 这个包


<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
 </dependency>

spring4.x hibernate4.x 整合 ehcache 基于 注解 annotate

废话不说 直接贴源码链接 :  https://git.oschina.net/alexgaoyh/alexgaoyh.git

使用ehcache来提高系统的性能,现在用的非常多, 也支持分布式的缓存,在hibernate当中作为二级缓存的实现产品,可以提高查询性能。

pom.xml

[xml] view plain copy
  1. <dependency>  
  2.     <groupId>org.hibernate</groupId>  
  3.     <artifactId>hibernate-ehcache</artifactId>  
  4.     <version>4.1.6.Final</version>  
  5.     </dependency>  

在项目的src下面添加ehcache的配置文件ehcache.xml

[xml] view plain copy
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">  
  2.     <!-- 
  3.     Subdirectories can be specified below the property e.g. java.io.tmpdir/one 
  4.     -->  
  5.     <diskStore path="java.io.tmpdir"/>  
  6.   
  7.     <!--  
  8.     Mandatory Default Cache configuration. These settings will be applied to caches  
  9.     created programmtically using CacheManager.add(String cacheName)  
  10.     -->  
  11.     <defaultCache  
  12.          maxElementsInMemory="10000"  
  13.          eternal="false"  
  14.          timeToIdleSeconds="120"  
  15.          timeToLiveSeconds="120"  
  16.          overflowToDisk="true"  
  17.          maxElementsOnDisk="10000000"  
  18.          diskPersistent="false"  
  19.          diskExpiryThreadIntervalSeconds="120"  
  20.          memoryStoreEvictionPolicy="LRU"  
  21.      />  
  22.       
  23.     <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"  
  24.            maxElementsInMemory="5000"   
  25.            eternal="true"   
  26.            overflowToDisk="true" />  
  27.     <cache name="org.hibernate.cache.internal.StandardQueryCache"  
  28.            maxElementsInMemory="10000"   
  29.            eternal="false"   
  30.            timeToLiveSeconds="120"  
  31.            overflowToDisk="true" />    
  32.       
  33.     <!--  
  34.     java文件注解查找cache方法名的策略:如果不指定java文件注解中的region="ehcache.xml中的name的属性值",   
  35.     则使用name名为com.lysoft.bean.user.User的cache(即类的全路径名称), 如果不存在与类名匹配的cache名称, 则用 defaultCache  
  36.     如果User包含set集合, 则需要另行指定其cache  
  37.     例如User包含citySet集合, 则也需要  
  38.     添加配置到ehcache.xml中  
  39.     -->      
  40.     <cache name="javaClassName" maxElementsInMemory="2000" eternal="false"   
  41.            timeToIdleSeconds="120" timeToLiveSeconds="120"  
  42.            overflowToDisk="true" />    
  43.           
  44. </ehcache>  

spring 集成hibernate 的配置文件中,添加如下配置

[xml] view plain copy
  1. <!-- 开启查询缓存 -->  
  2. <prop key="hibernate.cache.use_query_cache">true</prop>  
  3. <!-- 开启二级缓存 -->  
  4. <prop key="hibernate.cache.use_second_level_cache">true</prop>  
  5. <!-- 高速缓存提供程序 -->   
  6. <!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->  
  7. <prop key="hibernate.cache.region.factory_class">  
  8.      org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory  
  9. </prop>  

Spring也使用ehcache, 所以也需要在spring配置文件中添加ehcache的配置

[xml] view plain copy
  1. <!-- cacheManager, 指定ehcache.xml的位置 -->   
  2.     <bean id="cacheManagerEhcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  3.         <property name="configLocation">  
  4.             <value>classpath:ehcache.xml</value>  
  5.         </property>  
  6.         <!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->  
  7.         <property name="shared" value="true"/>  
  8.     </bean>  

在类中定义:

[java] view plain copy
  1. @Entity    
  2. @Table(name = "t_user")    
  3. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="javaClassName")    
  4. public class User implements Serializable {    
  5.   
  6. }  

默认情况下二级缓存只会对load get 之类的方法缓存, 想list iterator 之类的方法也使用缓存 必须跟查询缓存一起使用, 重写查询方法 

[java] view plain copy
  1. .setCacheable(true)  

[java] view plain copy
  1. criteria.setCacheable(true).list();  

之后进行验证

阅读全文
0 0
原创粉丝点击