1
votes

I have a EJB question.

Consider we have a stateful bean(a simple shop cart), and user gets its session using a simple desktop application, not user may continue its process using mobile(another ui module) or web, so the question is:

how would I merge the two stateful session when sessions are for one user?

I was thinking about keep all sessions, and once user attempts to continue its process by another module, system will find any exists session and merge it with the new one, but it needs to much code and may complex the system. is there anything belong to EJB itself to do so?

and another question is, what is the difference between stateless and singleton EJB?!

1

1 Answers

1
votes

how would I merge the two stateful session when sessions are for one user? is there anything belong to EJB itself to do so?

No, there is not such merge in JEE.

However, you could share the same stateful reference among different clients. When you get a reference to Stateful session bean using JNDI lookup or dependency injection, the Container makes a new Stateful bean instance, which is assigned with a unique object identity.

The ejb reference knows this unique identity, for this reason every request that you make using this reference will be processed in the same Stateful instance.

Therefore, different requests made by different clients through the same reference, will be processed in the same Stateful instance allowing to share the Stateful's state among clients.

The container is responsible to serialize concurrent requests so you don't have to worry about concurrency.

What you need to solve is how to make this ejb's reference available to all your clients.

and another question is, what is the difference between stateless and singleton EJB?!

Among other differences, the more relevant in the previous question's context are:

  • Stateless

    • one instance per request
    • has no conversational state
  • Singleton

    • one instance per application
    • has conversational state
    • the state is shared by all client
  • Stateful

    • one instance per client (reference)
    • has conversational state
    • the state is availabe only to his client