4

Stateful session beans are often illustrated by implementing a shopping cart. Coming from outside Java EE, my inclination would be to handle this kind of state with a persistent model entity: a ShoppingCart object with Products and quantities. This way, my state is being maintained by the database along with all my other state rather than by the application server.

What are the technical advantages to stateful session bean design over "ordinary" persistence? Are shopping carts in Java EE-based web applications indeed usually written with SFSBs, or as in other systems just by more elaborate domain modeling?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77

1 Answers1

1

There are several ways to implement a shopping cart. The main difference between SFSB and DB persistence is, well, the persistence :)

A Stateful session bean will only "persist" the data during the session time. So if the user session becomes inactive (for exemple after 30 minutes of inactivity), the shopping cart will be reset

With the database persistence, the shopping cart will be stored permenantly, so if a user have a filled shopping cart, then don't visit the webshop during 6 months, and visit it again, the cart will still be filled

I think usually the first solution is used, as involving a non in-memory database is not a good idea to store volatile data. There will be a lot of hard drive I/O overhead for data that don't really need long-term persistence

cporte
  • 2,191
  • 3
  • 20
  • 30
  • I don't think the amount of disk I/O is that easy to measure. After all, SFSBs can be passivated whenever the server wants and that will imply disk I/O as well. – Daniel Lyons Jul 08 '12 at 04:33
  • Good point ! I forgot that. But I think it depends of the session time, number of sessions, SFSB cache size, etc. If we consider a long session time and many users, there will be potentially many passivate beans. But I think that case is an issue, and it's better to reduce the session time or change the cache size, as too many passivation is never a good news. With a database, you always have I/O, whatever the user do. Every "add to cart", "remove from cart", "change amount", etc, will lead to I/Os. I hope there will be more "add to cart" clic then bean passivation in your case :) – cporte Jul 08 '12 at 08:55
  • On the other hand, sites like Amazon do keep your cart in long-term storage of some kind. I dunno. I don't feel like I have a sense for short-to-medium-term caching with a lifespan different from the HTTP session. – Daniel Lyons Jul 08 '12 at 19:17