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.  [ 1 post ] 
Author Message
 Post subject: Hibernate is Hitting DB instead of Second-level cache
PostPosted: Fri Apr 11, 2008 5:19 am 
Newbie

Joined: Tue Jan 29, 2008 2:49 am
Posts: 2
Hi ,
i was trying to test whether the data is retrieved from second level cache . I have to sessions s1 and s2. s1 is loaded an object with key.
now i am trying to load the object with same key from using the other session s2 , according to my expectation the object is already there in second-level cache and no need to retrieve it again from the DB.
But hibernate again issues a select query to load it again.

My configuration file setting inclues the following

Code:
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.show_sql=true


and hbm file is as follows

Code:
<hibernate-mapping>
<class name="hibernate.Contact" table="CONTACT">
  <cache include="all" usage="read-write"/>
  <id access="field" column="ID" name="id" type="long">
   <generator class="increment"/>
  </id>
  <property column="FIRSTNAME" generated="never" lazy="false"
   name="firstName" type="string"/>
  <property column="LASTNAME" generated="never" lazy="false"
   name="lastName" type="string"/>
  <property column="EMAIL" generated="never" lazy="false" name="email" type="string"/>
</class>
</hibernate-mapping>


Code:
long key = 1 ;
         Session session=null,s=null;
         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
         
         session = sessionFactory.openSession();
         s = sessionFactory.openSession();
         
         session.setCacheMode( CacheMode.NORMAL ) ;
         s.setCacheMode( CacheMode.NORMAL ) ;
         
         Transaction tx = session.beginTransaction();
                  
         Contact person = null ;
         person = ( Contact )session.load( Contact.class , key ) ;
         printContact( person ) ;
         session.flush() ;
         tx.commit();
         
         Transaction t = s.beginTransaction() ;

         person = ( Contact ) s.load( Contact.class , key ) ;
         person = ( Contact ) s.load( Contact.class , key ) ;

         printContact( person ) ;

         t.commit();

Can anybody suggest if there is any problem in what i did ? and also
according to my understanding firstlevel cache is associated with each session and is lost when it is closed and secondlevel cache is associated with SessionFactory specific and is shared to all the sessions created using the SessionFactory.

Thanks in advance
and
regards Sajith


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

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.