Java EE 6:如何从无状态会话Bean调用有状态会话Bean? [英] Java EE 6: How to call Stateful Session Bean from Stateless Session Bean?

查看:204
本文介绍了Java EE 6:如何从无状态会话Bean调用有状态会话Bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有状态会话Bean(SFSB)作为身份验证模块。在SFSB中,我存储当前登录的用户。此外,我还有一些外观(它是无状态会话Bean(SLSB)),用于处理我的实体的JPA / SQL内容。为了检查当前用户的访问权限,我尝试从SLSB调用SFSB。但当从SLSB调用时,当前用户字段始终为null。当直接调用SFSB时,当前用户字段设置正确...对于调用我使用@EJB注释。



任何想法可能是什么问题?这是一个上下文问题吗?通常可以从SLSB调用SFSB来保存它的状态?



非常感谢提前!

解决方案

您不应该从无状态会话bean调用有状态会话bean。



以下是一些阅读: JEE6教程 - 会话Bean



无状态bean不知道你的会话。任何时候你都称之为无国籍的。然后它调用一个有状态会话bean。没有惊喜,它没有​​与客户端会话的状态相关的任何上下文,因为它是从无状态对象调用的。



我不知道是否可以正常工作,但是您可以尝试通过使用@EJB表示法执行JNDI查找而不是DI来获取上下文。在无状态ejb中这样的东西可能会起作用。你可能必须玩它,我不能保证任何东西。它应该得到客户端调用无状态ejb的上下文。客户端将需要具有会话上下文/范围或忘记它。

  @Resource SessionContext sessionContext; 

MyStatefulBean msb =(MyStatefulBean)sessionContext.lookup(ejb / MyStatefulBean);
msb.doSomething(fubar);

最好从具有会话范围或另一状态的客户端调用有状态会话bean EJB。无国籍和有状态有不同的理由。


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 Stateless Session Beans (SLSB)) that handles the JPA/SQL stuff for my entities. In order to check the access permissions of the current user, I try to call the SFSB out of the SLSB. But the current user field is always "null" when called from SLSB. When calling the SFSB directly, the current user field is set correctly... For calling I use the @EJB annotation.

Any ideas what the problem might be? Is that somehow a context problem? Is it generally possible to call a SFSB from SLSB preserving it's state?

Many thanks in advance!

解决方案

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.

这篇关于Java EE 6:如何从无状态会话Bean调用有状态会话Bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆