java - session bean 中的静态变量限制

标签 java jakarta-ee distribution session-bean

不可能在 session bean 代码上使用静态变量。这种限制是武断的还是有根据的?为什么?

最好的问候

最佳答案

FAQ on EJB restrictions 中所述,使用 EJB 的限制之一是:

enterprise beans should not read or write nonfinal static fields

discussion on static fields中进一步扩展:

Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVM). Updating a static class field implies an intent to share the field's value among all instances of the class. But if a class is running in several JVMs simultaneously, only those instances running in the same JVM as the updating instance will have access to the new value. In other words, a nonfinal static class field will behave differently if running in a single JVM, than it will running in multiple JVMs. The EJB container reserves the option of distributing enterprise beans across multiple JVMs (running on the same server, or on any of a cluster of servers). Nonfinal static class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they are distributed.

It is acceptable practice to use static class fields if those fields are marked as final. Since final fields cannot be updated, instances of the enterprise bean can be distributed by the container without concern for those fields' values becoming unsynchronized.

关于java - session bean 中的静态变量限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9141673/

相关文章:

java - 为什么这段代码不是线程安全的?

iphone - 内部 iPhone 应用程序分发

java - 空间复杂度示例

java - Spring Data JPA 中的反向排序

java - 使用 WireMock 和 Eureka 的 Spring Boot 集成测试失败,返回 "No instances available"

java - 如何在数据库表已填充 JPA 时正确设置 @Id 字段

java - 为什么将类注释为@Service 不创建 bean?

mysql - 尝试使用 @DataSourceDefinition 部署数据源时出现 ClassNotFoundException

c++ - 分布和内部状态

python - 如果我们将概率密度函数数据作为 x 和 y 计算百分位数