aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How to Inject session bean from session bean and viceversa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to Inject session bean from session bean and viceversa" Watch "How to Inject session bean from session bean and viceversa" New topic
Author

How to Inject session bean from session bean and viceversa

dinesh thalis
Ranch Hand

Joined: Nov 19, 2008
Posts: 39
I have two session beans, they are StudentFacade and SchooldtFacade both of them Stateless, Remote. What I need is call StudentFacade method from SchooldtFacade and vice-versa. Below show how I did it.



If run this application using Galssfish version V2.1 ,V3 work fine without any exception. If I run it using jboss-5.1.0.GA it gave below exception.




How can I solve this problem.

Thank you.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

JBoss AS has a problem with circular injection of EJBs. You can use JBoss specific @org.jboss.ejb3.annotation.IgnoreDependency on one side of the injection to get past this problem. See this thread for details http://www.coderanch.com/t/498867/JBoss/dependency-injection-jboss

[My Blog] [JavaRanch Journal]
dinesh thalis
Ranch Hand

Joined: Nov 19, 2008
Posts: 39
Thanks for reply. I'm using jboss 5.10 and netbeans 6.9.1 In here IDE can't find @IgnoreDependency , I added this also import org.jboss.annotation.IgnoreDependency; but not able to find library that contain @IgnoreDependency. please tell what is the library name should I use ?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

dinesh thalis wrote: I added this also import org.jboss.annotation.IgnoreDependency;

That's not the correct one for AS5. The correct one for AS5 is mentioned in my previous reply. You can find that annotation in JBOSS_HOME/client/jboss-ejb3-ext-api.jar of AS5.
Manuel Alberto Quero
Ranch Hand

Joined: Jul 17, 2009
Posts: 31
If you run into this problem when an EJB is referencing to itself (which is an specific case of circular injection),
you can workaround this using the SessionContext interface (SessionContext.getBusinessObject()):

EjbExampleLocal refToMyself = sessionCtx.getBusinessObject(EjbExampleLocal.class)
refToMyself.otherMethod();

Hope this helps,

Manuel
dinesh thalis
Ranch Hand

Joined: Nov 19, 2008
Posts: 39
This is how currently my code, it works fine without any exception my application currently run on glassfish v2.1



according to your(Manuel Alberto Quero) advice change my code below show that



But it gave exception



How can I solve this issue

Thanks in advance

Manuel Alberto Quero
Ranch Hand

Joined: Jul 17, 2009
Posts: 31
Hi,

It seems that the problem is that you are initializing customFacade before ctx is given the proper value by the EJB container (i.e. when ctx is still null).

You can try to initialize customFacade in the method's body or if you prefer you could add a method as follows:


Just remember that this solution is valid when the EJB is referencing to itself...

Hope this can solve your problem,

Manuel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Inject session bean from session bean and viceversa