First, Introduction
Jndi (Java naming and directory Interface,java naming and directory interfaces) is a set of APIs that access naming and directory services in Java applications. The naming service links names to objects so that we can access them by name. A directory service is a naming service in which objects are not only named but also attributes.
Second, Tomcat Configuration Jndi There are three different ways.
First: Single app exclusive data source
Find the project's context node in Tomcat's Server.xml and add a private data source
- <context docbase= "Web" path= "/web" reloadable= "true" source= "org.eclipse.jst.jee.server:WebApp " >
- <resource name= "Jndi/testdb"//Specifies the Jndi name that will be used for the configuration of the spring data source bean and
- Auth= "Container"
- </Context>
- Type= "Javax.sql.DataSource"//Data source bed type, using standard Javax.sql.DataSource
- Driverclassname= "Com.mysql.jdbc.Driver"//jdbc Drive
- Url= "jdbc:mysql://localhost:3306/appdb"//Database URL address
- Username= "root"//database user name
- password= "123456"//Database Password
- Maxactive= "20"
- Maxidle= "10"//maximum number of idle connections
- Maxwait= "10000"//When the pool's database connection is already occupied, the maximum wait time
- Removeabandoned= "true"
- removeabandonedtimeout= "180"
- Logabandoned= "true"//Discarded database connection is logged for tracking
- factory= "Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" />
Advantages: Simple
Cons: Poor reusability
Second: Configure a global Jndi data source to apply to a single application
1. Globalnamingresources node in Tomcat's Server.xml, add a global data source under the node
- <resource name= "Jndi/testdb"//Specifies the Jndi name that will be used for the configuration of the spring data source bean and
- Auth= "Container"
- Type= "Javax.sql.DataSource"//Data source bed type, using standard Javax.sql.DataSource
- Driverclassname= "Com.mysql.jdbc.Driver"//jdbc Drive
- Url= "jdbc:mysql://localhost:3306/appdb"//Database URL address
- Username= "root"//database user name
- password= "123456"//Database Password
- Maxactive= "20"
- Maxidle= "10"//maximum number of idle connections
- Maxwait= "10000"//When the pool's database connection is already occupied, the maximum wait time
- Removeabandoned= "true"
- removeabandonedtimeout= "180"
- Logabandoned= "true"//Discarded database connection is logged for tracking
- factory= "Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
- />
2. In Tomcat Server.xml find the project context node to which you want to apply this Jndi data source, and increase the reference to the global data source ResourceLink
- <context docbase= "WebApp" path= "/webapp" reloadable= "true">
- <resourcelink global= "Jdbc/mysql" name= "Jdbc/mysql" type= "Javax.sql.DataSource" />
- </Context>
Pros: reusability, controllability
Cons: Configuring a relative third method is a bit cumbersome, and each project has to be
Third: Configure a global Jndi data source to apply to all apps deployed under Tomcat
1 Configure the Tomcat single global data source. (verified)
Because there are multiple Web projects loaded at the same time, any Web project can be used after configuration
1) Add the JDBC driver package under Tomcat's Lib folder.
2) in the Server.xml configuration file under Tomcat's Conf folder, add:
- <resource name= "Jndi/testdb"//Specifies the Jndi name that will be used for the configuration of the spring data source bean and
- Auth= "Container"
- Type= "Javax.sql.DataSource"//Data source bed type, using standard Javax.sql.DataSource
- Driverclassname= "Com.mysql.jdbc.Driver"//jdbc Drive
- Url= "jdbc:mysql://localhost:3306/appdb"//Database URL address
- Username= "root"//database user name
- password= "123456"//Database Password
- Maxactive= "20"
- Maxidle= "10"//maximum number of idle connections
- Maxwait= "10000"//When the pool's database connection is already occupied, the maximum wait time
- Removeabandoned= "true"
- removeabandonedtimeout= "180"
- Logabandoned= "true"//Discarded database connection is logged for tracking
- factory= "Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
- />
The factory here refers to which data source configuration class is used by the resource configuration, which uses the standard data source resource configuration class of Tomcat, which can also be written by itself. Implement the Javax.naming.spi.ObjectFactory interface. In some places the use of the Commons-dbcp.jar in the org.apache.commons.dbcp.BasicDataSourceFactory, if you use this will need to commons-dbcp.jar and its dependent jar package, are placed in Tomcat Lib , it is not enough to put the light under the web-inf/lib of the project.
3) in the Context.xml configuration file under Tomcat's Conf folder, add:
- <resourcelink global= "Jndi/testdb" name= "Jndi/testdb" type= "Javax.sql.DataSource"/>
The global resource is exposed directly to all Web projects under that Tomcat.
4) Add a resource reference to the Web project's XML: (can be omitted)
- <resource-ref>
- <description> JNDI DataSource</description>
- <res-ref-name>jndi/testdb</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth> Container</res-auth>
- </resource-ref>
The Jndi exposed data source of the Tomcat configuration is loaded at startup, where the Res-ref-name value is consistent with the name value of Server.xml, Context.xml.
5) Configure the spring data source bean information in the Web project:
- <Bean id= "DataSource" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
- < Property name= "Jndiname" >
- <value>java:comp/env/jndi/testdb</value>
- </Property >
- 5. </Bean>
Directly replace the node of the bean id= "DataSource" in the project Web-inf/conf/data-access-config.xml file with
2 Configuring Tomcat multiple global Data sources (verified)
Note: The configuration file step is the same as a single item, modifying only the file contents
1) Ibid.
2) server.xml new data source, format and single, modify name only
3) context.xml new configuration, format and single
4) Ibid. (can be omitted)
5) Dbe2 is the data source one, DBCPCCJ is the data source two, DataSource in the original frame with the underlined part of the content
- <Bean id= "dbe2" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
- < Property name= "Jndiname" >
<value>java:comp/env/jndi/dbe2</value>
- </Property >
- </Bean>
- <Bean id= "DBCPCCJ" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
- < Property name= "Jndiname" >
- <value>java:comp/env/jndi/dbcpccj</value>
- </Property >
- </Bean>
- <Bean id= "DataSource" class= "Com.wishbuild.persistent.source.DynamicDataSource" >
- < Property name= "Targetdatasources" >
- <map key-type="java.lang.String" value-type=" Com.wishbuild.persistent.source.DataSourceType ">
- <entry value-ref= "Dbe2" key= "dbe2" ></entry>
- <entry value-ref= "DBCPCCJ" key= "DBCPCCJ" ></entry>
- </Map>
- </Property >
- < name= "Defaulttargetdatasource" ref= "Dbe2" ></property>
- </Bean>
6) Modify the Datasourcetype.java Datasourceloadserver.java Dynamicdatasource.java three files in the Com.wishbuild.persistent.source package.
The first of the Datasourcetype.java
To file content:
Public enum DatasourceType { dbe2, DBCPCCJ }
Modified to:
Public class DatasourceType {
Public Static Final String dbe2 = "Dbe2";
Public Static Final String DBCPCCJ = "DBCPCCJ";
}
A second file Datasourceloadserver.java
Modify the contents of the file to use the DatasourceType class as a String, for example:
Public Static void Setdatasourcetype (DatasourceType dbType)
Modified to:
Public Static void Setdatasourcetype (String dbType)
A third file Dynamicdatasource.java
Modify the contents of the file to use the DatasourceType class as a String, for example:
DatasourceType key = Datasourceloadserver. Getdatasourcetype ();
Modified to:
String key = Datasourceloadserver. Getdatasourcetype ();
The above six configuration is completed, the JNDI+TOMCAT configuration of multiple data sources can be dynamically switched, where the switch data source code is: Datasourceloadserver.setdatasourcetype (DATASOURCETYPE.DBE2); That is, as with the original code, the benefit of multiple data sources is that any data source that Tomcat provides can be invoked as long as items such as App,web are launched under the Tomcat.
Pros: reusability, disposable
Cons: No controllability
Third, the difference between a data source configuration in a Tomcat/conf/context.xml file or a server.xml file:
Server.xml is a resource that cannot be dynamically reloaded, and once the server is started, to modify the file, you will have to restart the server to reload. In the context.xml file, the Tomcat server periodically scans the file. Once the file is found to be modified (the timestamp changes), the file is automatically reloaded without restarting the server.
Four, Common errors and Solutions
A) java.lang.ClassNotFoundException:org.apache.commons.dbcp.BasicDataSourceFactory
Error Details:
...... Warning: Failed to register in JMX:javax.naming.NamingException:Could not load resource factory class [Root exception is Java . lang. ClassNotFoundException:org.apache.commons.dbcp.BasicDataSourceFactory]
2010-1-18 13:22:37 Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener Creatembeans
Severity: Exception processing Global JNDI Resources
Javax.naming.NamingException:Could Not load resource factory class [Root exception is java.lang.ClassNotFoundException: Org.apache.commons.dbcp.BasicDataSourceFactory] ...
Problem Description: This is not found Org.apache.commons.dbcp.BasicDataSourceFactory class, this class in Commons-dbcp.jar, this jar package is placed under the web-inf/lib of the project, Instead of Tomcat, but when Tomcat starts reading its own configuration (such as Server.xml), it is not the project Lib but the Lib from Tomcat, so it must be an error. It is not possible to copy this package to Tomcat's lib, since Commons-dbcp.jar has some dependencies with other jar packages.
Solution: Change the factory= "Org.apache.commons.dbcp.BasicDataSourceFactory" in the Tomcat Jndi configuration to the factory= "Tomcat comes with" Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory ".
b) caused By:javax.naming.NameNotFoundException:Name Membermdb is not bound in this Context
Problem Description: Most of the resource are configured with global, but no link is responsible. The global resource is configured in the Server.xml globalnamingresources node resource, this resource also need to configure a resourcelink in the context node, the global resource binding to the Engineering Bureau Configuration of the unit.
Solution: Configure the Global resourcelink.
c) Cannot load JDBC driver class ' Com.mysql.jdbc.Driver ' Java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
Problem Description: Because the JNDI configuration needs to use the MySQL drive, so Tomcat needs to join the MySQL drive jar package, I use Mysql-connector-java-5.1.5-bin.jar.
Solution: Copy the Mysql-connector-java-5.1.5-bin.jar under Project Web-inf/lib to the Lib of Tomcat.
d) Name [Jndi/testdb] is isn't bound in this Context. Unable to find [JDBC].
Check to see if the configuration files for Tomcat (Server.xml and Context.xml) and Web projects are named the same in the Config. xml and Data-access-config.xml.
SPRINGMVC +jndi Configuring the data source under Tomcat (GO)