使JPA EntityManager会话无效 [英] Invalidating JPA EntityManager session
问题描述
我正在使用的一个项目使用Spring 2.5&使用Hibernate作为提供者的JPA。
我的DAO类扩展了JpaDaoSupport,所以我使用getJpaTemplate()方法得到我的JpaTemplate。
数据库可以通过我的应用程序或第三方应用程序进行更改。
当第三方应用程序更改数据库(主要是配置数据更改)时,我需要为应用程序的用户提供一种使所有JPA会话无效并重新加载新数据(即,使后台中的所有休眠会话无效)。这需要我的应用程序的所有并发用户看到。
我该怎么做?
有两级缓存:第一级是EntityManager自己的缓存。
您可以在一个实体上 refresh
,它将从数据库重新加载,或者您可以 clear
实体管理器本身,在这种情况下,所有实体都从缓存中移除。 JPA无法驱逐一个特定的实体从缓存中。根据您使用的实施情况,您可以执行此操作,例如Hibernate的 evict
方法。
二级缓存是全局缓存。
JPA 1.0没有提供对二级缓存的支持。然后您需要依赖基础的特定实现,或者禁用它。使用
@Cache $ JPA 2.0将解决此问题 c $ c>注释和缓存API。您可以使用特定于Hibernate的API清除二级缓存,例如 SessionFactory.evict(...)
。
高级缓存问题包括: 查询缓存 某些查询的结果可以被缓存。在JPA 1.0中再次不支持它,但是大多数实现都可以指定哪些查询将被缓存以及如何进行缓存。
群集
然后也是繁琐的同步群集中节点间高速缓存的问题。在这种情况下,这主要取决于所使用的缓存技术,例如, JBoss缓存。
你的问题仍然是通用的,答案取决于你到底在做什么。
我在一个系统上工作了许多更新,而没有经过hibernate,我们终于禁用了二级缓存。
但是,您也可以跟踪所有打开的会话,并在必要时逐出所有打开会话的所有一级缓存,以及二级缓存。你仍然需要自己管理同步,但我想这是可能的。
A project I am working on uses Spring 2.5 & JPA with Hibernate as a provider.
My DAO classes extend JpaDaoSupport, so I get my JpaTemplate using the getJpaTemplate() method.
The back-end database can get changed either by my application, or a third-party application.
When a third-party application changes the database (mostly configuration data changes), I need to provide the user of my application a way to invalidate all JPA sessions and reload the new data (i.e. invalidate all the hibernate sessions in the background). This needs to be "seen" by all concurrent users of my application.
How can I do this?
There are two levels of caches:
1st level is the EntityManager's own cache.
You can either
refresh
on one entity and it will be reloaded form the database, or you canclear
the entity manager itself, in which case all entities are removed from the cache. There is no way with JPA to evict only one specific entity from the cache. Depending on the implementation you use, you can do this, e.g. Hibernate'sevict
method.2nd level caching is the global cache.
JPA 1.0 did not provide support for 2nd level cache. You need then to rely on the underlying specific implementation, or to disable it. JPA 2.0 will address this issue with
@Cache
annotation and the cache API. You can clear the 2nd level cache using Hibernate-specific API, e.g.SessionFactory.evict(...)
.
Advanced issues with caching are:
Query cache
Result of certain queries can be cached. Again not support for it in JPA 1.0, but most implementation have ways to specify which query will be cached and how.
Clustering
Then comes also the tedious problem of synchronizing caches between nodes in a cluster. In this case, this mostly depend on the caching technology used, e.g. JBoss cache.
Your question is still somehow generic and the answer will depend on what you are exactly doing.
I worked on a system were many updates would be done without going through hibernate, and we finally disabled the 2nd level cache.
But you could also keep track of all opened sessions, and when necessary evict all 1st level cache of all opened session, plus the 2nd level cache. You would still need to manage synchronization yourself, but I imagine it's possible.
这篇关于使JPA EntityManager会话无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!