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