开发者

Hibernate multi user environment

Consider a situation where user 1 query the database thorugh application using hibernate(get or load or from customer where name = "gkp") he get开发者_开发问答s 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜