简体   繁体   中英

Hibernate + Spring + session + cache

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM