When to use Stateful or Stateless

Asked

Viewed 30,862 times

34

I would like to know the difference and when to use.

  • Stateful
  • Stateless

3 answers

42


The difference between Stateful and Stateless is that one will save the state of the objects(Stateful) and the other will recognize each request as a new request(Stateless).


Stateless:

  • no record of all previous interactions are saved.
  • each interaction is treated on the basis of the information available to the interacting

Example: Internet Protocols (IP), Hyper Text Broadcast Protocol.

Perks:

  • Reduce memory usage on the server.
  • reduces expired session problems

Disadvantages:

  • Harder to maintain user interaction and create an application seamless web
  • May require extra information to be sent to and from customer

Statefull

  • The Opposite of the Stateless
  • Keeps track of the interaction state
  • Allows data to be maintained between different requests.

Example: Java:Httpsession, APS.net: Session

Perks:

  • Keep track of a user throughout the application run.
  • More intuitive, entity data can be kept on the server between changes
  • Can improve performance when data recovery is required only once.

Disadvantages:

  • Requires memory to be allocated to store the data
  • May lead to decreased performance if session storage is not maintained efficiently

2

Oracle JAVA EE 6 documentation regarding:

Stateful Session Beans

The state of an Object consists of the values of its instance variables. In a stateful Session bean, the instance variables represent the state of a Unique client/bean Session. Because the client interacts ("Talks") with its bean, this state is often called the Conversational state.

Stateless Session Beans

A stateless Session bean does not maintain a Conversational state with the client. When a client invokes the methods of a stateless bean, the bean’s instance variables may contain a state specific to that client but only for the Duration of the Invocation. When the method is finished, the client-specific state should not be retained.

Very simple summary:

Utilise Stateful when you need to maintain the conversational state.

Example: In a shopping cart, where you add several items and they are items relevant only to a customer, that is, there is the need to keep the list.

Utilise Stateless when there is no need to maintain the state of values.

Example: List store products (it’s the same list for any customer, right?)

The above example is quite simple, just to illustrate the idea of stateful and stateless, the use of Ejbs is usually much more complex.

-1

As far as I know Stateful keeps the status of the object during the session. Stateless keeps it independent of the session.

Example you have a program of buying and selling dollars. In this case you can use an Sstateless service to store the data of the exchange house that contains the reservation. And another Stateful service for each user (session) buy or sell dollars. What matters here is the home exchange will need to share resources between multiple users simultaneously, so you need to maintain the state of resources.

Here’s a bigger detail: http://www.devmedia.com.br/trabalhando-com-session-beans-stateless-e-stateful/28260

  • So Stateless is a Singleton ? there is no risk of a new instance?

  • There are differences. I recommend reading the documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html#gipim. If you doubt post here I try to explain better

  • right, but if there are differences I believe your example is not complete...

  • 1

    Stateless does not share resources, for each call the instance objects have their own state until the end of the method execution. Singleton is shared in all calls to the method, so it is instance objects can be shared between calls.

  • 3

    That mixes people. Stateless treats each request independently. Statefull has Session to identify the user and restore the status between each request. Singleton is another bix in another layer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.