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.  [ 6 posts ] 
Author Message
 Post subject: How to force hibernate to flush and retrieve updated values
PostPosted: Tue Jul 22, 2008 1:24 am 
Newbie

Joined: Thu Feb 28, 2008 12:35 pm
Posts: 15
Hi all,

I am facing problem in inconsistent data state with hibernate due to delayed flush().

We are using hibernate and spring in the application.

I have the mapping between three tables:
Class A having multiple instances of class B which in turn has mulitple instances of class C.

When i save class C, i am updating class B and class A also and using saveorupdate method.

Once the save is over, i am using hibernate Query to retrieve the saved instance values and display to the user.

But in one of our testing environments, hibernate is not flushing the changes immediately and this is resulting in inconsistent display issue to the user.

Please suggest me as to how should i resolve this issue..

i tried using session.flush() after the save but still the issue is observed.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 22, 2008 5:53 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Are class B and class A being updated within the same transaction, or in different transactions with different Hibernate sessions? I'd first ensure it was all happening within the same transaction.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 22, 2008 7:52 am 
Newbie

Joined: Thu Feb 28, 2008 12:35 pm
Posts: 15
All the classes are updated in the same transaction to keep in sync all of them. I am doing session.flush() to manually flush the changes to the database.

this scenario is working fine in my machine but when the same code runs in other environment which is quite loaded, the hibernate is not flushing the changes immediately.

This is resulting in scenario where user updates the values but views the previous values. But after sometime, he is able to view the changes.

I am thinking of following strategies:

1. DO session.refresh() after i re-query the hibernate objects. This may try to get the latest state of the hibernate object.

2. DO session.evict() and sessionFactory.evict() to remove any cached instances.

Please suggest if my strategies are any good. If you have any other suggestion, you are welcome..

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 22, 2008 9:33 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
After a flush you should see the changed values.

Are you using a 2nd level cache?


Use logging to check:
- when and what statements sql statements are issued
- which results are retrieved from the db and which come from the 2nd level cache
- if the 2nd level cache is invalidated apropriatly.


Just to check: You aren't using a new jdbc connection for every call, right?
kind regards.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 23, 2008 8:23 am 
Newbie

Joined: Thu Feb 28, 2008 12:35 pm
Posts: 15
I had put an interceptor for hibernate events and this is what i am able to observe so far..

1. when i query the hibernate objects through mapping e.g., classAobj.getClassBObjects(), hibernate is firing query to retrieve the ClassB objects as expected.

2. When i change this flow by replacing querying part for ClassB objects by firing a seperate query instead, hibernate is first doing a dirty check on the classA object and then fires the query.

ClassB is mapped as lazy = true collection with ClassA.

So, I am wondering in which instance of the above (i.e., querying through mapping and querying through hsql query) will hit the database and get the latest data...

any useful links will be appreciated.


Top
 Profile  
 
 Post subject: Re: How to force hibernate to flush and retrieve updated values
PostPosted: Thu Jul 07, 2011 10:43 am 
Newbie

Joined: Wed Apr 04, 2007 5:02 pm
Posts: 3
I know it's been a while, but I am facing that same issue for quite some time now. I basically did all I could to avoid such a scenario. By all I could, I mean hacks. Like security checks if it was really written to the DB. Ridiculously often flush the session. But sometimes a property value is simply not written to the database. Not even delayed. And it mostly happens in heavy load production environment. Locally it doesnt happen at all. It even occurs in unit tests from time to time...

I'm clueless.

Anyone with an idea out there?


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