2011-11-18 85 views
1

我在cfg文件中指定了下面的屬性。對象沒有從休眠中的二級緩存中獲取?

<property name="hibernate.cache.use_second_level_cache">true</property> 
<property name="hibernate.cache.provider_class"> 
    net.sf.ehcache.hibernate.EhCacheProvider</property> 

在我的main1程序中,我創建了id爲1的人並提交併使該線程在提交後保留。然後,我開始這是越來越療法人ID爲1與下面的代碼的另一個線程T2

person = (Person)session. get(Person.class,1); 

當T2上面的語句通過,它產生的背景下面的查詢

Hibernate: select person0_.id as id0_1_, person0_.cname as cname0_1_ from  MyProject1.Person person0_ where person0_.id=? 

我不知道爲什麼線程T2從數據庫中讀取而不是從二級緩存中讀取?因爲我使用的用法讀寫

我的映射看起來像

<class name="com.daasl.Person" > 
<cache usage="read-write"/> 
    <id name="id" type="int"> 
    <generator class="increment"/> 
    </id> 
    <property name="name" column="cname" type="string"/> 
</class> 
+0

你有沒有[啓用](http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e2196)上'Person'實體緩存?你的實體是'Serializable'嗎?你配置好了ehcache.xml嗎? –

+0

嗨Tomsaz,你過去在啓用Person實體緩存答案在這裏? - 你可以看到使用的人員映射。是你的實體Serializable? - 是的。您是否已正確配置ehcache.xml中?-As我在後begining說,我配置了性能即hibernate.cache.use_second_level_cache並在hibernate.cfg.xml在hibernate.cache.provider_class。還有什麼我們需要配置。 –

回答

1

得到了issue.Actually各主要PROGRAMM都有自己的JVM實例,因此它自己的會話factory.So二級緩存不會在這裏工作。在創建人員之後關閉會話後,我在同一主程序中嘗試了相同的操作。然後打開新的會話,並試圖找到同一個人,它從二級緩存中獲得它。