Three ways to configure Jndi data sources in Tomcat

Source: Internet
Author: User

 three ways to configure Jndi data sources in TomcatCategory: Java advanced 2012-07-01 10:24 18328 people read reviews (0) favorite reports Tomcatjdbcweblogicmysqlbean Test

Directory (?) [+]

In the course of my past work, the development server was generally tomcat

Data sources are often configured with a DataSource bean in Applicationcontext.xml

Then modify the Jndi configuration at deployment time

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

Unlike servers like Jboss,weblogic, you can add Jndi data sources directly from the admin interface

And few people are going to study its configuration.

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

The test is uncle, teach him to change the data source or when not heard

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

Note: If your project is lost directly under WebApps, there is no project corresponding to the context node in Server.xml

Update: Since some of the previous configuration from the network, not very good, made some updates

Made a few personal comments on each of these methods.

PS: The following configuration is tested under apache-tomcat-6.0.35 and can access the database

First, a single application with exclusive data sources is one step, find Tomcat Server.xml find the project context node, add a private data source XML code
  1. <Context docbase="WebApp" path="/webapp" reloadable="true" source=" Org.eclipse.jst.jee.server:WebApp ">
  2. <Resource
  3. name="Jdbc/mysql"
  4. scope="shareable"
  5. type="Javax.sql.DataSource"
  6. factory="Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
  7. url="Jdbc:mysql://localhost:3306/test"
  8. driverclassname ="Com.mysql.jdbc.Driver"
  9. username="root"
  10. password="root"
  11. />
  12. </Context>

Advantages: Simple

Cons: Poor reusability

Second, configure the global Jndi data source to apply to a single application

In two steps

The first step is to find the Globalnamingresources node in Tomcat's Server.xml and add a global data source XML code under the node
  1. <Resource
  2. name="Jdbc/mysql"
  3. scope="shareable"
  4. type="Javax.sql.DataSource"
  5. factory="Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
  6. url="Jdbc:mysql://localhost:3306/test"
  7. driverclassname ="Com.mysql.jdbc.Driver"
  8. username="root"
  9. password="root"
  10. />
Second, 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 XML code
    1. <context  docbase= "WebApp"  path= "/webapp"  reloadable= "true" >  
    2.     <resourcelink  global= "Jdbc/mysql"  name=< Span class= "Attribute-value" > "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 the global Jndi data source to apply to all apps deployed under Tomcat

Two steps, too.

The first step

Refer to the first step of the second type

The second step is to find the context.xml of Tomcat and 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 <Context>

XML code
    1. <Context>
    2. <resourcelink global="Jdbc/mysql" name="Jdbc/mysql" type=" Javax.sql.DataSource " />
    3. <watchedresource>web-inf/web.xml</watchedresource>
    4. <Context>
Pros: reusability, disposable

Cons: No controllability

Spring reference to a Jndi data source

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

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

The values of type and factory vary

Username=>user

Url=>jdbcurl

Driverclassname=>driverclass

XML code
  1. <Resource name="jdbc/mysql_c3p0" scope="shareable "
  2. type="Com.mchange.v2.c3p0.ComboPooledDataSource"
  3. factory="Org.apache.naming.factory.BeanFactory"
  4. jdbcurl= "jdbc:mysql://localhost:3306/test" driverclass="Com.mysql.jdbc.Driver "
  5. user="root" password="root" />
For more detailed information, please refer to: Java Advanced network www.javady.com

Three ways to configure Jndi data sources in Tomcat

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.