JSF ManagedBean must implement Serializable or not ?

Posted: May 8, 2014 in J2EE, Java

Recently I was reading about serialization and I noticed that all the ManagedBean in my JSF project implements Serializable interface.

Normally, we use Serialization to send an object over the network or if the state of an object needs to be persisted to a flat file or a database.
But my managedBean was just a means to value bind the UI components. I digged deep into this topic and I think its useful to share my findings with you all.

We need not make every ManagedBean serializable. It depends upon the Scope the MBean. If you don’t want to serialize your MBean, make sure the scope is ‘request’.

Instead, if you are dealing with ‘session’ scope or ‘view’ scope (even view scope is using session scope). The session scope is backed by the Servlet’s HttpSession.
All session attributes are supposed to implement Serializable because the servlet container tries to persist session data to file system
to be able to survive heavy load and/or reviving sessions during server restart.

Sometimes you might have noticed that your post contructs in your MBean get executed on server restart. Why ?
Some containers will require you to explicitly disable session serialization (or else they will be generating exceptions).
Tomcat by default enables persisting session through server restart.

Advertisements

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