1 Reply Latest reply: Dec 21, 2010 8:48 AM by 810311 RSS

    Call Enterprise Bean (or Database) from private Method in Session-Bean

    810311
      Hi Everybody,

      I've a question regarding the possibility to call an dependency injected EJB in an private method of a session bean.
      Imagine the following.
      @Stateless
      public class SomeBean implements SomeLocal{
         @EJB
         private AnotherLocal anotherBean;
         
         /** Will be called from a web-app via delegate layer */
         @TransactionAttribute(TransactionAttribute.RequiresNew)
         public void someBusisnessMethod(){
             String something = this.getSomeThing();
             //Do more
         }
      
         private String getSomeThing(){
            return anotherBean.aMethodWhichCallsTheEntityManager();
         }
      }
      I've to refactor code with uses such Call-Hierachy and I want to know whether this is a correct way? Somebody told me that such stuff should not be made, and I quess he told me an explanation, why not to do such stuff, but unfortunally I've forgotten that. Do someone have a suggestion why not to do this? Could it blow the application to hell? Is there any difference to the following code (The way I would have done it)?
      @Stateless
      public class SomeBean implements SomeLocal{
         @EJB
         private AnotherLocal anotherBean;
      
          @Resource
          private SessionContext sessionContext;
         
         /** Will be called from a web-app via delegate layer */
         @TransactionAttribute(TransactionAttribute.RequiresNew)
         public void someBusisnessMethod(){
             SomeLocal self = this.sessionContext.getBusinessObject(SomeLocal.class);
             String something = self.getSomeThingBusinessMethod();
             //Do more
         }
      
         @TransactionAttribute(TransactionAttribute.Required)
         public String getSomeThingBusinessMethod(){
            return anotherBean.aMethodWhichCallsTheEntityManager();
         }
      }
        • 1. Re: Call Enterprise Bean (or Database) from private Method in Session-Bean
          810311
          Found the answer by myself....

          Here it is if someone might have the same question:
          http://stackoverflow.com/questions/3381002 or if the link may down sometime the content of the answer...
          >
          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.

          written by David Blevins