简体   繁体   English

sessionscoped托管bean与有状态ejb

[英]sessionscoped managed bean vs stateful ejb

If I have a @ManagedBean that's @SessionScoped , why would I use a @Stateful EJB? 如果我的@ManagedBean@SessionScoped ,为什么我会使用@Stateful EJB? I used it before for shopping carts and maintaining a conversational state, but since a managed bean will be kept during the user session I can store state there, then call SLSB for bussiness logic. 我之前用过购物车并保持会话状态,但由于托管bean将在用户会话期间保存,我可以在那里存储状态,然后调用SLSB以获取业务逻辑。 Is that correct? 那是对的吗? If it is, then stateful ejbs will be left for more specific applications such as when you need transactions etc? 如果是,那么有状态的ejbs将留给更具体的应用程序,例如何时需要交易等?

Very often stateless session beans can be used for a lot of business problems. 通常,无状态会话bean可用于许多业务问题。

Stateful does not necessarily means only a remote server keeps state, although this is certainly one of the options. 有状态并不一定意味着只有远程服务器保持状态,尽管这肯定是其中一个选项。 A remote Swing client could first send a bunch of data to a stateful session bean, hold on to the stub and then subsequently send some commands that operate on this data. 远程Swing客户端可以首先将一堆数据发送到有状态会话bean,保留存根,然后发送一些操作此数据的命令。 This saves the client from having to send the same (large amount of) data each and every time. 这使客户端不必每次都发送相同(大量)的数据。

In the remote use case, it indeed somewhat mirrors the usage of the HTTP session when web clients (browsers) are used. 在远程用例中,当使用Web客户端(浏览器)时,它确实在某种程度上反映了HTTP会话的使用。 The major difference is that the session is per bean here, while with the HTTP session, the session is a scope shared by many beans. 主要区别在于会话是每个bean,而在HTTP会话中,会话是许多bean共享的范围。 Since the HTTP session is based on cookies, and cookies are global for a domain for the entire browser, the HTTP session can not directly support multiple sessions from the same client (eg per tab or per window). 由于HTTP会话基于cookie,并且cookie对于整个浏览器的域是全局的,因此HTTP会话不能直接支持来自同一客户端的多个会话(例如,每个选项卡或每个窗口)。 This is trivial with stateful session beans. 这对于有状态会话bean来说是微不足道的。

However... 然而...

Remote Swing clients talking to remote EJBs are not that common. 与远程EJB通信的远程Swing客户端并不常见。

In the context you described in your question, you will typically use local EJBs and you will store most state in the HTTP session (be careful with sharing!) and these days in the view scope or conversation scope. 在您在问题中描述的上下文中,您通常会使用本地EJB,并且您将在HTTP会话中存储大多数状态(小心共享!)以及这些日期在视图范围或会话范围中。

So, finally, when to use stateful session beans in this scenario? 那么,最后,何时在这种情况下使用有状态会话bean?

One important use case is the extended persistence context in JPA . 一个重要的用例是JPAextended persistence context Normally with a transaction scoped entity manager, when an entity crosses the transactional boundary of an EJB method call it will be detached. 通常使用事务范围的实体管理器,当实体跨越EJB方法调用的事务边界时,它将被分离。 If you want to (optimistically) lock an entity between user interactions, this is undesirable. 如果您希望(乐观地)在用户交互之间锁定实体,这是不可取的。 You'll lose the lock. 你将失去锁定。

With an extended persistence context, the entity remains attached and the locks valid when you return from a call to the stateful session bean. 使用扩展的持久性上下文,当您从调用有状态会话bean返回时,实体保持连接并且锁有效。 This is very useful for preview functionality to assure that nobody else has made any changes to the entity when you okay after the preview. 这对预览功能非常有用,以确保在预览后没有其他人对实体进行任何更改。 Or indeed for a shopping cart where you want to assure that for some time the item can't be sold to anyone else while in the cart. 或者对于购物车而言,您确实要确保该物品在购物车中不能出售给其他任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM