Tomcat JNDI Configuration

Source: Internet
Author: User

1. Add a JNDI data source to Tomcat
We recommend that you use adiminstration of Tomcat. By default, the Administration component is not installed in Tomcat. For detailed installation steps, see install the component Admin for Tomcat.
Log on to http: // localhost: 8080/admin and choose resources> data sources> Create new data source.
Set the JNDI parameter: (MySQL is used as an example here)
JNDI name: JDBC/MySQL (start with the JNDI name)
Data source URL: JDBC: mysql: // localhost: 3306/test (here test is the database name, of course, other parameters can be included, such as: JDBC: mysql: // localhost: 3306/test? User = root & Password = & useunicode = true & characterencoding = GBK & autoreconnect = true & failoverreadonly = false)
JDBC Driver Class: COM. MySQL. JDBC. Driver (put the corresponding driver jar package to $ tomcat_home $/common/LIB)
User name: Root (user name)
Password: ***** (password)
Max. Active connections: 4
Max. idle connections: 2
Max. Wait for connection: 5000

2. Configure resouce-ref in a web project
Add the following content to the project's web. xml root node:

<Resource-ref>
<Res-ref-Name> JDBC/MySQL </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
<Res-sharing-scope> retriable </RES-sharing-scope>
</Resource-ref>

Note: The res-ref-name must be the same as the JNDI name: JDBC/MySQL set in Tomcat.

3. Set $ atat_home $/CONF/server. xml
Because Tomcat does not automatically add the data source resource information to the context, this step is often ignored.
The content of the current server. XML is:
<? XML version = "1.0" encoding = "UTF-8"?>
<Server>
<Listener classname = "org. Apache. Catalina. Core. aprlifecyclelistener"/>
<Listener classname = "org. Apache. Catalina. mbeans. globalresourceslifecyclelistener"/>
<Listener classname = "org. Apache. Catalina. storeconfig. storeconfiglifecyclelistener"/>
<Listener classname = "org. Apache. Catalina. mbeans. serverlifecyclelistener"/>
<Globalnamingresources>
<Environment
Name = "simplevalue"
Type = "Java. Lang. Integer"
Value = "30"/>
<Resource
Auth = "Container"
Description = "user database that can be updated and saved"
Name = "userdatabase"
Type = "org. Apache. Catalina. userdatabase"
Path Name = "CONF/tomcat-users.xml"
Factory = "org. Apache. Catalina. Users. memoryuserdatabasefactory"/>
<Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "123456"
Driverclassname = "com. MySQL. JDBC. Driver"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // localhost: 3306/test"
Maxactive = "4"/>
</Globalnamingresources>
<Service
Name = "Catalina">
<Connector
Port = "8080"
Redirectport = "8443"
Minsparethreads = "25"
Connectiontimeout = "60000"
Connectionlinger = "-1"
Serversotimeout = "0"
Maxsparethreads = "75"
Maxthreads = "150"
Tcpnodelay = "true"
Maxhttpheadersize = "8192">
</Connector>
<Connector
Port = "8009"
Redirectport = "8443"
Protocol = "AJP/1.3">
</Connector>
<Engine
Defaulthost = "localhost"
Name = "Catalina">
<Realm classname = "org. Apache. Catalina. realm. userdatabaserealm"/>
<Host
Appbase = "webapps"
Name = "localhost">
<Context
Docbase = "/test"
Path = "/test"
Reloadable = "true"
DEBUG = "5"
Crosscontext = "true">
<Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "123456"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Maxactive = "4"
/>
</Context>
</Host>
</Engine>
</Service>
</Server>
If you do not change the server. xml file, throw exception cannot create JDBC driver of class ''for connect URL 'null' when using JDBC/MySQL '.
Pay attention to the final resource of the Code
<Context
Docbase = "/test"
Path = "/test"
Reloadable = "true"
DEBUG = "5"
Crosscontext = "true">
<Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "123456"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Maxactive = "4"
/>
</Context>
The <resource/> label does not contain the driverclassname and URL attributes. This is the reason for throwing exception cannot create JDBC driver of class ''for connect URL 'null.
We only need to set the corresponding resource in <globalnamingresources> </globalnamingresources>

<Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "123456"
Driverclassname = "com. MySQL. JDBC. Driver"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // localhost: 3306/test"
Maxactive = "4"/>

Replace the resource in the context.

The content of the changed server. xml file is:
<? XML version = "1.0" encoding = "UTF-8"?>
<Server>
<Listener classname = "org. Apache. Catalina. Core. aprlifecyclelistener"/>
<Listener classname = "org. Apache. Catalina. mbeans. globalresourceslifecyclelistener"/>
<Listener classname = "org. Apache. Catalina. storeconfig. storeconfiglifecyclelistener"/>
<Listener classname = "org. Apache. Catalina. mbeans. serverlifecyclelistener"/>
<Globalnamingresources>
<Environment
Name = "simplevalue"
Type = "Java. Lang. Integer"
Value = "30"/>
<Resource
Auth = "Container"
Description = "user database that can be updated and saved"
Name = "userdatabase"
Type = "org. Apache. Catalina. userdatabase"
Path Name = "CONF/tomcat-users.xml"
Factory = "org. Apache. Catalina. Users. memoryuserdatabasefactory"/>
<Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "123456"
Driverclassname = "com. MySQL. JDBC. Driver"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // localhost: 3306/test"
Maxactive = "4"/>
</Globalnamingresources>
<Service
Name = "Catalina">
<Connector
Port = "8080"
Redirectport = "8443"
Minsparethreads = "25"
Connectiontimeout = "60000"
Connectionlinger = "-1"
Serversotimeout = "0"
Maxsparethreads = "75"
Maxthreads = "150"
Tcpnodelay = "true"
Maxhttpheadersize = "8192">
</Connector>
<Connector
Port = "8009"
Redirectport = "8443"
Protocol = "AJP/1.3">
</Connector>
<Engine
Defaulthost = "localhost"
Name = "Catalina">
<Realm classname = "org. Apache. Catalina. realm. userdatabaserealm"/>
<Host
Appbase = "webapps"
Name = "localhost">
<Context
Docbase = "/test"
Path = "/test"
Reloadable = "true"
DEBUG = "5"
Crosscontext = "true">
<Resource
Name = "JDBC/MySQL"
Type = "javax. SQL. datasource"
Password = "123456"
Driverclassname = "com. MySQL. JDBC. Driver"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // localhost: 3306/test"
Maxactive = "4"/>
</Context>
</Host>
</Engine>
</Service>
</Server>

4. Use the JNDI resource in Java code to obtain the datasource

Javax. Naming. initialcontext CTX = new javax. Naming. initialcontext ();
Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/MySQL ");
Connection conn = Ds. getconnection ();

The Code passes the test in jdk1.5.

As mentioned in some documents, in versions earlier than jdk1.5, you can use
Datasource DS = (datasource) CTX. Lookup ("JDBC/MySQL ");
Obtain the datasource.

I have not tested the code.

The above code works well in the J2EE server environment, but a noinitialcontextexception will be reported in main.

The reason for noinitialcontextexception is that necessary JNDI parameters cannot be obtained from system. properties. In the server environment, these parameters are placed in system. properties when the server is started.
You can solve this problem by using the following methods:
Hashtable Env = new hashtable ();
Env. Put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory ");
Env. Put (context. provider_url, "T3: // localhost: 7001 ");
Initialcontext CTX = new initialcontext (ENV );

Or
System. setproperty (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory ");
System. setproperty (context. provider_url, "RMI: // localhost: 3306/test ");
System. setproperty (context. security_principal, "root ");
System. setproperty (context. security_credentials, "123456 ");

We recommend that you configure the above Code in the configuration file.
Java. Naming. Factory. Initial = weblogic. JNDI. wlinitialcontextfactory
Java. Naming. provider. url = RMI: // localhost: 3306/test
Java. Naming. Security. Principal = user
Java. Naming. Security. Credentials = Password

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.