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

查看:143
本文介绍了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和网络的第二个问题应用程序当天,而这个主题通常不常见。

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


在这种情况下在web
申请中使用它们?

in which case to use them on a web application?

SFSB和Web应用程序的传统示例是购物车。但与此同时,您可以使用 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放在Session中(因为无状态
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:

  • Java: Tracking a user login session - Session EJBs vs HTTPSession
  • Correct usage of Stateful Beans with Servlets

如果要使用它们,首先需要查找SFSB以获取对一个新的远程实例的引用。然后,您需要在某个地方存储此引用,以便在请求之间重用它。这个某处通常是 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,远程引用(称为句柄)可以序列化,以便以后重用。然后可以存储,例如在数据库中,即使我从未见过它。我不知道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.


这是一个好习惯吗?

Is it a good practice?

正如我已经说过的那样,人们通常建议反对它,除非你确切知道为什么要使用它们而不是 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天全站免登陆