We are using Hibernate with Spring for our Java application. We find out that when a session update something in database other sessions can not see the update. For example user1 get the account balance from database then user2 increase the balance , if user1 get the object another time he see the account balance before updating (seems that session use the value from its cache) but we want to get the updated object with new account balance. User1 use one session during all activity that is different from user2 session. Is any configuration to force to get the updated object from database? or any other help?
-
commit each transaction you perform with DB.Ved– Ved2012-06-30 11:28:34 +00:00Commented Jun 30, 2012 at 11:28
-
Please be more specific concerning transaction/session boundaries in terms of open/close, select/update/insert times of the two clients and the transaction isolation level you use.tscho– tscho2012-06-30 12:44:11 +00:00Commented Jun 30, 2012 at 12:44
2 Answers
That is by design (think of Session as "unit of work"); Sessions should be transactionally isolated. That is one of the vast many reasons Sessions should be short lived. Sounds to me like you might be using long lived Sessions.
But in any case, you can force the "other session" (user1 in your case) to refresh its state of the Account using session.refresh( theAccount );. REFRESH is a cascadeable action too, if you need dependent state to be refreshed as well when you refresh Account...
Comments
If you are using a single session for all activity then what you're seeing is to be expected.
Session 1 will load the object which is then changed by Session 2. However, Session 1 is still open the object is in the Session (first level) cache. You can refresh Session1 using session.clear() then if you reload the object you will get the updated version.