使用本机查询的第二级缓存无效 [英] 2nd level cache invalidation with native queries
问题描述
我有一个在JBoss AS 7安装中使用二级缓存的应用程序(Infinispan二级缓存提供程序).
我们有一些更新的JPQL查询会使缓存无效-我想知道如果在应用程序中包含一些本机SQL查询会产生什么影响.查询缓存会失效吗?
我还记得在Hibernate上使用sqlQuery.addSynchronizedQuerySpace(")指令来防止某些本机SQL查询的缓存失效. JPA是否也可以做到这一点?
谢谢!
I have an application that uses 2nd level cache on a JBoss AS 7 installation (Infinispan 2nd level cache provider).
We have some update JPQL Queries that invalidate the cache- I wonder what will be the effect if we include some native SQL queries in our application. Will the Query cache be invalidated ?
Also I remember using the sqlQuery.addSynchronizedQuerySpace("") instruction on Hibernate to prevent cache invalidation for some native SQL queries. Is it possible to do it also with JPA ?
Thanks!
推荐答案
我今天在处理同一问题时遇到了这个问题,所以我想将调查结果发布在这里.
I came across this question when dealing with the same problem today, so I thought I'd post my findings here.
使用JPA本机UPDATE/INSERT/DELETE查询确实会使Hibernate使整个 2级实体缓存无效.正如您在问题中提到的那样,Hibernate为此提供了一种解决方法,但是似乎不可能使用纯JPA来等效于Hibernate的addSynchronizedQuerySpace()
,addSynchronizedEntityClass()
和addSynchronizedEntityName()
.
Using JPA native UPDATE/INSERT/DELETE queries does cause Hibernate to invalidate the entire 2nd level entity cache. As you mentioned in your question, Hibernate has a workaround for this, but it seems to not be possible to do the equivalent of Hibernate's addSynchronizedQuerySpace()
, addSynchronizedEntityClass()
and addSynchronizedEntityName()
using pure JPA.
不过,JPA允许您执行的操作是 Query
对象即可访问JPA提供程序的API.如果您将Hibernate用作JPA提供程序,则将允许您使用Hibernate的addSynchronizedXxx
方法,如下所示:
What JPA however does allow you to do is to unwrap
a JPA Query
object to gain access to the JPA provider's API. If you're using Hibernate as a JPA provider, this will then allow you to use Hibernate's addSynchronizedXxx
methods as follows:
Query query = entityManager.createNativeQuery("UPDATE user SET ...");
query.unwrap(org.hibernate.SQLQuery.class)
.addSynchronizedEntityClass(User.class);
这不是理想的解决方案,但是它可以有效地防止整个二级缓存失效.
It's not an ideal solution, but it will effectively allow you to prevent the entire second level cache from being invalidated.
这篇关于使用本机查询的第二级缓存无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!