• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with the Hibernate Cache (I think)

 
Janus Engstrøm
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fellow ranchers!

I am loading an object from the database, changing the object's value and then fetching the object from the database again, unfortunately not with the expected result.
I was expecting the reload of the object to have the value stored in the database (not the cache) since I am evicting the object from the session, but it is not so: The object still holds the changed value.


BUT there is more mystery to it: If the database is cleared so the Value Object is created, then all works as a charm. The problem arises when the Value Object is loaded from the database (o != null)

What am I doing wrong? How come I don't get the values from the database when fetching after the evict() ?

I don't use 2nd level cache. (<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>)



Please see my test class. The Value Object is just holding a String with a get(), set() and toString() method.

With kind regards,
Janus


 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, The problem is that when the object is already present in the database, you start the transaction but never commit it...because you commit it only after saving...and although you evict it from session, the hibernate seems to keep it..(because you have an active transaction)...

So, make a small change to your code..
move

inside the block


It works like a Charm... :-)
 
Janus Engstrøm
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul,

You just saved my day
Thank you very much for your feedback, and you're right, now it works like a charm!

I just don't understand _why_ Hibernate "blocks" access to the database when there is an open transaction? Perhaps someone could enlighten me or perhaps post a link to an appropriate webpage?

With kind regards,
Janus
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why_ Hibernate "blocks" access to the database when there is an open transaction


I dont think Hibernate blocks access to the database when there is an open transaction. Probably, it doesn't allow you to load that same entity (having the same id) that you have loaded in the session and in the open transaction, because it cant have two objects of same entity type and having same id in the same session.
it allows you to access any other record of that same table. So, it just blocks those records which are in the session.
But I also can't understand why the "s.evict()" didnt work, it should have removed that instance from that session.

P.S. I am only speculating here, probably it'll be a good time for experts to step in and clear the air..
 
Janus Engstrøm
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, Rahul! Let's introduce the heavy artillery

Your idea makes sense, but in that case it would be nice that Hibernate would throw some exception instead of just "ignoring" my request.


With kind regards,
Janus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic