hibernate里的session一级缓存,当一个用户通过get()load()等方法从数据库拿到某条数据,数据被缓存,此时第二个用户修改该条数据,数据库该数据被修改,此时第一个用户再get()该数据,从缓存中拿到数据,拿到的并不是实时的数据而是缓存的数据。这样想对不对?应该怎么解决?
伊谢尔伦2017-04-18 09:50:21
It’s like this, so the session is generally very short, because all the caches are gone when the session is closed and reopened
You can use evict() or clear() to refresh the cache within the session
天蓬老师2017-04-18 09:50:21
The Session interface defines a refresh() method. Call this method before calling the get() or load() method. You can get the latest data by calling this method;
阿神2017-04-18 09:50:21
How to get the latest data has been explained above. My suggestion is that the session first corresponds to a database transaction, and the execution time of the transaction should be as short as possible. Second, for data that changes elsewhere during the session, the official solution should be to use a lock mechanism. Optimistic locks can be used when a certain amount of transaction failure and rollback can be tolerated. Otherwise, pessimistic locks can be considered. The original poster will provide the actual solution. It’s up to you to weigh which method to use in your application.