2012-08-22 151 views
5

在我的查询(查找)获得缓存会话关闭后,在新会话中,在我通过随机写入Sql查询更改数据库后,hibernate会逐出所有内容,我该如何阻止发生?我正在研究为很少改变的事情制定政策。ehcache hibernate第二级缓存,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"/> 

弹簧hibernate配置

<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> 
+0

您的CacheMode设置为REFRESH(即CacheMode.REFRESH) – Santosh

+0

不,我写了一个junit类来在同一个会话中运行相同的查询,但没有随机SQL,它从20秒到1秒才得到答复 – Rodriguez

+0

JUnit log 0st time - 20134 ms 1st time - 207 ms第2次 - 183 ms第3次 - 179 ms第4次 - 185 ms运行随机sql查询第5次 - 20043 ms第6次 - 182 ms第7次 - 181 ms第8次 - 177 ms第9次 - 179 ms – Rodriguez

回答

3

发现休眠期https://hibernate.onjira.com/browse/HHH-2224

我与波纹管进行了测试:

在我的随机查询

<sql-query name="random_write_query" callable="false"> 
     <synchronize table="USER"/> 
     <synchronize table="USER_ADDRESS"/> 
     {CALL PACKAGE.FUNCTION(?)} 
</sql-query> 

每当我拨打以上,这是一个DB改变它会作废只能由表= USER或syncronized缓存USER_ADDRESS

只有syncronized随机读取查询或实体将被驱逐

<sql-query name="random_read_query"> 
     <synchronize table="USER"/> 
     <synchronize table="USER_ADDRESS"/> 
     <return-scalar column="USERNAME" type="string"/> 
     <![CDATA[ 
      SELECT USERNAME FROM USER, USER_ADDRESS... 
     ]]> 
    </sql-query> 
相关问题