简体   繁体   English

Java 持久化应用程序无状态与有状态

[英]Java Persistance Application Stateless vs. Stateful

I am new in JPA and I can't understand the use and the difference between stateless and stateful.我是 JPA 新手,无法理解无状态和有状态之间的用途和区别。

Any experiences?有什么经验吗?

regards, Haythem问候,海瑟姆

Stateless and Stateful are EJB Session Bean concept, not of JPA . StatelessStateful是 EJB Session Bean 的概念,而不是JPA 的概念。

Nota bene: JPA is part of EJB specification, but can be used separately and outside of an EJB container.注意: JPA 是 EJB 规范的一部分,但可以单独使用,也可以在 EJB 容器之外使用。 This is what usually causes the confusion.这通常是造成混淆的原因。

Edited: A useful SO thread, Why Use Stateful Session Beans .编辑:一个有用的 SO 线程, Why Use Stateful Session Beans

"Stateful" and "Stateless" are attributes for session beans. “有状态”和“无状态”是会话 bean 的属性。

A session bean (in brief) provides a way to call methods on an application server.会话 bean(简而言之)提供了一种在应用程序服务器上调用方法的方法。 The bean is an instance of a java class. bean是java类的一个实例。 Usually, a bean is destroyed after the remote method terminates (and returns a result).通常,在远程方法终止(并返回结果)后销毁 bean。 Those bean are "stateless".那些 bean 是“无状态的”。

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.可以(但很不寻常)向 bean 添加字段和属性,以便客户端可以在服务器上“创建”此实例并将其用于多个操作。 Those beans are "stateful" (and should be avoided).这些 bean 是“有状态的”(应该避免)。

Stateful and Stateless are properties of EJB's (Java EE container managed session Beans), rather than anything specific to the JPA spec.有状态和无状态是EJB (Java EE 容器管理的会话 Bean)的属性,而不是特定于 JPA 规范的任何属性。 A Stateful bean will have state associated with it for each request from the same client.对于来自同一客户端的每个请求, 有状态bean 将具有与其关联的状态。 Stateless beans have no client specific state and a single instance could be used concurrently, threadsafe, between multiple clients requests. 无状态bean 没有客户端特定的状态,并且可以在多个客户端请求之间并发、线程安全地使用单个实例。

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). JPA 是一种可用于持久化有状态 bean 的持久化策略(我猜你也可以直接使用 JDBC,或者插入一些其他持久化策略)。

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.我认为部分混淆是 Java Persistence API 是 EJB3 规范的一部分,有时似乎可以与 EJB 互换使用。 I didn't really understand this until I first picked up Pro EJB 3: Java Persistence API .直到我第一次拿起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.看看http://en.wikipedia.org/wiki/Enterprise_JavaBean它似乎提供了关于这个主题的很好的概述。

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.基本上,如您所知,Java 会编译内容,然后您会在运行时看到应用程序,如果在类加载器之后一切顺利并避免执行阶段错误等等,当您将变量、对象等存储在 RAM 等任何地方时或当用户与您的应用程序交互时,数据库和这些数据可能会发生变化,然后您开发了一个有状态的应用程序,如果您认为您的代码逻辑中没有任何动态(可以重塑应用程序行为的东西),那么您将要开发一个无状态应用程序。

In short, statefull application means dynamic change in your class properties during runtime.简而言之,有状态应用程序意味着在运行时动态更改类属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM