Three ways to configure a Jndi data source in Tomcat

Source: Internet
Author: User
Tags tomcat server

In the course of my past work, the development server is typically tomcat

The configuration of a data source is often configured with a DataSource bean in Applicationcontext.xml

Then modify the Jndi configuration at deployment time

I guess it's because Tomcat configuration needs to be changed to config file.

Unlike jboss,weblogic, servers in the Admin interface can add Jndi data sources directly

And few people are going to study its configuration.

Recently a small project, release version of the time is through ant compiled into a jar package and then dropped to the test

Test is grandpa, teach him to modify the data source or when did not hear

Weekend idle to bored, read some of the Tomcat configuration tutorial, below to do some summary

Note: If your project is dropped directly below the WebApps, there will be no project-corresponding context node in Server.xml

Update: Some of the previous configuration from the network, not very useful, made some updates

A few personal evaluations of each of these methods

PS: The following configuration under apache-tomcat-6.0.35 test pass, you can access the database


First, single application exclusive data source just one step, find Tomcat's server.xml. Find the project context node and add a private data source  XML code <context docbase= "WebApp" path= "/webapp" reloadable= "true" source= "org.eclipse.jst.jee.server:WebApp" > <resource name= "Jdbc/mysql" scope= "shareable" type= "Javax.sql.DataSource" factory= Cat.dbcp.dbcp.BasicDataSourceFactory "url=" jdbc:mysql://localhost:3306/test "driverclassname =" com.mysql.jdbc.Dr Iver "username=" "root" password= "root"/> </Context>

Advantages: Simple

Disadvantage: Poor reusability
The second is to configure the global Jndi data source to apply to a single application

The first step in two steps , find the Globalnamingresources node in Tomcat Server.xml, add a global data source XML code under the node <resource name= "Jdbc/mysql"      Scope= "shareable" type= "Javax.sql.DataSource" factory= "Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" Url= "jdbc:mysql://localhost:3306/test" driverclassname = "Com.mysql.jdbc.Driver" username= "root" passwor d= "root"/> Step Two, locate the engineering context node to which you want to apply this Jndi data source, and increase the reference to the global data source ResourceLink XML code <context docbase= "WEBAPP" Pat H= "/webapp" reloadable= "true" > <resourcelink global= "jdbc/mysql" name= "Jdbc/mysql" type= "Javax.sql.DataSource" "/> </Context>

Benefits: reusability, controllability

Disadvantage: Configuring a relatively third approach is cumbersome, with each project
The third is to configure the global Jndi data source to apply to all Tomcat deployments

Two steps, first step .

Refer to the second step of step two , find Tomcat context.xml, add a ResourceLink node under the context node to reference the data source configured in the first step

The root node of this XML configuration file is the <Context>

XML code <Context> <resourcelink global= "Jdbc/mysql" name= "Jdbc/mysql" type= "Javax.sql.DataSource"/> & Lt Watchedresource>web-inf/web.xml</watchedresource> <Context> Benefits: reusability, one-time in place

Disadvantage: No controllability

Spring's reference to the Jndi data source

Add a bean to the applicationcontext.xml to replace the original datasource

XML code <jee:jndi-lookup id= "DataSource" jndi-name= "Jdbc/mysql"/>
C3P0 The data source configuration

Values for type and factory are changed

Username=>user

Url=>jdbcurl

Driverclassname=>driverclass XML code <resource name= "jdbc/mysql_c3p0 scope=" shareable "type=" com.mchange.v2.c3 P0. Combopooleddatasource "factory=" Org.apache.naming.factory.BeanFactory "jdbcurl=" jdbc:mysql://localhost:3306/tes T

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.