Question

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

Any experiences?

regards, Haythem

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top