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

1

6

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 ?

Ill answered 1/8, 2010 at 6:28 Comment(0)
A
15

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.

Aztec answered 1/8, 2010 at 23:19 Comment(1)
Can someone also confirm that by using SessionContext.getBusinessObject() we guarantee that @Asynchronous methods will execute in different threads ?Triliteral

© 2022 - 2024 — McMap. All rights reserved.