0

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?

2
  • commit each transaction you perform with DB. Commented 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. Commented Jun 30, 2012 at 12:44

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

That last statement is not accurate Alex in the case you propose since they have just cleared() the old state of Account and had it reloaded from the database with the new version
Sorry. That was an aside based on them sharing the same object without the reload. I should have been clear.
I've removed it because it didn't add any value anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.