简体   繁体   English

使用DB时EJB有状态与无状态会话bean

[英]EJB stateful vs stateless session beans when using a DB

I am a little confused as to the usage of stateful session beans (SFSB) versus stateless session beans (SLSB). 关于有状态会话bean(SFSB)与无状态会话bean(SLSB)的使用,我有点困惑。

I understand that SFSB's maintain state with a client. 我知道SFSB与客户保持联系。 This helped: When to use Stateful session bean over Stateless session bean? 这有助于: 什么时候使用有状态会话bean而不是无状态会话bean?

The example provided here and in many other places is the shopping cart for SFSB. 这里和许多其他地方提供的示例是SFSB的购物车。

My question is why does one need a SFSB if the application is backed by a database? 我的问题是, 如果应用程序由数据库支持,为什么需要SFSB? Would a shopping cart typically update the database every time there is a change to it? 购物车通常会在每次更改数据库时更新数据库吗? or does the state reside in the SFSB until it is not needed (and then dumped to the DB) like some sort of cache?? 或者状态是否驻留在SFSB中,直到它不需要(然后转储到数据库),就像某种缓存一样?

"If a task needs a series of method calls (more than one) and you need to keep previous results to use them in next call then SFSB can be used" - source . “如果任务需要一系列方法调用(多个),并且您需要保留以前的结果以在下次调用中使用它们,那么可以使用SFSB” - source This would be more like checkout (forms between pages??) where nothing would be saved in the DB until the last page. 这更像是checkout(页面之间的形式??),直到最后一页才会在DB中保存任何内容。 But for a shopping cart I would imagine writing to the DB whenever the user adds something to their cart? 但是对于购物车,我想在用户向购物车添加东西时写入数据库?

Or am I missing the point of a SFSB :) 或者我错过了SFSB的观点:)

I have spent many hours in the past trying to find with no success some example other than the (obvious) Shopping Cart. 我过去花了很多时间试图找到除了(明显的)购物车之外的一些例子而没有成功。

In my opinion, the Stateful bean belong to the JSE realm only, where the client does not have anything like HTTPSession. 在我看来,有状态bean只属于JSE领域,客户端没有像HTTPSession这样的东西。 So the only way to maintain its state to the server, is to keep the reference to the Stateful Proxy object. 因此,将状态维护到服务器的唯一方法是保持对Stateful Proxy对象的引用。 In many Oracle/Sun document a Stateful is referenced as an extension of the client to the server (or the opposite). 在许多Oracle / Sun文档中,Stateful被引用为客户端到服务器的扩展(或相反)。

In the past I have infact written some little JSE Main class backed by a Stateful bean for quick administration purposes: closing/modifyng account, monitoring etc... 在过去,我实际上写了一些由有状态bean支持的小JSE Main类,用于快速管理:关闭/修改帐户,监控等...

For the rest of your question: 对于你的其余问题:

When we talk about JEE realm (web app mainly), you have infact few options to keep client state: HTTPSession with all the basket in memory, or a reference to the basket and a reference to its ID maybe in session and round trip from session to the DB for storing objects. 当我们谈论JEE领域(主要是Web应用程序)时,你实际上很少有选择来保持客户端状态:HTTPSession包含内存中的所有篮子,或者对篮子的引用以及对其ID的引用可能在会话和往返会话中到DB用于存储对象。 There are pros and cons for choosing on or the other approach. 选择或其他方法有利有弊。

Sometimes I found example of Stateful bean being kept in http session. 有时我发现有状态bean的例子保存在http会话中。 I found this approach quite strange. 我发现这种方法很奇怪。 Honestly, I haven't yet found a Stateful being stored in session in a production release. 老实说,我还没有在生产版本中发现有状态存储在会话中。

You use SFSB-s when the data not sure to be persist. 当数据不确定持久时,您使用SFSB-s。 The user just put some things into the shopping cart but never buy them. 用户只是将一些东西放入购物车但从未购买过。 In this case store this data in a DB is not a good idea. 在这种情况下,将此数据存储在DB中并不是一个好主意。 Store the data really needed and not trash in the DB. 存储真正需要的数据而不是数据库中的垃圾。 The data related to things that don't get bought/handled in a predefined period of time are transient, you should store them in a transient (session scoped/expire date constrained) manner. 与在预定义的时间段内未被购买/处理的事物相关的数据是暂时的,您应该将它们存储在瞬态(会话范围/过期日期约束)方式中。 When the user checkout the contents of her/him shopping cart the data get business sense and should take place in the DB. 当用户签出她/他购物车的内容时,数据具有商业意义并且应该在DB中进行。

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

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