5
votes

After my queries(lookups) get cached session closes, in a new session, hibernate is evicting everything after I change the DB through a random write Sql Query, how can I stop that from happening ? I'm looking into creating policies for things that rarely change.

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 configuration

 <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
Is your CacheMode set as REFRESH (i.e. CacheMode.REFRESH) - Santosh
Nope, I wrote a junit class to run the same queries in the same session without the random SQL in between and it goes from 20 sec to 1 sec to get a reply - Rodriguez
JUnit log 0st time - 20134 ms 1st time - 207 ms 2st time - 183 ms 3st time - 179 ms 4st time - 185 ms running random sql query 5st time - 20043 ms 6st time - 182 ms 7st time - 181 ms 8st time - 177 ms 9st time - 179 ms - Rodriguez

1 Answers

3
votes

found the Hibernate Issue https://hibernate.onjira.com/browse/HHH-2224

I tested with the bellow :

in my random query

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

whenever I call the above and it's a DB change it will invalidate only the cache syncronized by table = USER or USER_ADDRESS

and only syncronized random read queries or entities will be evicted

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