我为我的应用程序使用Hibernate 3.2.5。我正在尝试执行Query Cache
,但它不起作用。查询缓存不工作
问题描述:
对于第一次,DB被命中,数据被取出并缓存。第二次,对于相同的查询,数据库被命中,而不是从缓存中获取数据。第二次,我希望它从缓存中取出它,而不是再次击中数据库。
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
创建的文件:
为使Query Cache
,我在cfg.xml
文件作出了以下项ehcache.xml
并添加在hbm.xml
文件下面的条目:
<cache usage="read-only" />
下面是我试过的代码:
SessionFactory sf = new Configuration().configure("trial.cfg.xml").buildSessionFactory();
Session session = sf.openSession();
List departments1 = session.createQuery("from Dept dept where dept.deptId = 1")
.setCacheable(true)
.setCacheRegion("departmentId")
.list();
//Some business operations
session.flush();
session.close();
//Some business operations
Session session1 = sf.openSession();
List departments2 = session1.createQuery("from Dept dept where dept.deptId = 1").list();
//In the above line again it is hitting the DB rather than taking it from the cache.
我相信我错过了一些不会从缓存中获取数据并因此触及数据库的东西。请让我知道如何使这个Query Cache
工作。
是什么让你说它正在打击数据库?如果您只是依靠日志文件中打印的select语句,那么这不是正确的观察。 – JSS
是的,我依靠日志文件中打印的select语句来检查它是否是数据库命中。请让我知道什么是其他方式/正确的方法来检查它是否是数据库? – user182944
我等待得到这个答复...但似乎我必须为此创建一个不同的线程:( – user182944