Do you need isIdentical() when there’s equals()?

Local clients still need isIdentical()

Remember for a Remote client, the only local comparison you do is on two stub objects, using equals(). This doesn’t work when you want to compare something back on the server, in this case the two EJB object references. That’s what isIdentical() is for. But local clients have the real thing! They have the real reference to the EJB object, so they can use equals(), to see if two EJB object (local) references are meaningfully equivalent. But... that’s still not what you want. There is no guarantee in the spec, for the results you’ll get with .equals()! So while it seems like you could just use .equals() rather than isIdentical() with a local client, the spec does not guarantee that the results will be the same. Bottom line: if you want to know if two EJB object references are referencing the same session object, you have to use isIdentical() even when the EJB object is local.

REMOTE client view

image with no caption

A Remote client has to make a Remote method call, isIdentical(), to get meaningful comparison info about an EJB object. Calling equals() on a stub won’t tell you anything useful.

LOCAL client view

image with no caption

Calling equals() and isIdentical() on a local EJB object reference are not guaranteed to give you the same result. You ...

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.