File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes JNDI design question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "JNDI design question" Watch "JNDI design question" New topic

JNDI design question

Nasim Sohail

Joined: Sep 13, 2001
Posts: 1
I want to get the connection using weblogic server 6.0 datasource. For my Jndi context i always use same credential and other parameters as required to create InitialContext object. Should I declare DataSource object as static variable in my class or instanace variable.
Is JNDI lookup for each http request is expensive.
If i design a singleton class that use same DataSource object for all the simultaneous clients. Is it save to do that or i should not use singleton pattern that creates a single DataSource Instance? In other words i should create a new DataSource object for each request to get connection?
Note : I am also using the connection pooling so i always return connection after executing my query?
// Is is to save to use same datasource object when Datasource object is shared among the concurrent clients
public class ServiceLocator{
private DataSource ds;
private static ServiceLocator sl;
public static getInstance(){
if (sl == null){
sl = new ServiceLocator();
return sl;
private ServiceLocator(){
try {
Context ctx = new InitialContext(ht);
ds = (javax.sql.DataSource) ctx.lookup ("myJtsDataSource");
} catch (NamingException e) { }
public Connection getConnection(){
return ds.getConnection();
[This message has been edited by Nasim Sohail (edited September 13, 2001).]
[This message has been edited by Nasim Sohail (edited September 13, 2001).]

Joined: Sep 18, 2001
Posts: 1
Hi There i am looking for exactly the same answers.Pls let me know if you come to know something at
syed adnan

Joined: Sep 20, 2001
Posts: 5
It is not neccessary to do DataSource as, static if you want to perform connection pooling.
Yes, JNDI lookup for each request is expensive, but it is not needed beacause your class is singalton & only one instance is created & you will lookup DataSource in Constructor so it is lookup only once.
Then you maintain a pool of connections & populate and maintain it.
Obviously, In this senario DataSource object is shared between different clients but the method which return connection should be synchronized.
Here are also some delay can be produce if multiple client are reached at the same time, but i think it
is less than to lookup the DataSource object each time. You could not return multiple connection at the
same time beacuase multithreading is not allowed in ejb.
CMP approach is much better then this, beacuse server handle all these pooling issues itself.
In your code getInstance() not returning any value.
I agree. Here's the link:
subject: JNDI design question
It's not a secret anymore!