jump to navigation

Stateful Session Beans – Best practices January 25, 2008

Posted by Coolguy in J2EE, Java.

Stateful Session EJB is a server-side object designed to hold data on behalf of a particular client.

SFSBs should be used to store session-oriented data. They hold the data on behalf of the client. Session-oriented data is used to track a multi-request sequence, ordering, or any associated data that is part of a sequence.

SFSBs, however, have a limited lifespan and are not intended to survive server crashes. Although SFSBs are allowed to access a database and load data into memory, but the act of caching persistent data lies within the responsibilities of entity beans.

The EJB specification suggests that a shopping cart for an e-commerce system could be implemented with SFSBs. This is perfectly acceptable if that shopping cart is only intended to be an in-memory implementation that does not need to survive a server crash. In reality, however, most implementations of shopping carts are required to survive server crashes; the data contained within the shopping cart needs to be persistent and transactional — with an in-memory data cache. i:e Using entity beans.

When to use them ? 

In very few J2EE systems !!

Systems that have JSP/servlet front-ends should use HttpSession objects to store session-oriented state. Using both  HttpSession and SFSB is simply duplicating the effort.

For systems that do not have a servlet/JSP front-end, SFSBs should be used to track session-oriented state similar to the way a web-based system would use an HttpSession object.

Use SFSB’s along with  HttpSession when:

Your app server doesn’t offer caching of HttpSession objects. This will mean that you may have to have to limit on number of sessions.  Using SFSB’s in this scenario will increase scalability as a  container can activate/passivate SFSB’s as needed.



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: