user107986
user107986

Reputation: 1541

Why EJB need to implement serializable interface?

Is there any reason why EJB classes need to be serializable? I've heard it has something to do with the fact that RMI is being used under the hood. I know how RMI (remote method invocation) works, there's a remote object registered on the server side and only the stub of remote object is sent to the client, not the whole object.

So in RMI applications the methods of remote object need to take arguments and return values that are serializable, as they are being sent over the network, but not the remote object itself.

Upvotes: 7

Views: 8609

Answers (1)

Steve C
Steve C

Reputation: 19435

The reason that the old J2EE style EJBS that implement javax.ejb.EntityBean, javax.ejb.SessionBean and javax.ejb.MessageDrivenBean should be serializable is historical. The original javax.ejb.EnterpriseBean interface that these extend happens to itself extend java.io.Serializable. In the very early EJB days it was thought that this was needed to facilitate moving beans between JVMs.

All practical reasons for EJBs to actually implement Serializable disappeared by the time that the EJB 2.0 specification was published.

The introduction of EJB 3 removed the requirement for any of these interfaces (and subsequently java.io.Serializable) to be implemented.

No EJB specification since EJB 2.0 has specified that EJBs must be serializable. This has only been implicit because of the interface inheritance.

The implicit requirement disappears altogether with EJB 3.x.

Upvotes: 13

Related Questions