一段时间后EntityManager变成NULL(大约10m)
在我的 JavaEE 6 应用程序(JBoss 6.0.0.Final)中,我有一个像这样概述的类层次结构
@Stateful public class UserFacade
{
@Inject @MyRepository
private EntityManager em;
}
@Stateful public class UserBl
{
@EJB private UserFacade fUser;
}
public class MyServlet
{
@EJB private UserBl blUser;
}
servlet 在登录过程中使用,并且工作正常。但是,如果在大约 10 分钟不活动时间后从同一浏览器调用 servlet,则 UserBl
中的 EntityManager em
变为 NULL
(检查了此在使用之前明确)。
在其他应用程序(JBoss 5.1.0.GA)中,我遇到了类似的问题,并通过显式检查 NULL
并从上下文中查找 EntitiyManager
来解决它那种情况。
我想知道是否存在基本的设计缺陷或者我是否遗漏了某些内容。在这种情况下,@Stateless Bean 也是可能的。但根据我的理解,这也应该适用于 @Stateful Bean。
in my JavaEE 6 application (JBoss 6.0.0.Final) I have a class hierarchy outlined like this
@Stateful public class UserFacade
{
@Inject @MyRepository
private EntityManager em;
}
@Stateful public class UserBl
{
@EJB private UserFacade fUser;
}
public class MyServlet
{
@EJB private UserBl blUser;
}
The servlet is used during a login process and it works fine. But if the servlet is called from the same browser after an inactivity period of about 10 minutes, the EntityManager em
in UserBl
became NULL
(checked this explicitly before using it).
In a other application (JBoss 5.1.0.GA) I've had a similar issued and solved it by explicitly checking for NULL
and looking up the EntitiyManager
from the context in that case.
I'm wondering if there is a fundamental design flaw or if I missed something. In this scenario @Stateless
Beans are possible, too. But in my understanding this should also work with @Stateful
Beans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我记得,有状态 bean 的注入一直是有问题的(至少在 JavaEE 5 中)。这可能是钝化激活问题。
但这里实际上并不需要有状态 bean - EntityManager 是每个事务的(默认情况下;您可以将其设置为扩展,以允许它跨越多个请求),而无状态 bean 是正确的方法去。
As far as I recall, injection of stateful beans has been problematic (at least in JavaEE 5). It might be a passivation-activation issue.
But you don't really need stateful beans here - the
EntityManager
is per-transaction (by default; you can set it to extended which allows it span multiple requests) and a stateless bean is the proper way to go.