Returning a Stateful Java Bean from a Stateless Bean?

女生的网名这么多〃 提交于 2019-12-11 07:30:00

问题


I want to handle a login scenario as follows:

  1. Client connects to a Stateless Java Bean (SLJB) and tries to login;
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!