问题
I want to handle a login scenario as follows:
- Client connects to a Stateless Java Bean (SLJB) and tries to login;
- If login succeeds, the SLJB returns to the user a Stateful Java Bean (SFJB), so that the client can continue using the application.
I am currently doing the second step as:
return new StatefulBean(some params);
Is this the right way to do it? It does not seem to me as I get the exception:
Class org.eclipse.persistence.internal.jpa.EntityManagerImpl is not Serializable
when running my application, and I think it is related to the described method.
What would be the correct way to return a reference to the SFJB from the SLJB to the client?
回答1:
First of all, this is completely wrong:
new StatefulBean(some params)
EJB container is responsible for creating and destroying instances of beans, you should never create them manually.
In your scenario I would reverse the flow: the client connects to the stateful bean which might stateless session bean as a helper. No need to pass beans around, client always uses the same bean.
回答2:
As Tomasz mentions, you probably need to rethink your flow.
That said, you can get a hold of a new stateful instance by doing a JNDI lookup, using the portable JNDI name that us assigned to each bean at startup.
来源:https://stackoverflow.com/questions/10355765/returning-a-stateful-java-bean-from-a-stateless-bean