Web 应用程序中的有状态 EJB? [英] Stateful EJBs in web application?

查看:21
本文介绍了Web 应用程序中的有状态 EJB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未使用过有状态 EJB.我知道有状态 EJB 对 Java 客户端很有用.

I never used stateful EJBs. I understand that a stateful EJB can be useful with a java client.

但我想知道:在哪种情况下在 Web 应用程序上使用它们?如何?我们是否应该将这些有状态的 bean 放在 Session 中(因为无状态的 http)?

But i wonder: in which case to use them on a web application? And how? Should we put these stateful beans in Session (because of stateless http)?

这是一个好习惯吗?(无需过多讨论有状态与无状态)

Is it a good practice? (without debating too much about stateful vs stateless)

推荐答案

很有趣,这是当天关于 SFSB 和 Web 应用程序的第二个问题,而这个主题通常不那么常见.

Funny enough, it's a 2nd question on SFSB and web app for the day, while this topic is usually not that common.

在这种情况下在网络上使用它们申请?

in which case to use them on a web application?

SFSB 和网络应用的传统示例是购物车.但同时,您可以对 HttpSession 做同样的事情.

The traditional example of SFSB and web app is the shopping cart. But at the same time, you can do the same with the HttpSession.

理想情况下,如果状态与业务逻辑相关而不是表示逻辑,则它应该进入 SFSB.但在实践中,人们通常反对 SFSB(因为它引入了复杂性),除非他们提供了使用 HttpSession 无法轻松实现的东西.大多数情况下,您可以调整设计以将信息存储在 HttpSession 或数据库中并传递它,而无需 SFSB.但这归根结底是设计纯度问题.

Ideally, if the state relates to the business logic and not the presentation logic, it should go in a SFSB. But in practice, people usually advocates against SFSB (because of the complexity it introduces) unless they provide something you can not do easily with the HttpSession. Most of the time, you can tweak the design to store the information in the HttpSession or the database and pass it around, without the need to have SFSB. But it's ultimately a question of design purity.

怎么办?我们应该把这些有状态的会话中的bean(因为无状态http)?

And how? Should we put these stateful beans in Session (because of stateless http)?

EJB 模型是比HttpSession 更丰富的模型,因为EJB 是事务性组件,对于SFSB 的钝化和激活有显式的回调.这伴随着关于如何正确使用 SFSB 的增加的复杂性,特别是 (1) 异常处理和 (2) 并发以及 (2) SFSB 的移除和超时.有关更多详细信息,请在此处查看我的回答:

The EJB model is a richer model than the HttpSession, because EJB are transactional components, and there are explicit callbacks for the passivation and activation of SFSB. This comes with an increased complexity about how to use SFSB correctly, notably (1) exception handling and (2) concurrency and (2) removal and time-out of SFSB. See my answers here for more details:

如果您想使用它们,您首先需要查找 SFSB 以获取对一个新远程实例的引用.然后,您需要将此引用存储在某处,以便跨请求重用它.这个somewhere通常是HttpSession,这意味着即使你使用SFSB,你也不能完全摆脱它.

If you want to use them, you will need first to look up the SFSB to get a reference to one fresh remote instance. Then you will need to store this reference somewhere in a way to reuse it across requests. This somewhere is usually the HttpSession, which means that even if you use SFSB, you can't get rid of it completely.

使用EJB2,远程引用——称为句柄——可以被序列化以便以后重用.例如,即使我从未见过,也可以将 if 存储在数据库中.我不知道 EJB3 是否还可以.

With EJB2, the remote reference -- called a handle -- could be serialized to be reused later. It was then possible to store if for instance in database, even though I've never seen that. I don't know if it's still possible with EJB3.

这是一个好的做法吗?

正如我已经说过的,人们通常反对它,除非您确切地知道为什么要使用它们而不是 HttpSession 并且只有当您很好地掌握EJB 模型.(例如,如果业务服务可通过 Web 前端和桌面客户端访问,则 SFSB 可能是合理的)许多其他框架具有与 SFSB 类似的功能,人们仍然设法创建出色的应用程序.

As I said already, people usually advice against it unless you know exactly why you would use them rather than the HttpSession and only if you have a good command of the EJB model. (SFSB could be justified for instance if the business service is accessible through a web front-end and a desktop client) Lots of other frameworks don't have something similar as SFSB and people still manage to create great apps with them.

PS:我在网络应用程序中使用过 SFSB,它确实比 HttpSession 使用起来更棘手,但它最终奏效了.

PS: I've used SFSB in a web app, and it can indeed be a trickier to use than the HttpSession, but it ultimately worked.

这篇关于Web 应用程序中的有状态 EJB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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