Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Query related to JNDI datasource

Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using apache common DBCP for connectiion pooling in tomcat. I am working on a web application. Following is the lookup code:-
initContext=new InitialContext();
ds=(DataSource) initContext.lookup("java:comp/env/jdbc/yourrail");
Every time, when i need a connection object, i am always executing the above code(lookung up for datasource in JNDI tree).

My Question is:-
(1)I have a servlet as a front controller. If say 10 threads are executing the above code, they all 10 thread get a reference to the same datasource object?(I think they all get the reference to same data source object.)
(2)If it is the same data source object, then
is thread safe?(I think it is thread safe. Implementation of DBCP provides that)
(3)If i am getting the same Datasource object after lookup, it would be better to lookup only once, and store this datasource object as attribute in servlet context?

Please validate above queries. Thanks in advance for your support.

Abhishek Yadav

author & internet detective
Posts: 39997
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Yes. You are correct.
2) Also yes. DBCP is well used. It's unlikely anything threading bugs are left (if they ever existed)
3) Caching JNDI resources locally is an accepted practice.
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic