views:

138

answers:

1

Consider a situation where user 1 query the database thorugh application using hibernate(get or load or from customer where name = "gkp") he gets the data.After this a DBA manually updates that particular row by exeuting a update query in db.If a second user executes the same query will he get the updated data or the old one(which the user 1 sees).

Will hibernate store the results in session and gives back to the second user.What happens actually?

+3  A: 

The second user will get an object containing the old data if you have second-level caching enabled, and one containing the new data if not. Unless of course they are using the same ISession instance, which would not be advisable.

First-level caching is at the session level; second-level is at the session factory level. If you have updates that are made directly rather than via NHibernate, they will not be picked up by either level of caching, so if you are hitting the cache (either with different sessions and second-level caching turned on, or through using the same session) you will not see the changes that have been made.

In these scenarios, it often makes sense to use a version column of some sort (e.g. SQL Server timestamp, Oracle ORA_SCN) and optimistic offline locking, to prevent updates being made to an entity which has actually been updated underneath by another process.

David M
Thanks for explanation but its somewhat confusing//The second user will get the old one if you have second-level caching enabled, and a new one if not //what you mean by //and a new one if not???? does it mean if i use first level cache then the second user gets the updated value?if that s the point u r trying to say thenwhy this statment //If you have updates that are made directly rather than via NHibernate, they will not be picked up by either level of caching.could you pls tell me clearly ..
karthick prabhu
I could think that versioning is the option but the intial thought i got when i saw this problem was "every user will have separate session , then there wont be a chance for same session usuage(first level cache) , second level also not possible so only one solution is versioning"correct me if i am wrongthanks
karthick prabhu
Answer edited. If you are in an environment where you are using NHibernate, but some data updating is carried out not via NHibernate, and you want a user not to be able to save changes to a row that has been updated since he first fetched it, then yes, versioning is absolutely your only option.
David M

related questions