在有状态的EJB 3.1 bean中保持会话? [英] Hold session in stateful EJB 3.1 bean?

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

问题描述




  • Java EE 6

  • CDI

  • JSF 2

  • EJB 3.1

  • Spring Security



我为我的JSF页面提供了基于CDI的后备bean(@ViewScoped,@Named)。



我使用@Stateless EJB bean来完成实际工作。



我只需要很少的会话信息,如jSessionCookie(由容器管理),内部用户名和一些其他内部标识。现在,我不知道在哪里放置这个会话信息,以便我可以在JSF的后备bean中访问它,还可以将它提供给无状态的EJB?我应该使用@Stateful EJB会话bean,还是应该使用@SessionScoped和@Named来创建基于CDI的POJO?



有没有最佳实践?

解决方案

对于您的特定用例,有状态会话bean不会是一个不错的选择。请注意,与人们可能声称的有争议的是,有状态的会话bean肯定不是您通常应该避免的事情。然而,它们是针对高级用例的,例如在处理JPA的扩展持久性上下文时。



有状态会话bean在这里不工作的原因是它们不是自动与HTTP会话相关联,这似乎是您的首要关注点。您可以向其添加@SessionScoped注释,但是您也可以使用常规托管bean。您不会使用SFSB的任何特殊功能。



请参阅alo:





你可以注入你的statele具有会话范围CDI bean的ss EJB,但是您必须意识到,在同一应用程序中,您的EJB bean将依赖于HTTP会话(有时您希望避免的情况,例如,如果您的bean也必须从其他上下文调用)。


I'm working on a Java webapp trying to combine the following technologies:

  • Java EE 6
  • CDI
  • JSF 2
  • EJB 3.1
  • Spring Security

I provide CDI-based backing beans (@ViewScoped, @Named) for my JSF pages.

I use @Stateless EJB beans for the actual work to be done.

I only need few session information like jSessionCookie (managed by container), internal username and some other internal IDs. Now, I wonder where to put this session information so that I can access it in the backing beans for JSF, but also provide it to the stateless EJBs? Should I use a @Stateful EJB session bean or should I create CDI-based POJO with @SessionScoped and @Named?

Are there any best practices?

解决方案

For your particular use case, a stateful session bean would not be a good choice.

Do note that contrary to what people may claim, stateful session beans are surely not something you should generally avoid. However, they are for advanced use cases, for instance when dealing with JPA's extended persistence context.

The reason why stateful session beans would not work here, is that they are not automatically associated with the HTTP session, which seems to be your prime concern. You could add the @SessionScoped annotation to them, but then you could just as well use a regular managed bean. You would not use any of the particular features of a SFSB.

See alo:

You can inject your stateless EJBs with a session scoped CDI bean, but you have to realize that within the same application your EJB bean would be dependent on the HTTP session then (something you sometimes want to avoid, e.g. if your bean has to be called from other contexts as well).

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

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