2014-05-13 64 views
0

這裏是我的配置工作:休眠4.x版與第二級的Ehcache並不如預期

的pom.xml: 使用Hibernate和休眠-的Ehcache 4.2.8.Final版本

Spring配置:我有以下休眠特性

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 
<prop key="hibernate.cache.use_second_level_cache">true</prop> 

ehcache.xml中

<ehcache> 

    <diskStore path="java.io.tmpdir"/> 

    <!-- cache setting for com.cisco.locker.entity.SystemConfig entity --> 
    <cache name="com.cisco.locker.entity.SystemConfig" 
     maxElementsInMemory="100" 
     eternal="false" 
     timeToIdleSeconds="600" 
     timeToLiveSeconds="600" 
     overflowToDisk="false" 
     statistics="true" 
     /> 

</ehcache> 

實體 - com.cisco.locker.entity.SystemConfig.java

@Entity 
@Table(name = "system_config") 
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
public class SystemConfig implements Serializable { 
... 
} 

日誌:

First call: http://localhost:8080/test/getSystemConfigs 
{ 
    "statusCode": "200", 
    "statusMessage": "SUCCESS", 
    "response": { 
     "admin.lcu_status_polling_timer": "120", 
     "admin.site_status_polling_interval": "15" 
    } 
} 

Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ? 
2014-05-13 00:53:40,553 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-1]: com.cisco.locker.entity.SystemConfig cache - Miss 
2014-05-13 00:53:40,561 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-1]: com.cisco.locker.entity.SystemConfig cache - Miss 

Second call: http://localhost:8080/test/getSystemConfigs 
{ 
    "statusCode": "200", 
    "statusMessage": "SUCCESS", 
    "response": { 
     "admin.lcu_status_polling_timer": "120", 
     "admin.site_status_polling_interval": "15" 
    } 
} 

Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ? 
2014-05-13 00:53:48,037 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-2]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#11 
2014-05-13 00:53:48,037 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-2]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#12 


Third call – After manually changing value in DB "admin.site_status_polling_interval" from 15 to 10: http://localhost:8080/test/getSystemConfigs 
{ 
    "statusCode": "200", 
    "statusMessage": "SUCCESS", 
    "response": { 
     "admin.lcu_status_polling_timer": "120", 
     "admin.site_status_polling_interval": "10" 
    } 
} 

Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ? 
2014-05-13 00:54:08,738 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-4]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#11 
2014-05-13 00:54:08,738 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-4]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#12 

我不似乎明白緩存如何拿起最新值當我結束了做手工數據庫更改。日誌表明它也從緩存中選取這些更改後的值。

第二級緩存是否會選擇手動數據庫更改,即數據庫更改沒有通過休眠,因爲上述日誌提示?或者我有什麼不對嗎?

回答

0

那麼,正如日誌所示,對數據庫執行SQL查詢。數據來自數據庫,而不是來自緩存。你看到的點擊是商店點擊。

要從緩存中獲取實體,請使用session.get(),而不是查詢。

+0

JB Nizet ... api在內部使用session.get()。你看到日誌中的SQL查詢,因爲我有hibernate.show_sql = true屬性集。另外,您所說的「你看到的命中是商店命中」究竟意味着什麼?它是否意味着它從緩存中獲取值?感謝你的幫助! – Darshan

+0

所有where子句都是'where systemconf0_.property like?'。這不能是'session.get()'的結果。而對於我來說,商店的熱門話題就是當某個信息被存儲在緩存中時。不從緩存中獲取*。 –

+0

你是對的我正在使用HQL查詢不session.get()。讓我嘗試使用session.get()。 – Darshan