具有多个服务器的 JPA
我目前正在开发一个使用 JPA(当前是 Toplink)来实现持久性的项目。目前,我们正在运行单个应用程序服务器,但是,为了冗余,我们希望添加一个负载均衡器和另一个应用程序服务器(随着它的增长,可能会更多)。
首先,我遇到了 JPA 缓存的问题。由于两个进程将更新同一个数据库,因此 JPA 缓存返回缓存的值而不是访问数据库。我了解如何关闭它,并且数据库本身实现了一定程度的缓存。完全关闭缓存是解决这个问题的方法吗?我看到了告诉 JPA 始终在查询级别从数据库获取数据的方法,但在多服务器环境中,您似乎总是希望这种情况发生。
除了这个具体问题之外,我还对任何使用多个应用程序服务器实现了 JPA 解决方案的人以及在实现过程中出现的问题(以及您的任何建议)感兴趣。
非常感谢。
I am currently working on a project that uses JPA (Toplink, currently) for its persistence. Currently, we are running a single application server, but, for redundancy, we would like to add a load balancer and another application sever (and possibly more as it grows).
First, I'm running into the issue of JPA caching. Since two processes will be updating the same database, the JPA cache returns the cached value rather than going to the database. I see how to turn that off, and the database itself implements a level of caching. Is turning off the cache completely the way to go here? I see the ways to tell JPA to always get from the database at a query level, but in a multi-server environment, it seems that you'll always want that to happen.
Along with this specific question, I'm interested in anyone out there who has implemented a JPA solution with multiple application servers and what problems arose during the implementation (and any suggestions you have).
Thanks much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所发现的,您可以禁用共享缓存,请参阅 http://wiki.eclipse。 org/EclipseLink/Examples/JPA/Caching 或 http://wiki.eclipse .org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F
根据您的数据和要求,EclipseLink 中还提供其他选项。
选项列表包括:
禁用共享缓存
启用缓存协调(请参阅http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/config/PersistenceUnitProperties.html#COORDINATION_PROTOCOL)
设置缓存失效超时(请参阅< a href="http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/annotations/Cache.html#expiry%28%29" rel="noreferrer">http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/annotations/Cache.html#expiry%28%29" eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/annotations/Cache.html#expiry%28%29)
启用乐观锁定,这将确保当更新过时时,任何过时的对象都无法更新 自动使缓存中的对象失效。
研究 EclipseLink 和 Oracle Coherence 的 Oracle TopLink 集成以提供分布式缓存。
另请参阅http://en.wikibooks.org/wiki/Java_Persistence/Caching#Caching_in_a_Cluster< /a>
没有完美的解决方案,通常使用的解决方案取决于数据/类,通常应用程序有一组只读类、主要读取类和主要写入类。就我个人而言,我会为只读启用缓存并设置 1 天超时,为主要读取启用具有缓存协调的缓存,并为主要写入禁用缓存。
As you have found, you can disable the shared cache, see http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching or http://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F
There are also other options available in EclipseLink depending on your data and requirements.
A list of option include:
Disable shared cache
Enable cache coordination (see, http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/config/PersistenceUnitProperties.html#COORDINATION_PROTOCOL)
Set a cache invalidation timeout (see, http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/annotations/Cache.html#expiry%28%29)
Enable optimistic locking, this will ensure that any stale object cannot be updated, when an update on stale data occurs it will fail, and EclipseLink will automatically invalidate the object in the cache.
Investigate the Oracle TopLink integration of EclipseLink and Oracle Coherence to provide a distributed cache.
See also, http://en.wikibooks.org/wiki/Java_Persistence/Caching#Caching_in_a_Cluster
There is no perfect solution, the solution used normally depend on the data/class, normally an application has a set of read-only classes, read-mostly classes and write mostly classes. Personally I would enable the cache for the read-only with a 1 day timeout, enable the cache with cache coordination for the read-mostly, and disable the cache for the write mostly.