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