简体   繁体   English

休眠+春季+会话+缓存

[英]Hibernate + Spring + session + cache

We are using Hibernate with Spring for our Java application. 我们在Java应用程序中使用Spring的Hibernate。 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从数据库获取帐户余额,然后user2增加余额,如果user1再次获得对象,则他在更新之前再次看到帐户余额(似乎会话使用其缓存中的值),但是我们希望使用new获取更新的对象账户余额。 User1 use one session during all activity that is different from user2 session. 在与user2会话不同的所有活动期间,User1使用一个会话。 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"); 这是设计使然(将Session视为“工作单元”); 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 ); 但无论如何,您可以使用session.refresh( theAccount );强制“其他会话”(在您的情况下为user1) 刷新其帐户状态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. 会话1将加载由会话2更改的对象。但是,会话1仍处于打开状态,该对象位于会话(第一级)缓存中。 You can refresh Session1 using session.clear() then if you reload the object you will get the updated version. 您可以使用session.clear()刷新Session1,然后如果重新加载该对象,则将获得更新的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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