1

I am a little confused as to the usage of stateful session beans (SFSB) versus stateless session beans (SLSB).

I understand that SFSB's maintain state with a client. This helped: When to use Stateful session bean over Stateless session bean?

The example provided here and in many other places is the shopping cart for SFSB.

My question is why does one need a SFSB if the application is backed by a database? 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??

"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. This would be more like checkout (forms between pages??) where nothing would be saved in the DB until the last page. 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 :)

Community
  • 1
  • 1
ovg
  • 1,486
  • 1
  • 18
  • 30

2 Answers2

0

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. So the only way to maintain its state to the server, is to keep the reference to the Stateful Proxy object. In many Oracle/Sun document a Stateful is referenced as an extension of the client to the server (or the opposite).

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...

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. There are pros and cons for choosing on or the other approach.

Sometimes I found example of Stateful bean being kept in http session. I found this approach quite strange. Honestly, I haven't yet found a Stateful being stored in session in a production release.

Leonardo
  • 9,607
  • 17
  • 49
  • 89
0

You use SFSB-s when the data not sure to be persist. 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. 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.

The Bitman
  • 1,279
  • 1
  • 11
  • 25