-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Second level cache and sessionFactory?
PostPosted: Wed Dec 10, 2008 8:56 am 
Newbie

Joined: Tue Dec 09, 2008 10:48 am
Posts: 4
Location: FRANCE
Hello,
I'm wondering about second level cache: I'm trying to activate it and to see the performance after execution with cache. I think that a first level cache is associated to a session, and the second one is associated to the session Factory...

So, i have done necessary configurations in order to activate the second level cache, and i think it's ok and it's runing: this is the content of the region of my Customer class:

Code:
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=3097,elementCountInMemory=3097,elementCountOnDisk=0,sizeInMemory=4923969]



when i'm executing the same function "getCustomers" for the second time, in the same session, it doesn't generate all queries associated to collections, what is what i want, but i was surprised when i desactivate the cache, it do the same thing=> i deduced that it was the first level cache which was runing...

For this reason, i closed the session but still in the same sessionFactory, i execute the function "getCustomers()" and now it doesn't read from cache even, my second level is containing all data i want.... I put here the content if the statistic after:
1- executing 3 times the function ,
2-closing the session
3-Statistics are:
Code:
Statistics[start time=1228834340015,sessions opened=2,sessions closed=1,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=3,statements prepared=3750,statements closed=3750,second level cache puts=8130,second level cache hits=0,second level cache misses=3747,entities loaded=4408,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=25,collections loaded=3722,collections updated=0,collections removed=0,collections recreated=0,collections fetched=3722,queries executed to database=3,query cache puts=0,query cache hits=0,query cache misses=0,max query time=27812]


4- the call to the function leads to the execution of n+1 select to bring all collections....

Could you explain me please, what is wrong?

Thank you for help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 10:01 am 
Beginner
Beginner

Joined: Thu Oct 02, 2008 10:15 am
Posts: 21
There are many things to consider here before we can come to a conclusion:
1) Which implementation of Second Level Cache[SLC] are you using, Oscache etc.?
2) There are some configuration/coding changes needed.
a) Have the osache.properties in the classpath.[In case of Oscache]
b)hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider
c) Before executing the query:
getHibernateTemplate().setCacheQueries(true);
d) In the mapping file
<cache usage="read-only"/>

Now, the above code samples are just a guideline and assumes that your are using Spring
and a hibernate.properties file.

If your configuration is all set, you should be able to use the Second level Cache
without any problems.
Let me know.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 11:45 am 
Newbie

Joined: Tue Dec 09, 2008 10:48 am
Posts: 4
Location: FRANCE
this is la configuration of my file .cfg.xml:

Code:
<!-- cache configuration -->
         <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
         <property name="hibernate.cache.use_second_level_cache">true</property>
         <property name="hibernate.cache.use_query_cache">true</property>
       <property name="hibernate.cache.use_minimal_puts">true</property>


and this my mapping file:

Code:
<class
        name="fr.test.hib.bean.Customer"
        table="Customer"
        lazy="false"
    >
        <cache usage="read-write" />


I have also my Ehcache.xml file:


Code:
<defaultCache
   maxElementsInMemory="10000"
   eternal="false"
   overflowToDisk="true"
   timeToIdleSeconds="120"
  />
 
  <cache name="fr.test.hib.bean.Customer"
    maxElementsInMemory="1000"
    eternal="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600"
    overflowToDisk="false"
  />
<cache name="org.hibernate.cache.StandardQueryCache"
      maxElementsInMemory="5"
      eternal="false"
      timeToLiveSeconds="120"
      overflowToDisk="false"/>

   <cache name="org.hibernate.cache.UpdateTimestampsCache"
      maxElementsInMemory="1000"
      eternal="true"
      overflowToDisk="false"/>
 



I think the cache is runing while in the statistics it's written:

Code:
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=3097.....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 12:05 pm 
Beginner
Beginner

Joined: Thu Oct 02, 2008 10:15 am
Posts: 21
If you are running the cached-data-query again in "another session", you should see either the hit count or miss count going up. And it seems both of these are 0 for you.
Makes me wonder if you running the queries in the same session?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 4:56 pm 
Newbie

Joined: Tue Dec 09, 2008 10:48 am
Posts: 4
Location: FRANCE
i'm running the queries in the same session factory but not in the same session, which is my goal...
It' runing now, i can't explain why, but i changed the usge to read-only and it worked!!!!
thank you for help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.