Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

jndi

Thread view: 
slurper - 08 Mar 2005 13:24 GMT
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="xyz@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.home=${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>
Dan Nuttle - 11 Mar 2005 02:13 GMT
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.

> i have problems understanding jndi.
> i'm this far: i deployed the sun application server and i'm working through
[quoted text clipped - 41 lines]
> res-type="javax.sql.XADataSource" steady-pool-size="8">
>       <property name="DatabaseName"

value="jdbc:pointbase:embedded:ejbtimer,database.home=${com.sun.aas.instance
Root}/lib/databases"/>
>       <property name="User" value="pbPublic"/>
>       <property name="Password" value="pbPublic"/>
[quoted text clipped - 13 lines]
>     </jdbc-connection-pool>
>   </resources>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.