473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

jndi

i have problems understanding jndi.
i'm this far: i deployed the sun application server and i'm working through
the examples (Duke bookstore, ...). Sometimes i see code which does a
lookup to a JNDI-name like this:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/BookDB");
con = ds.getConnection();

But what is happening here? the lookup to jdbc/BookDB returns a Datasource??
Where does that come from?
This Datasource is configured in the server-configuration.
Why then use this mechanism? Everything you lookup via JNDI is specified in
the server configuration. What i would expect is that jndi lets me access
configuration information (for example connection settings for databases).
For example when i go from development to production environment, the
database url changes. So i need only change the configuration information
in the jndi-tree. But that doesn't seem to be the case. Anybody can shed
some light on this? thank you very much.

when i look into the server configuration, i see defined jndi-names and the
properties that they should have
for example (snippet at end of message)

<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/__TimerPool"
object-type="system-admin" pool-name="__TimerPool"/>
<jdbc-resource enabled="true" jndi-name="jdbc/PointBase"
object-type="user" pool-name="PointBasePool"/>
<jdbc-resource enabled="true" jndi-name="jdbc/BookDB" object-type="user"
pool-name="PointBasePool"/>
<mail-resource debug="false" enabled="true" from="xy*@foo.com"
host="localhost" jndi-name="mail/Session" object-type="user"
store-protocol="imap" store-protocol-class="com.sun.mail.imap.IMAPStore"
transport-protocol="smtp"
transport-protocol-class="com.sun.mail.smtp.SMTPTransport" user="nobody"/>
<jdbc-connection-pool connection-validation-method="auto-commit"
datasource-classname="com.pointbase.xa.xaDataSource"
fail-all-connections="false" idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true" max-pool-size="32"
max-wait-time-in-millis="60000" name="__TimerPool" pool-resize-quantity="2"
res-type="javax.sql.XADataSource" steady-pool-size="8">
<property name="DatabaseName"
value="jdbc:pointbase:embedded:ejbtimer,database.h ome=${com.sun.aas.instanceRoot}/lib/databases"/>
<property name="User" value="pbPublic"/>
<property name="Password" value="pbPublic"/>
</jdbc-connection-pool>
<jdbc-connection-pool connection-validation-method="auto-commit"
datasource-classname="com.pointbase.xa.xaDataSource"
fail-all-connections="false" idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true" max-pool-size="32"
max-wait-time-in-millis="60000" name="PointBasePool"
pool-resize-quantity="2" res-type="javax.sql.XADataSource"
steady-pool-size="8">
<property name="DatabaseName"
value="jdbc:pointbase:server://localhost:9092/sun-appserv-samples"/>
<property name="Password" value="pbPublic"/>
<property name="User" value="pbPublic"/>
</jdbc-connection-pool>
</resources>

Jul 17 '05 #1
1 6190
I'm not sure I understand your confusion. Yes...the lookup returns a
DataSource, as defined by the config file. The JNDI service maintains it
and returns it whenever it's looked up. If that service is Tomcat, for
instance, then when Tomcat starts, it reads the configuration file and says,
"Oh, I need to make an instance of DataSource available, created using the
specified class name (com.pointbase.xa.xaDataSource)." (I think, but I'm
not sure, that DataSource is a thread-safe singleton maintained by the
server. So you always get the same instance.)

So when you need the DataSource instance, you just do a JNDI lookup, and
presto, it's there. Your code doesn't have to know or worry about all the
configuration settings...the DataSource class, the database driver class,
the name of the database, the login id and password, the connection pool
scheme...all of that is handled by the JNDI service. You just say, "Hey,
JNDI...gimme a DataSource so I can get a connection." And then when you
close the connection, the pool sees that and reclaims the connection.
(Assuming that your server is using connection pooling...I can't imagine any
server that wouldn't. The connection pool may be good, bad or indifferent,
but I think all JNDI services use one.) All without any effort on your
part. And this is accessible by any application that can access the JNDI
service. (Like any web app running under Tomcat, for instance.)

You ask "Why then use this mechanism?" That's what I don't understand. The
Context is just your "phone book" to look up the "number" of the thing you
want. It's the way you access all of the stuff defined and set up by the
config file.

Unless it's set up otherwise, JNDI resources are put into the java:comp/env
spot in the JDNI tree, so you could just do this:

Context initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/BookDB");

But most examples show that you should get the initial context (which
references the entire JNDI tree, including anything not in java:comp/env),
and then get a sub-context from it that refers just to java:comp/env. I
don't know why.

Anyway, hope this helps. I know that I found JNDI cofusing at first too,
but trust me, soon its simplicity becomes very apparent.

"slurper" <sl*********@skynet.be> wrote in message
news:42***********************@news.skynet.be...
i have problems understanding jndi.
i'm this far: i deployed the sun application server and i'm working through the examples (Duke bookstore, ...). Sometimes i see code which does a
lookup to a JNDI-name like this:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/BookDB");
con = ds.getConnection();

But what is happening here? the lookup to jdbc/BookDB returns a Datasource?? Where does that come from?
This Datasource is configured in the server-configuration.
Why then use this mechanism? Everything you lookup via JNDI is specified in the server configuration. What i would expect is that jndi lets me access
configuration information (for example connection settings for databases).
For example when i go from development to production environment, the
database url changes. So i need only change the configuration information
in the jndi-tree. But that doesn't seem to be the case. Anybody can shed
some light on this? thank you very much.

when i look into the server configuration, i see defined jndi-names and the properties that they should have
for example (snippet at end of message)

<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/__TimerPool"
object-type="system-admin" pool-name="__TimerPool"/>
<jdbc-resource enabled="true" jndi-name="jdbc/PointBase"
object-type="user" pool-name="PointBasePool"/>
<jdbc-resource enabled="true" jndi-name="jdbc/BookDB" object-type="user" pool-name="PointBasePool"/>
<mail-resource debug="false" enabled="true" from="xy*@foo.com"
host="localhost" jndi-name="mail/Session" object-type="user"
store-protocol="imap" store-protocol-class="com.sun.mail.imap.IMAPStore"
transport-protocol="smtp"
transport-protocol-class="com.sun.mail.smtp.SMTPTransport" user="nobody"/>
<jdbc-connection-pool connection-validation-method="auto-commit"
datasource-classname="com.pointbase.xa.xaDataSource"
fail-all-connections="false" idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true" max-pool-size="32"
max-wait-time-in-millis="60000" name="__TimerPool" pool-resize-quantity="2" res-type="javax.sql.XADataSource" steady-pool-size="8">
<property name="DatabaseName"
value="jdbc:pointbase:embedded:ejbtimer,database.h ome=${com.sun.aas.instance
Root}/lib/databases"/> <property name="User" value="pbPublic"/>
<property name="Password" value="pbPublic"/>
</jdbc-connection-pool>
<jdbc-connection-pool connection-validation-method="auto-commit"
datasource-classname="com.pointbase.xa.xaDataSource"
fail-all-connections="false" idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true" max-pool-size="32"
max-wait-time-in-millis="60000" name="PointBasePool"
pool-resize-quantity="2" res-type="javax.sql.XADataSource"
steady-pool-size="8">
<property name="DatabaseName"
value="jdbc:pointbase:server://localhost:9092/sun-appserv-samples"/>
<property name="Password" value="pbPublic"/>
<property name="User" value="pbPublic"/>
</jdbc-connection-pool>
</resources>

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mdh | last post by:
I am trying to learn the basics of MVC applications using a Tomcat infrastructure. I'm starting by building a simple application with: * a login.jsp page for a basic login form with a action...
0
by: Jeff K | last post by:
JNDI is adding a naming attribute to my add request. Is there a way to prevent this? For example: Using LdapContext.createSubcontext(String s, Attributes atts) with the following: s =...
2
by: Jon Dellaria | last post by:
I have been using MySql as the database using JSP's and JavaBeans but recently I have wanted to start using the database connection pooling mechanism built into TomCat. I think I am having a...
1
by: Kent | last post by:
I'm running on Win2000 and JRE 1.4.1.... I've got a small client trying to do get an InitialContext() and talk to the local JBoss server running on same machine (different VM of course). When...
0
by: Steffen | last post by:
Hi! I'm trying to access a EntityBean from a servlet via the bean's local home interface. The EJB and the Servlet are together in one .ear file and I'm using JBoss 3.2.3. I think the...
1
by: John Chambers | last post by:
Hi all, I'm relatively new to JNDI concepts and need some help. I am writing a web app that accepts a variable specifying a jndi pooled connection datasource as a url variable. This is a...
0
by: crossroadsk | last post by:
I have a problem in configuring JNDI properties for LDAP server i created jndi.properties file in the current directory where i'm running a simple java code. jndi.properties file which i...
5
by: Pallavi Kadam | last post by:
Tell Me Fullform of Jndl Tellme Jndi Is Releated To Topics Jndi Imp role In j2ee
1
dmjpro
by: dmjpro | last post by:
how do i do JNDI program in local machine.... plze give me JNDI programming free tutorial with examples .... plz help me .... thanx
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.