Java EE 6: How to call Stateful Session Bean from Stateless Session Bean?

前端 未结 3 1754
星月不相逢
星月不相逢 2020-12-31 17:45

I have a Stateful Session Bean (SFSB) which acts as authentication module. In the SFSB I store the current user that is logged in. Moreover I have some facades (which are St

3条回答
  •  青春惊慌失措
    2020-12-31 18:23

    You shouldn't call a stateful session bean from a stateless session bean.

    Here is some reading: JEE6 Tutorial - Session Beans

    Stateless beans don't know anything about your session. Any time you call it, it is stateless. Then it calls a stateful session bean. No surprise it doesn't have any context relating to the state of the client's session because it is called from stateless object.

    I don't know if it will work, but you possibly could try to get the context by doing a JNDI lookup instead of DI using the @EJB notation. Something like this in the stateless ejb might work. You'll probably have to play with it and I can't guarantee anything. It should get the context of the client calling the stateless ejb. The client will need to have session context/scope or forget it.

    @Resource SessionContext sessionContext;
    
    MyStatefulBean msb = (MyStatefulBean)sessionContext.lookup("ejb/MyStatefulBean");
    msb.doSomething(fubar);
    

    It is better to call the stateful session bean from a client that has a session scope or from another stateful ejb. Stateless and stateful have different reasons for being.

提交回复
热议问题