我有一个停留在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中实现缓存?
发布评论