Showing posts with label jndi. Show all posts
Showing posts with label jndi. Show all posts

Thursday, October 3, 2013

Understanding JNDI

Conceptually, JNDI is like System.getProperties() on steroids.
System.getProperties() allows you to pass String parameters to your code from the command line. Similarly, JNDI allows you to configure arbitrary objects outside of your code (for example, in application server config files) and then use them in your code.
In other words, it's an implementation of Service Locator pattern: your code obtains services configured by environment from the centeral registry.
As usually with Service Locators, your code should have some entry point to access Service Locator.InitialContext is this entry point: you create InitialContext and then obtain required services from JNDI with lookup().

What is JNDI ? What is its basic use..? When it is used?

What is JNDI ?
It stands for Java Naming and Directory Interface. A Google search would have told you that and more.
What is its basic use?
JNDI allows distributed applications to look up services in an abstract, resource-independent way.
When it is used?
The most common use case is to set up a database connection pool on a Java EE application server. Any application that's deployed on that server can gain access to the connections they need using the JNDI name "java:comp/env/FooBarPool" without having to know the details about the connection.
This has several advantages:
  1. If you have a deployment sequence where apps move from devl->int->test->prod environments, you can use the same JNDI name in each environment and hide the actual database being used. Applications don't have to change as they migrate between environments.
  2. You can minimize the number of folks who need to know the credentials for accessing a production database. Only the Java EE app server needs to know if you use JNDI.