【发布时间】:2012-08-22 09:46:25
【问题描述】:
在我的查询(查找)获得缓存会话关闭后,在一个新会话中,我通过随机写入 Sql 查询更改数据库后,hibernate 正在逐出所有内容,我该如何阻止这种情况发生?我正在考虑为很少改变的事物制定政策。
INFO Executing [namedSqlQuery=dao.web_login, objs=[user1]]
DEBUG org.springframework.orm.hibernate3.SessionFactoryUtils user1- Opening Hibernate Session
DEBUG org.hibernate.impl.SessionImpl user1 - opened session at timestamp: 5511432318976000
DEBUG org.hibernate.impl.SessionFactoryImpl user1- evicting second-level cache: USERS_LOOKUP
DEBUG org.hibernate.impl.SessionFactoryImpl user1- evicting second-level cache: COUNTRY_LOOKUP
ecache.xml
<cache name="query.oneHourPolicy"
maxElementsInMemory="10000"
eternal="false"
timeToLiveSeconds="3600"
diskPersistent="true"
overflowToDisk="true"/>
spring 休眠配置
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
【问题讨论】:
-
您的 CacheMode 是否设置为 REFRESH(即 CacheMode.REFRESH)
-
不,我写了一个 junit 类在同一个会话中运行相同的查询,中间没有随机 SQL,它从 20 秒到 1 秒得到回复
-
JUnit 日志第 0 次 - 20134 毫秒 第 1 次 - 207 毫秒 第 2 次 - 183 毫秒 第 3 次 - 179 毫秒 第 4 次 - 185 毫秒 运行随机 sql 查询 第 5 次 - 20043 毫秒 第 6 次 - 182 毫秒 第 7 次时间 - 181 毫秒 第 8 次 - 177 毫秒 第 9 次 - 179 毫秒
标签: java spring hibernate caching ehcache