File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes EJB and Resource references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB and Resource references" Watch "EJB and Resource references" New topic
Author

EJB and Resource references

Dave Coombes
Greenhorn

Joined: Nov 06, 2001
Posts: 19
Hi,
I am hoping someone can shed some light on the following behaviour. I am using Websphere 4.0, although I'm not sure if this is relevant.
I have a session bean (SB1) which needs to reference a DataSource so I add a resource reference in its deployment descriptor, say jdbc/MyDataSource which I then map at deployment to the JNDI name jdbc/DS1 of a real datasource.
Similarly I have a session bean (SB2) which also needs access to a DataSource so I add a reference in its DD to jdbc/MyDataSource and at deployment map this in JNDI to jdbc/DS2.
In SB1 and SB2 I look up the datasource using java:comp/env/jdbc/MyDataSource and the correct real datasource is returned to each bean.
I then decide that I would like to move the data access logic to a separate Data Access Object (DAO) (as described in the J2EE Core Patterns). I then have to decide how to look up the DataSource in the DAO (I know I could look the datasource up in the EJB and pass it in).
I create a new InitialContext in the DAO for each call and do a lookup using java:comp/env/jdbc/MyDataSource. To my surprise this worked. More surprising to me was that it would return the correct datasource depending on if SB1 or SB2 was calling it. Even more surprising was that if I cached the InitialContext in the DAO, (i.e. create it when the first call is made, either by SB1 or SB2 and then use that context for all subsequent calls) it still finds the correct datasource.
The same is true for EJB references.
This would imply that the naming context, or something related to it is being carried by the calling thread. Is this the case? The behaviour I see is exactly what I want, especially in the situation where I have an EJB calling a method in a class which subsequently looks up another bean and calls a method on it, as it allows the class to be reused, with the target bean being configurable via the calling beans DD.
I hope this explanation is clear enough for someone to tell me if this is expected/reliable behaviour.
All comments much appreciated.
Cheers
Dave
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Yes, this is as you expect. It is carried along exactly as transactions are carried along -- in fact it's all based on the thread that is running, which gets associated with the EJB.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
 
wood burning stoves
 
subject: EJB and Resource references