i specified the below properties in cfg file.
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider</property>
In my main1 program i create the person with id 1 and commited and made that thread on hold after commit. Then i start the another thread T2 which is getting ther person with id 1 with below code
person = (Person)session. get(Person.class,1);
when T2 passed above statement, it generate the below query in background
Hibernate: select person0_.id as id0_1_, person0_.cname as cname0_1_ from MyProject1.Person person0_ where person0_.id=?
I am not sure why thread T2 reading from database instead of getting it from second level cache? as i am using usage as read-write
My Mapping looks like
<class name="com.daasl.Person" >
<cache usage="read-write"/>
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name" column="cname" type="string"/>
</class>
Got the issue.Actually each main programm has its own jvm instance, hence its own session factory.So second level cache wont work here. I tried the same thing in the same main programe once session is closed after creating the person in it. Then open new session and tried to get the same person, it got it from second level cache.