Prefer Local Interfaces
An EJB component can have remote and local interfaces. Clients not located in the same
application server instance as the bean (remote clients) use the remote interface to access the
bean. Calls to the remote interface require marshalling arguments, transportation of the
marshalled data over the network, un-marshaling the arguments, and dispatch at the receiving
end. Thus, using the remote interface entails signicant overhead.
If an EJB component has a local interface, then local clients in the same application server
instance can use it instead of the remote interface. Using the local interface is more ecient,
since it does not require argument marshalling, transportation, and un-marshalling.
If a bean is to be used only by local clients then it makes sense to provide only the local interface.
If, on the other hand, the bean is to be location-independent, then you should provide both the
remote and local interfaces so that remote clients use the remote interface and local clients can
use the local interface for eciency.
Using Pass-By-Reference Semantics
By default, the Enterprise Server uses pass-by-value semantics for calling the remote interface of
a bean, even if it is co-located. This can be expensive, since clients using pass-by-value
semantics must copy arguments before passing them to the EJB component.
However, local clients can use pass-by-reference semantics and thus the local and remote
interfaces can share the passed objects. But this means that the argument objects must be
implemented properly, so that they are shareable. In general, it is more ecient to use
pass-by-reference semantics when possible.
Using the remote and local interfaces appropriately means that clients can access EJB
components eciently. That is, local clients use the local interface with pass-by-reference
semantics, while remote clients use the remote interface with pass-by-value semantics.
However, in some instances it might not be possible to use the local interface, for example
when:
■
The application predates the EJB 2.0 specication and was written without any local
interfaces.
■
There are bean-to-bean calls and the client beans are written without making any
co-location assumptions about the called beans.
For these cases, the Enterprise Server provides a pass-by-reference option that clients can use to
pass arguments by reference to the remote interface of a co-located EJB component.
You can specify the pass-by-reference option for an entire application or a single EJB
component. When specied at the application level, all beans in the application use
pass-by-reference semantics when passing arguments to their remote interfaces. When
specied at the bean level, all calls to the remote interface of the bean use pass-by-reference
EJB PerformanceTuning
Chapter2 • TuningYourApplication 37