有状态EJB与会话属性 [英] Stateful EJB vs. Session Attribute

查看:128
本文介绍了有状态EJB与会话属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用状态EJB时,如果客户端没有活动会话,服务器如何知道EJB与谁相关联?我以前使用过无状态的EJB,但现在正在尝试学习如何使用状态EJB。我正在考虑将我的购物车作为有状态EJB,而不是在用户会话中添加一个POJO Cart作为属性。但是,由于状态EJB没有在HttpSession中显式添加为属性,服务器如何将客户端与状态EJB相关联?

When using a Stateful EJB, how does the server know who the EJB is associated with if the client does not have an active session? I have used stateless EJB's before, but am now trying to learn how to use stateful EJB's. I was thinking of implementing my shopping cart as a stateful EJB, instead of having a POJO Cart that I add as an attribute in the user's session. But since a Stateful EJB isn't explicitly added as an attribute in the HttpSession, how does the server associate the client with the stateful EJB?

推荐答案

EJB在技术上不需要可以访问客户端的JSESSION_ID,因为像任何基本的pojo一样,只要主叫客户端存活就可以使用它。一旦主叫客户端被销毁或以其他方式放弃了对SFSB的控制,该bean就可能被钝化或销毁(因此忘记了会话)。

The EJB doesn't technically need to have access to the JSESSION_ID of the client because, like any basic pojo, it's alive and usable, as long as the calling client is alive. Once the calling client is destroyed or otherwise relinquishes control of the SFSB, the bean is liable to be passivated or destroyed (and hence "forget" the conversation)

Oracle JavaEE-6教程


在客户端/ bean会话期间保留状态。如果
客户端删除该bean,会话结束,状态
消失
。然而,这种状态的暂时性质不是一个问题,因为当客户端和bean
之间的对话结束时,不需要保留状态

The state is retained for the duration of the client/bean session. If the client removes the bean, the session ends and the state disappears. This transient nature of the state is not a problem, however, because when the conversation between the client and the bean ends, there is no need to retain the state

想到它是一样的方式你拥有一个常规的java对象的一个​​关系:一旦你已经将组合对象设置为null,你基本上结束了与该对象的对话。这同样适用于(排序)。客户端不需要将具体的会话信息传递给EJB。 EJB的正常生命周期和注释(特别是 @Remove )照顾一切。

Think of it the same way you get hold of a regular java object in a has-a relationship: once you've set the composed object to null, you've basically ended your conversation with that object. The same applies here (sort of). There's no need for the client to pass specific session information to the EJB. The EJB's normal lifecycle and annotations (specifically @Remove) take care care of everything else.

谨慎的一句话关于SFSB:它们是重量级的,它们比SLSB持续时间更长。不要使用它们,除非你真的需要一个全面的EJB的陷阱。在许多情况下,一个简单的HttpSession和SLSB就足够了。

A word of caution about SFSBs: They are heavyweight and they last longer than SLSBs. Don't use them unless you really need a full-scale EJB's trappings. In many cases, a plain HttpSession and an SLSB would suffice.

进一步阅读

  • Lifecycle of an EJB

这篇关于有状态EJB与会话属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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