Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the return-value of SessionContext.getBusinessObject() different from 'this' keyword used in the bean?

Tags:

java

ejb-3.0

The SessionContext.getBusinessObject() is described in the docs as follows,

Obtain an object that can be used to invoke the current bean through the given business interface.

Parameters: businessInterface - One of the local business interfaces or remote business interfaces for this session bean.

Returns: The business object corresponding to the given business interface.

Can't I use the 'this' keyword in Java instead, to accomplish the same ? How are these different ?

like image 618
stratwine Avatar asked Aug 01 '10 06:08

stratwine


1 Answers

The motivation here is that most EJB implementations work on proxies. You wouldn't be too far off in thinking of it as old-school AOP. The business interface is implemented by the EJB container, quite often via a simple java.lang.reflect.Proxy, and this object is handed to everyone in the system who asks for the ejb via @EJB or JNDI lookup.

The proxy is hooked up to the container and all calls on it go directly to the container who will preform security checks, start/stop/suspend transactions, invoke interceptors, etc. etc. and then finally delegate the call to the bean instance -- and of course do any clean up required due to any exceptions thrown -- then finally hand the return value over through the proxy to the caller.

Calling this.foo() directly, or passing 'this' to a caller so they can make direct calls as well, will skip all of that and the container will be effectively cut out of the picture. The 'getBusinessObject(Class)' method allows the bean instance to essentially get a proxy to itself so it can invoke its own methods and make use of the container management services associated with it -- interceptors, transaction management, security enforcement, etc.

like image 124
David Blevins Avatar answered Nov 15 '22 07:11

David Blevins