By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

jndi

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.