Java 持久化应用程序无状态与有状态
我是 JPA 的新手,我无法理解无状态和有状态的用法和区别。
有什么经验吗?
问候, 海瑟姆
I am new in JPA and I can't understand the use and the difference between stateless and stateful.
Any experiences?
regards,
Haythem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
无状态 和 有状态 是 EJB 会话 Bean 概念,而不是 JPA。
注意事项:JPA 是 EJB 规范的一部分,但可以在 EJB 容器外部单独使用。这通常是造成混乱的原因。
编辑:一个有用的SO线程,为什么使用有状态会话Bean。
Stateless and Stateful are EJB Session Bean concept, not of JPA.
Nota bene: JPA is part of EJB specification, but can be used separately and outside of an EJB container. This is what usually causes the confusion.
Edited: A useful SO thread, Why Use Stateful Session Beans.
“Stateful”和“Stateless”是会话 Bean 的属性。
会话 bean(简而言之)提供了一种调用应用程序服务器上的方法的方法。 bean 是 java 类的一个实例。通常,bean 在远程方法终止(并返回结果)后被销毁。这些 bean 是“无状态的”。
可以(但相当不寻常)向 bean 添加字段和属性,以便客户端可以在服务器上“创建”此实例并将其用于多个操作。这些 bean 是“有状态的”(应该避免)。
"Stateful" and "Stateless" are attributes for session beans.
A session bean (in brief) provides a way to call methods on an application server. The bean is an instance of a java class. Usually, a bean is destroyed after the remote method terminates (and returns a result). Those bean are "stateless".
It is possible (but rather unusual) to add fields and attributes to the bean so that a client can "create" this instance on the server and use it for more then one operation. Those beans are "stateful" (and should be avoided).
我认为造成混淆的部分原因是 Java Persistence API 是 EJB3 规范的一部分,有时似乎与 EJB 可以互换使用。直到我第一次学习Pro EJB 3:Java Persistence API时我才真正理解这一点。看看 http://en.wikipedia.org/wiki/Enterprise_JavaBean 它似乎提供了关于这个主题的一个很好的概述。
I think part of the confusion is that Java Persistence API is part of the EJB3 spec and sometimes seems to be used interchangeably with EJB. I didn’t really understand this until I first picked up Pro EJB 3: Java Persistence API. Take a look at http://en.wikipedia.org/wiki/Enterprise_JavaBean it seems to provide a good overview on the subject.
有状态和无状态是 EJB(Java EE 容器管理的会话 Bean)的属性,而不是任何特定于 JPA 规范的内容。对于来自同一客户端的每个请求,有状态 bean 都会具有与其关联的状态。 无状态 beans没有客户端特定的状态,并且单个实例可以在线程安全的情况下并发使用多个客户请求。
JPA 是一种可用于持久化有状态 bean 的持久化策略(我猜您也可以直接使用 JDBC,或插入其他一些持久化策略)。
Stateful and Stateless are properties of EJB's (Java EE container managed session Beans), rather than anything specific to the JPA spec. A Stateful bean will have state associated with it for each request from the same client. Stateless beans have no client specific state and a single instance could be used concurrently, threadsafe, between multiple clients requests.
JPA is one persistence strategy that can be used to persist stateful beans (I'm guessing you could also use JDBC directly, or plugin some other persistence strategy).
基本上,如您所知,Java 会编译一些东西,然后当您在 RAM 等任何地方存储变量、对象等时,如果类加载器之后一切顺利并避免执行阶段错误以及所有这些,您就会在运行时看到应用程序或者数据库,当用户与您的应用程序交互时,这些数据可能会发生变化,那么您开发了一个有状态的应用程序,如果您认为您的代码逻辑中没有什么是动态的(可以重塑应用程序行为的东西),那么您将要开发一个无状态应用程序。
简而言之,有状态应用程序意味着在运行时类属性的动态变化。
Basically, as you know, Java compiles things and then you see the app during runtime if everything went well after the class loader things and avoiding execution stage errors and all that, when you are storing variables, objects and so on in any place like RAM or Databases and these data can change when user interact with your application, then you developed a statefull application, if you believe there's nothing be dynamic in your code logic( things that can reshape a behavior of your application) then you are about to develop a stateless application.
In short, statefull application means dynamic change in your class properties during runtime.