EJB3 + JSF2;像有状态的一样无状态

发布于 2024-09-17 04:00:12 字数 194 浏览 12 评论 0原文

我在 ejb 容器中有一个无状态会话 bean。如果我从 jsf2 表单调用它,它工作正常,但如果我再次回忆该表单,它会显示我之前插入的相同数据。即使我关闭并重新打开浏览器也会发生这种情况。我必须等待几分钟,直到表单显示空字段。 不会为每个请求重新创建无状态会话 Bean。它的行为就像一个有状态的。有什么问题吗?

应用服务器:Glassfish 3.0.1

I've a stateless session bean in an ejb container. If I invoke it from a jsf2 form it works fine, but if I recall the form again it shows me the same data I've inserted before. It happens even if I close and reopen the browser. I must wait several minutes until the form shows empty fields.
The stateless session bean is not recreated for each request. It behaves like a stateful one. What's the wrong?

Application server: Glassfish 3.0.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-09-24 04:27:19

无状态会话 bean 不是
为每个请求重新创建。

这不是一个正确的术语。不保证为每个请求创建新的无状态 bean 实例。

出于性能原因,容器将池化一些无状态 bean 实例。

池实例的数量取决于容器配置。

The stateless session bean is not
recreated for each request.

this is not a correct term. there is no guaranty to create new instance of stateless bean for each request.

container will pool some instances of stateless bean for performance reason.

the number of pooled instances depends on container configuration.

难得心□动 2024-09-24 04:22:42

我原来的问题是:EJB3 + Struts2。 Struts 为每个请求创建新的会话 bean,即使它是有状态的。

Struts 本身不执行任何操作,它只执行您告诉他执行的操作。我怀疑你只是误用了东西。显示一些代码来说明问题可能会有所帮助。

现在我发现 JSF2 为每个请求重用相同的会话 bean,即使它是无状态的。

一般来说,情况并非如此,您可以获得无状态会话 bean (SLSB) 的任何 实例。即使由于某些原因您在特定情况下获得了相同的实例(可能是因为 beans 池配置),在使用 SLSB 时这根本不应该是问题,您不应该关心你得到什么实例,当然不依赖于实例的状态(因为它们是无状态的)。

回到你的问题,我怀疑你滥用 SLSB 并期待不正确的事情:

  • 不要期望在调用之间获得新初始化的实例,这不是无状态的含义。
    • 实际上,不要对您将获得的实例抱有任何期望。
  • 不要依赖 SLSB 实例的状态(它们是无状态的,您不应该依赖状态)。
    • 不要依赖远程调用之间的实例变量。
    • 实际上,避免使用实例变量,可能不需要它们。

相关问题

My original problem was: EJB3 + Struts2. Struts create new session bean for each request even if it is stateful.

Struts doesn't do anything by itself, it only does what you tell him to do. And I suspect that you're just misusing things. Showing some code to illustrate the problem might help.

Now I discover that JSF2 reuse the same session bean for each request even if it is stateless.

This is not true in general, you can get any instance of a stateless session bean (SLSB). And even if for some reasons you get the same instance in your particular situation (maybe because of the beans pool configuration), this shouldn't be an issue at all when using SLSB, you should not care of what instance you get and certainly not rely on the state of instances (since they're stateless).

Back to your question, I suspect that you're misusing SLSB and expecting things that are not true:

  • Don't expect to get newly initialized instances between calls, that's not what stateless means.
    • Actually, don't expect anything about the instance you'll get.
  • Don't rely on the state of a SLSB instance (they are stateless, you're not supposed to rely on state).
    • Don't rely on instance variable between remote calls.
    • Actually, avoid using instance variables, there is probably no need for them.

Related questions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文