Java EE 6: How to invoke a Bean session from a Bean Idle session?

I have a Bean State Session (SFSB) that acts as an authentication module. In SFSB, I store the current user who is logged in. Moreover, I have some facades (which relate to a stand-alone Beans (SLSB) session) that handles JPA / SQL stuff for my objects. To check the permissions of the current user, I am trying to call SFSB from SLSB. But the current user field is always "null" when called from SLSB. When directly calling SFSB, the current user field is set correctly ... For the call, I use the @EJB annotation.

Any ideas what might be the problem? Is this somehow a context issue? Is it even possible to call an SFSB from an SLSB while maintaining its state?

Thank you very much in advance!

+6
source share
3 answers

You should not call a session with a bean state from a session without a bean state.

Here are some examples: JEE6 Tutorial - Session Beans

Stateless beans knows nothing about your session. Every time you call him, he is stateless. It then invokes a session with a bean state. Not surprisingly, it does not have context associated with the state of the client session, because it is called from an object without state.

I don't know if this will work, but you can try to get context by doing a JNDI lookup instead of a DI using the @EJB note. Maybe something like this in stateless ejb. You will probably have to play with this, and I can’t guarantee anything. It should receive the context of the client calling stateless e-mail. The client will need to have a context / session area or forget about it.

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

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

+8
source

You should not introduce stateless EJBs in stateless EJBs. This can have very unpredictable consequences, as the state-run EJB starts when injected and managed using a bean. In the worst case, stateless EJBs can be reused by the application server for different users, which will then access the same EJB state. In your case, the user will be identified as another user.

Most likely, you want to associate the EJB with the state with the current HTTP session, which is not performed automatically, as many people believe. For more information, read the section titled EJB 3 Is Not Contextual here: Contexts and Dependence on Injection Articles

To associate an EJB with state in a session, you need to enter an EJB with state in a CDI bean with session coverage that can be freely entered into a stand-alone bean - in fact, only a stub is entered and the bean session area (along with the EJB with state) is created for each new session.

Perhaps even a better approach is to extract the stateful bean interface and use the CDI manufacturer to create a realistic implementation of the sateful bean with the session. That way, you can also deal with this case where a standalone EJB is automatically removed from an exception in an EJB. In this case, you may want to recreate the EJB during the same session.

+4
source

If you enter a session with a state of bean inside a stateless bean using look-up also does not work, because a new instance will be created for the stateful bean, so it will not contain any values, such as registered user data, etc.

0
source

Source: https://habr.com/ru/post/920243/


All Articles