-->
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.  [ 3 posts ] 
Author Message
 Post subject: Querying after insert. second level cache is not storing
PostPosted: Mon Feb 22, 2010 9:55 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
I am learning second level cache, It didn't work properly. Can anyone explain this please.

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
C c1 = new C();
c1.setName("c1");
session.saveOrUpdate(c1);
session.getTransaction().commit();

session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
C test = (C)session.get(C.class, c1.getId()); // I expected to get this object from second level cache. But hibernate does a select a select.
// Hibernate did ask the second level cache for this id before the select but second level cache didn't have it

session.getTransaction().commit();

session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
test = (C)session.get(C.class, c1.getId()); // Now second level cache returns the object
session.getTransaction().commit();

This is the mapping file.

<class name="C" table="TABLE_C" >
<cache usage="read-write"/>
<id name="id" column="C_ID">
<generator class="native" />
</id>
<property name="name" />
</class>

I thought Hibernate would put the result of insert into the second level cache. But It didn't


Last edited by kavithakaran on Mon Feb 22, 2010 3:58 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Querying after insert. second level cache is not helping
PostPosted: Mon Feb 22, 2010 10:40 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
I have done some debugging and I can simplify my question.

Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
C c1 = new C();
c1.setName("c1");
session.saveOrUpdate(c1);// This one need an immediate insert in order to get the Id assigned
session.getTransaction().commit();


But at the end of this transaction, second level cache doesn't have c1 stored.

However If I use assigned primary key as below where the insertion can wait until I call commit, second level cache stores the c1.

Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
C c1 = new C();
c1.setName("c1");
c1.setId(1L);
session.saveOrUpdate(c1);// This call won't trigger an immediate insert
session.getTransaction().commit();


Top
 Profile  
 
 Post subject: Re: Querying after insert. second level cache is not storing
PostPosted: Tue Feb 23, 2010 12:20 pm 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
It is only objects that are inserted into the immediately into the database are not going to the second cache.
If I do an update immediately after the insert, the object gets stored into the second level cache.
(I inserted the line highlighted by bold and it get stored into the second level cache)

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
C c1 = new C();
c1.setName("c1");
session.saveOrUpdate(c1);// This one need an immediate insert in order to get the Id assigned

c1.setName("change it and see if you are getting stored into second level cache");

session.getTransaction().commit();

After that, just to see what happens if I force update to happen immediately,I did a session.flush() after the bold line. Again the object is available in second level cache.(so my next doubt here is hibernate should use memory locks here)

It is only the object that need an immediate sql insert are not going to the second level cache.

Can someone clear this please?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.