SPRINGMVC +jndi Configuring the data source under Tomcat

Source: Internet
Author: User

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

    1. <context docbase= "Web" path= "/web" reloadable= "true" source= "org.eclipse.jst.jee.server:WebApp " >
    2. <resource name= "Jndi/testdb"//Specifies the Jndi name that will be used for the configuration of the spring data source bean and
    3. Auth= "Container"
    4. </Context>
  1. Type= "Javax.sql.DataSource"//Data source bed type, using standard Javax.sql.DataSource
  2. Driverclassname= "Com.mysql.jdbc.Driver"//jdbc Drive
  3. Url= "jdbc:mysql://localhost:3306/appdb"//Database URL address
  4. Username= "root"//database user name
  5. password= "123456"//Database Password
  6. Maxactive= "20"
  7. Maxidle= "10"//maximum number of idle connections
  8. Maxwait= "10000"//When the pool's database connection is already occupied, the maximum wait time
  9. Removeabandoned= "true"
  10. removeabandonedtimeout= "180"
  11. Logabandoned= "true"//Discarded database connection is logged for tracking
  12. 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

  1. <resource name= "Jndi/testdb"//Specifies the Jndi name that will be used for the configuration of the spring data source bean and
  2. Auth= "Container"
  3. Type= "Javax.sql.DataSource"//Data source bed type, using standard Javax.sql.DataSource
  4. Driverclassname= "Com.mysql.jdbc.Driver"//jdbc Drive
  5. Url= "jdbc:mysql://localhost:3306/appdb"//Database URL address
  6. Username= "root"//database user name
  7. password= "123456"//Database Password
  8. Maxactive= "20"
  9. Maxidle= "10"//maximum number of idle connections
  10. Maxwait= "10000"//When the pool's database connection is already occupied, the maximum wait time
  11. Removeabandoned= "true"
  12. removeabandonedtimeout= "180"
  13. Logabandoned= "true"//Discarded database connection is logged for tracking
  14. factory= "Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
  15. />

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

    1. <context docbase= "WebApp" path= "/webapp" reloadable= "true">
    2. <resourcelink global= "Jdbc/mysql" name= "Jdbc/mysql" type= "Javax.sql.DataSource" />
    3. </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:

  1. <resource name= "Jndi/testdb"//Specifies the Jndi name that will be used for the configuration of the spring data source bean and
  2. Auth= "Container"
  3. Type= "Javax.sql.DataSource"//Data source bed type, using standard Javax.sql.DataSource
  4. Driverclassname= "Com.mysql.jdbc.Driver"//jdbc Drive
  5. Url= "jdbc:mysql://localhost:3306/appdb"//Database URL address
  6. Username= "root"//database user name
  7. password= "123456"//Database Password
  8. Maxactive= "20"
  9. Maxidle= "10"//maximum number of idle connections
  10. Maxwait= "10000"//When the pool's database connection is already occupied, the maximum wait time
  11. Removeabandoned= "true"
  12. removeabandonedtimeout= "180"
  13. Logabandoned= "true"//Discarded database connection is logged for tracking
  14. factory= "Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
  15. />

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:

    1. <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)

    1. <resource-ref>
    2. <description> JNDI DataSource</description>
    3. <res-ref-name>jndi/testdb</res-ref-name>
    4. <res-type>javax.sql.DataSource</res-type>
    5. <res-auth> Container</res-auth>
    6. </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:

    1. <Bean id= "DataSource" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
    2. < Property name= "Jndiname" >
    3. <value>java:comp/env/jndi/testdb</value>
    4. </Property >
    5. 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

  1. <Bean id= "dbe2" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
  2. < Property name= "Jndiname" >

  3. <value>java:comp/env/jndi/dbe2</value>
  4. </Property >
  5. </Bean>
  6. <Bean id= "DBCPCCJ" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
  7. < Property name= "Jndiname" >
  8. <value>java:comp/env/jndi/dbcpccj</value>
  9. </Property >
  10. </Bean>
  11. <Bean id= "DataSource" class= "Com.wishbuild.persistent.source.DynamicDataSource" >
  12. < Property name= "Targetdatasources" >
  13. <map key-type="java.lang.String" value-type=" Com.wishbuild.persistent.source.DataSourceType ">
  14. <entry value-ref= "Dbe2" key= "dbe2" ></entry>
  15. <entry value-ref= "DBCPCCJ" key= "DBCPCCJ" ></entry>
  16. </Map>
  17. </Property >
  18. < name= "Defaulttargetdatasource" ref= "Dbe2" ></property>
  19. </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)

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.