Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of JNDI

Tags:

java

jndi

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

like image 237
Ajay Avatar asked Aug 29 '09 08:08

Ajay


People also ask

What is the purpose of JNDI Mcq?

JNDI is an API used to access the directory and naming services (i.e. the means by which names are associated with objects).

What is difference between JNDI and JDBC?

JDBC is Java Database Connectivity API, while JNDI is Java Naming and Directory Interface API. The main thing here is that in a JNDI directory you're actually storing a JDBC DataSource, so, you're simply using JDBC to obtain a Connection via JNDI lookup.

What is supported by JNDI?

The product includes a name server to provide shared access to Java™ components, and an implementation of the javax. naming JNDI package which supports user access to the name server through the Java Naming and Directory Interface (JNDI) naming interface.

What is LDAP and JNDI?

LDAP is a standard way to provide access to directory information. JNDI gives Java applications and objects a powerful and transparent interface to access directory services like LDAP.


1 Answers

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 = (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 Oracle's tutorial.

like image 108
Simon Nickerson Avatar answered Sep 20 '22 23:09

Simon Nickerson