HQL更新是否清除二级缓存? [英] Does HQL update clear 2nd level cache?

查看:179
本文介绍了HQL更新是否清除二级缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原生查询正在清除二级缓存条目。来自7年前的休眠论坛的回答称,HQL更新查询还会清除第二级缓存。但是这仍然是真的吗?



由于HQL查询具有更新的确切字段,并且在哪个实体中,我认为它不应该那么难如果调用常规 session.save(..)

解决方案


原生查询正在清除第二级缓存项。





  1. 原生查询(实际上,这仅适用于本地插入/删除/更新,而不是查询)会使所有缓存(全局缓存)的二级缓存条目无效。我已经通过当前稳定版本的Hibernate验证了这一点:4.1.9。这很明智,因为Hiberante不可能知道DB中发生了什么变化,所以唯一的选择是使整个二级缓存失效。在一些严重依赖二级缓存的系统中,这可能是一个严重的问题。

有可能指定从应该使二级缓存失效(甚至指定没有任何东西从缓存中被逐出)。
看看好博客文章 http://www.link-intersystems.com/bin/view/Blog/Hibernate%27s+second+level+cache+and+native+queries 详细解释了这一点。



为防止Hibernate使缓存中的任何内容失效:

  SQLQuery sqlQuery = session。 createSQLQuery(ALTER SESSION SET NLS_COMP ='BINARY'); 
sqlQuery.addSynchronizedQuerySpace();
int updatedEntities = sqlQuery.executeUpdate();

指示Hibernate仅使Person实体缓存失效:

  SQLQuery sqlQuery = session.createSQLQuery(UPDATE PERSON SET ... WHERE ...); 
sqlQuery.addSynchronizedEntityClass(Person.class);
int updatedEntities = sqlQuery.executeUpdate();




来自7年前的hibernate论坛的回答称,HQL
更新查询还会清除二级缓存。但是,这仍然是真的吗?




  1. HQL只会使第二级缓存与你正在做一些插入/更新/删除的实体有关的区域。这很有道理,因为Hibernate知道哪些实体受到HQL的影响,它只能清除该实体的第二级缓存区域。

示例:

  entityManager .createQuery(从p中删除p.id = 1。)。executeUpdate(); 

这将仅使Person实体缓存失效。



我不知道有什么可能阻止Hibernate使HQL的特定实体的二级缓存无效。


Native queries are clearing the 2nd level cache entries. An answer from the hibernate forum that is 7 years old says that HQL update queries also clear the 2nd level cache. But is this still true?

Since the HQL query has the exact fields to be updated, and in which entity, I think it shouldn't be that hard to behave as if a regular session.save(..) is invoked.

解决方案

Native queries are clearing the 2nd level cache entries.

  1. Native queries (actually this is only for native inserts/deletes/updates, not queries) are invalidating the 2nd level cache entries for all caches (WHOLE CACHE). I have verified this with the current stable version of Hibernate: 4.1.9. This is sensible because Hiberante can't possibly know what was changed in the DB, so the only option is to invalidate whole 2nd level cache. This can be a serious issue in some systems which relies heavily on the 2nd level cache.

There is though a possibility to specify what from 2nd level cache should be invalidated (or even specify that nothing is evicted from cache). Look at great blog post http://www.link-intersystems.com/bin/view/Blog/Hibernate%27s+second+level+cache+and+native+queries where this is explained thoroughly.

To prevent Hibernate from invalidating anything from cache:

SQLQuery sqlQuery = session.createSQLQuery("ALTER SESSION SET NLS_COMP = 'BINARY'");
sqlQuery.addSynchronizedQuerySpace("");  
int updatedEntities = sqlQuery.executeUpdate();

To instruct Hibernate to invalidate only the Person entity cache:

SQLQuery sqlQuery = session.createSQLQuery("UPDATE PERSON SET ... WHERE ...");
sqlQuery.addSynchronizedEntityClass(Person.class);
int updatedEntities = sqlQuery.executeUpdate();

An answer from the hibernate forum that is 7 years old says that HQL update queries also clear the 2nd level cache. But is this still true?

  1. HQL does invalidate only a the 2nd level cache region which is related to the entity you are doing some inserts/updates/deletes on. That makes sense because Hibernate knows which entities are affected by the HQL, it can clear only the 2nd level cache region for this entity.

Example:

entityManager.createQuery("delete from Person p where p.id = 1").executeUpdate();

This will invalidate "only" the Person entity cache.

I am not aware of any possibility to prevent Hibernate from invalidating 2nd level cache for specific entity when it comes to HQL.

这篇关于HQL更新是否清除二级缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆