hibernate4 二級快取demo例項
阿新 • • 發佈:2019-01-29
配置一目瞭然
hibernate使用版本是:hibernate-release-4.2.5.Final
需要的jar包:hibernate-release-4.2.5.Final\lib\required下所有jar包
ehcache jar包:hibernate-release-4.2.5.Final\lib\optional\ehcache下所有包
junit:junit-4.10.jar和mysql-connector-java-5.1.15-bin.jar
注:hibernate 4.2.5版本ehcache快取不依賴commons-logging-1.1.1.jar,需要的是slf4j-api-1.6.1.jar
專案結構如下
hibernate.cfg.xml
Xml程式碼- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
-
<
- <!-- Database connection settings -->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate4</property>
-
<property name="connection.username"
- <property name="connection.password">root</property>
- <!-- JDBC connection pool (use the built-in) -->
- <property name="connection.pool_size">1</property>
- <!-- SQL dialect -->
- <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
- <!-- Enable Hibernate's automatic session context management -->
- <property name="current_session_context_class">thread</property>
- <!-- Disable the second-level cache -->
- <!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
- -->
- <!-- 配置二級快取 -->
- <property name="hibernate.cache.use_second_level_cache">true</property>
- <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
- <!-- hibernate3的二級快取配置 -->
- <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
- <!-- 開啟查詢快取 -->
- <property name="hibernate.cache.use_query_cache">true</property>
- <!-- Echo all executed SQL to stdout -->
- <property name="show_sql">true</property>
- <!-- Drop and re-create the database schema on startup -->
- <property name="hbm2ddl.auto">update</property>
- <mapping class="com.test.pojo.User" />
- </session-factory>
- </hibernate-configuration>
注意:hibernate4和hibernate3配置不一樣,hibernate4是
Xml程式碼- <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
而hibernate3的配置是
Xml程式碼- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
此處有一個疑問是:hibernate4的官方文件中,已經把class改了,但是屬性名稱沒有改,還是hibernate.cache.provider_class,不是上面的hibernate.cache.region.factory_class,但是寫成hibernate.cache.provider_class會報下面錯誤
Java程式碼- org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
- at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:186)
- at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:150)
- at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
- at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:264)
- at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790)
- at com.test.pojo.UserTest.beforeClass(UserTest.java:28)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
- at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
- at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
- at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
- at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
- at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
- Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
- at org.hibernate.cache.internal.NoCachingRegionFactory.buildTimestampsRegion(NoCachingRegionFactory.java:87)
- at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:62)
- at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:72)
- at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:40)
- at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:35)
- at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:91)
- at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:176)
- ... 20 more
說是hibernate.cache.region.factory_class屬性沒有配置,估計官方文件裡沒有把屬性改過來。。。
ehcache.xml
Xml程式碼- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
- updateCheck="false">
- <!--
- name:cache唯一標識
- eternal:快取是否永久有效
- maxElementsInMemory:記憶體中最大快取物件數
- overflowToDisk(true,false):快取物件達到最大數後,將快取寫到硬碟中
- diskPersistent:硬碟持久化
- timeToIdleSeconds:快取清除時間
- timeToLiveSeconds:快取存活時間
- memoryStoreEvictionPolicy:快取清空策略
- 1.FIFO:first in first out 先講先出
- 2.LFU: Less Frequently Used 一直以來最少被使用的
- 3.LRU:Least Recently Used 最近最少使用的
- -->
- <defaultCache maxElementsInMemory="1000" eternal="false"
- timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
- <cache name="userCache" eternal="false" maxElementsInMemory="1000"
- overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600"
- timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU" />
- </ehcache>
User實體類
Java程式碼- package com.test.pojo;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- @Entity
- @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
- public class User {
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private int id;
- private String name;
- private int age;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
UserTest測試類:
Java程式碼- package com.test.pojo;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class UserTest {
- private static SessionFactory sessionFactory = null;
- @BeforeClass
- public static void beforeClass() {
- Configuration configuration = new Configuration();
- try {
- configuration.configure();
- } catch (HibernateException e) {