tags:

views:

369

answers:

2

How can you realize the usage of JNDI , with an example if possible?

+9  A: 

JNDI is the Java Naming and Directory Interface. It's used to separate the concerns of the application developer and the application deployer. When you're writing an application which relies on a database, you shouldn't need to worry about the user name or password for connecting to that database. JNDI allows the developer to give a name to a database, and rely on the deployer to map that name to an actual instance of the database.

For example, if you're writing code that runs in a Java EE container, you can write this to get hold of the data source with JNDI name "Database":


DataSource dataSource = null;
try
{
    Context context = new InitialContext();
    dataSource = context.lookup("Database");
}
catch (NamingException e)
{
    // Couldn't find the data source: give up
}

Note there's nothing here about the database driver, or the user name, or the password. That is configured inside the container.

JNDI is not restricted to databases (JDBC); all sorts of services can be given names. For more details, you should check out the Sun tutorial on the subject.

Simon Nickerson
So with this example in picture, how is JNDI different from placing Database names in a config xml file or a property file and then reading it from there?
Ajay
Firstly, you'd have to store the password in plain text in your config file. Secondly, if you have several apps pointing to the same database and something about the database config changes, you have to update the config in multiple places.
Simon Nickerson
well, how does JNDI help here?
Ajay
Only one place to make the changes, and the container manages keeping the settings secure for you.
Simon Nickerson
this should contain all you need to know about JNDI, whether in a J2EE context or a JavaSE context.http://www.javaworld.com/javaworld/jw-04-2002/jw-0419-jndi.html
Nico
And, if the database server changes, your app doesn't have to be affected, because the JNDI name remains unchanged.
duffymo
+3  A: 

JNDI is a very powerful mechanism for both organizing configuration information and discovering and listening to services via using the EventContext. In JNDI you can lookup and listen to any object (not just DataSources), assuming your JNDI service provider supports it.

Of course, the only issue is actually having a JNDI service provider; the great thing about this is that it surprisingly easy to roll your own. After all you can encode any Java instance into XML using the JavaBeans XMLEncoder and XMLDecoder: you don't need to rely on running within an application server!

So what is the difference between this an having configuration files? Well, it can be much cleaner because all of your applications can get their configuration from the same place. If they need to share configuration information (e.g. database locations) then this can be defined once in JNDI. Suppose you moved database servers: you don't need to remember the gazillion config files with the location in it. You just go to the one place: JNDI.

oxbow_lakes