如何在ejb 3.0中实现缓存?

编程入门 行业动态 更新时间:2024-10-10 04:19:30
本文介绍了如何在ejb 3.0中实现缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个停留在EJB 3.0环境中的客户。没有@Singleton,没有bean管理的并发: - (

I have a customer who's stuck in an EJB 3.0 environment. No @Singleton, no bean-managed concurrency :-(

考虑到线程管理和同步被ejb规范禁止,如何实现缓存?

Considering thread management and synchronization is forbidden by the ejb specification, how to implement a cache? In essence, I want an non-synchronized object cache for some costly operations.

推荐答案

使用静态字段和同步的限制在EJB 3.0规范中有所规定

The restriction of using static field and synchronization is stated in EJB 3.0 spec chapter 21.1.2. It also explains why.

•企业bean不能使用读/写静态字段使用因此,建议将企业bean类中的所有静态字段声明为final。

• An enterprise bean must not use read/write static fields. Using read-only static fields is allowed. Therefore, it is recommended that all static fields in the enterprise bean class be declared as final.

此规则是必需的以确保一致的运行时语义,因为,而一些EJB容器可以使用单个JVM来执行所有企业bean的实例,其他人可以将实例分布在多个JVM上。

This rule is required to ensure consistent runtime semantics because while some EJB containers may use a single JVM to execute all enterprise bean’s instances, others may distribute the instances across multiple JVMs.

•企业bean不能使用线程同步原语来同步多个实例的执行。

• An enterprise bean must not use thread synchronization primitives to synchronize execution of multiple instances.

原因与上述相同。如果EJB容器通过多个JVM分布企业bean的实例,则同步将不工作。

This is for the same reason as above. Synchronization would not work if the EJB container distributed enterprise bean’s instances across multiple JVMs.

通过singleton POJO的缓存,如果EJB容器在多个JVM之间分布EJB实例,例如在集群环境中,那么在每个JVM中实际上有多个缓存实例的风险。

If you implement a cache by singleton POJO, you may have the risk that actually there are multiple cache instances in each JVM if EJB container distributed EJB instances across multiple JVMs, for instance, in a cluster environment.

因此,

  • 如果应用程序未部署在集群环境中,则可以这样做。据我所知,WebLogic和GlassFish在一个JVM中运行一个服务器实例。
  • 如果数据一致性不是必需的,你也可以这样做。通常在谈论缓存时允许使用。

如果它不适合你,你可能应该考虑外部缓存数据的EJB容器放在Redis或Hazelcast。

If it doesn't work for you, probably you should think about caching data outside of EJB container, e.g. put in Redis or Hazelcast.

更多推荐

如何在ejb 3.0中实现缓存?

本文发布于:2023-11-30 21:51:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   如何在   ejb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!