ehcache hibernate二级缓存,hibernate自动驱逐

时间:2012-08-22 09:46:25

标签: java spring hibernate caching ehcache

在我的查询(查找)之后,缓存会话关闭,在新会话中,hibernate在通过随机写入Sql Query更改数据库之后驱逐所有内容,我该如何阻止它发生?我正在研究为很少改变的事情制定政策。

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

1 个答案:

答案 0 :(得分:3)

找到了Hibernate问题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>

每当我调用上面的内容并且它是数据库更改时,它将仅使table = USER或USER_ADDRESS同步的缓存无效

只有同步的随机读取查询或实体才会被驱逐

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