Java EE 6: how to call a stateful session bean from a stateless session bean?
I have a stateful session bean (SFSB), which acts as an authentication module In SFSB, I store the currently logged in user In addition, I have some appearance (stateless session bean (slsb)) to handle the JPA / SQL content of my entity In order to check the access rights of current users, I try to call SFSB. from SLSB. However, when called from slsb, the current user field is always "null" When calling SFSB directly, set the current user field correctly... In order to call, I use @ EJB annotation
What could be the problem with any idea? Is this some kind of context? Can SFSB normally be called from slsb to preserve its state?
Thank you in advance!
Solution
You should not call a stateful session bean from a stateless session bean
Here are some readings: jee6 tutorial – session beans
Stateless beans know nothing about your session Whenever you call, it is stateless It then calls the stateful session bean Not surprisingly, it has no context related to the client session state because it is called from a stateless object
I don't know if it will work, but you might try to get the context by JNDI lookup using @ EJB notation instead of di Such things in stateless EJBs may work You may have to play with it. I can't guarantee anything It should get the context in which the client invokes the stateless EJB The client needs to have a session context / scope or forget it
@Resource SessionContext sessionContext; MyStatefulBean msb = (MyStatefulBean)sessionContext.lookup("ejb/MyStatefulBean"); msb.doSomething(fubar);
It is best to call a stateful session bean from a client with session scope or another stateful EJB Stateless and stateful people have different reasons