First, the JNDI noun explanation
JNDI is the abbreviation for the Java Naming and directory interface (Java naming and directory Interface,jndi). It has been one of the core technologies of the Java 2 Platform Enterprise Edition (JEE) from the beginning. In JMS,JMAIL,JDBC,EJB and other technologies, this technology is used in a large number of applications.
Naming Service : It provides the ability to create a standard name for a given set of data. It allows you to associate a name with a Java object or resource without having to indicate the physical ID of the object or resource. The basic name operation is included in the context interface.
Directory Service: The directory service is a special type of database that, contrary to relational database management systems such as SQL Server, Access, and Oracle, constructs a directory service to handle behavior-based transactions and uses a relational information model. Basic directory service operations are included in the DirContext interface.
the architecture and implementation of JNDI
service providers for JNDI
A service provider is a set of Java classes that enable developers to communicate with the directory service in a manner similar to how the JDBC driver communicates with the database. A service provider that can be used for Jndi must implement the context interface or the extended interface Directory-context of the context.
When using Jndi, the reader only needs to understand Jndi, and the service provider is concerned with details such as the actual network protocol, encoding/decoding value, and so on.
When the SDK Software Development Kit is downloaded, some of sun's existing service providers are also downloaded. These service providers include LDAP, NIS, COS (CORBA object Service), RMI registration, and the provider of the file system. such as: Hashtableobj.put (context.initial_context_factory, "com.sun.jndi.ldap.ldapctx-fatory") means the use of the Sun LDAP Service provider. Of course, if you want to use an IBM service provider, you can use Com.ibm.jndi.LDAPCtxFatory instead of com.sun.jndi.ldap.ldapCtxFatory.
JNDI's Package
JNDI consists of 5 packages
· Javax.naming: Mainly used for naming operations, it contains the naming service classes and interfaces, the package defines the context interface and the InitialContext class;
· Javax.naming.directory: Mainly used for directory operations, it defines the DirContext interface and Initialdir-context class;
· Javax.naming.event: Request event notification in the named directory server;
· JAVAX.NAMING.LDAP: Provide LDAP support;
· JAVAX.NAMING.SPI: Allows for the dynamic insertion of different implementations, providing a way for developers of different named directory service vendors to develop and implement, so that applications can access related services through Jndi.
Common JNDI Operations
The common Jndi operations are as follows:
void void void void void namingenumeration listbindings (String sName), manifest: Returns a list of manifests that bind to the specified property name object in a specific context, which returns the name, class, and object itself, for those that need to
The specific usage is as follows:
//get a reference to the initial directory environmentContext cntxt=NewInitialContext ();//returns a list of manifests that are bound to the specified property name object in a specific contextnamingenumeration namenumlist= Ctxt.listbinding ("Cntxtname");//loop lists all names, classes, and objects while(Namenumlist.hasmore ()) {Binding BND=(Binding) namenumlist.next (); String Sobjname=Bnd.getname (); String Sclassname=Bnd.getclassname (); //get the objectsomeobject objlocal=(Someobject) Bnd.getobject ();}
The Namingenumeration list (string sName) is similar to Listbindings (string sName) except that it returns only a series of name/class mappings, primarily for context-browsing applications.
Second, JNDI configuration
Configuring JNDI Resources
Copy the data source definition template for the database reference of the JBoss document to the JBoss server. $DOC _home/docs/examples/jca/mysql-ds.xml---> $Server _home/server/default/deploy.
Modify the contents of the Mysql-ds.xml file so that it can properly access your MySQL database through JDBC, as follows:
<?XML version= "1.0" encoding= "UTF-8"?><datasources><Local-tx-datasource> <Jndi-name>Mysqlds</Jndi-name> <Connection-url>Jdbc:mysql://localhost:3306/demo</Connection-url> <Driver-class>Com.mysql.jdbc.Driver</Driver-class> <User-name>Root</User-name> <Password>Rootpassword</Password><Exception-sorter-class-name>Org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</Exception-sorter-class-name> <metadata> <type-mapping>Mysql</type-mapping> </metadata></Local-tx-datasource></datasources>
Configuring JNDI Resource References
In order for JNDI to parse java:comp/env/jdbc/mydatasource
the reference, the tag must be <resource-ref>
inserted into the Web. xml file (the deployment descriptor for the application)
<RESOURCE-REF>: Resource references, general declarations manage resources such as database drivers, JavaMail sessions, custom class factories, and so on.
<RESOURCE-ENV-REF>: Resource environment reference, used in Servlet 2.4 to simplify the provisioning of resource environments that do not require authentication information , generally declare resource-related managed objects, such as environment parameters, resource-ref variables.
<Resource-ref> <Res-ref-name>Jdbc/mydatasource</Res-ref-name> <Jndi-name>Java:mysqlds</Jndi-name>//point to the true JNDI name<Res-type>Javax.sql.DataSource</Res-type> <Res-auth>Container</Res-auth></Resource-ref>
<resource-ref>
The portal tells the servlet container that the deployer will set a resource called in the component naming context (component naming contexts) jdbc/mydatasource
. The component naming context is represented by a prefix java:comp/env/
, so the fully qualified local resource name is: java:comp/env/jdbc/mydatasource
.
Using JNDI to get a data source
Connection conn=NULL;Try{Context CTX=NewInitialContext (); Object Datasourceref=ctx.lookup ("Java:comp/env/jdbc/mydatasource"); DataSource DS=(Datasource) datasourceref; Connection C=ds.getconnection (); /*Use the connection*/c.close ();} Catch(Exception e) {e.printstacktrace ();}finally { if(conn!=NULL) { Try{conn.close (); } Catch(SQLException e) {} }}
External resource Links:
The role of JNDI in the Java EE
Detailed Jndi Lookup resource reference java:/comp/env
Three ways to configure Tomcat under Jndi
Apache Tomcat 5.5 servlet/jsp container
Resource-ref and Resource-env-ref
JNDI configuration: JBoss + MySQL