Java Persistance Application Stateless vs. Stateful

14.8k views Asked by At

I am new in JPA and I can't understand the use and the difference between stateless and stateful.

Any experiences?

regards, Haythem

5

There are 5 answers

1
Andreas Dolk On BEST ANSWER

"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).

2
Adeel Ansari On

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.

0
Joel On

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).

0
K2J On

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.

0
Aramis NSR On

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.