Three ways to configure JNDI data source in Tomcat

Three ways to configure JNDI data source in Tomcat

In my past work, the development server was generally Tomcat.

The data source is often configured by configuring a dataSource bean in applicationContext.xml

Then modify the JNDI configuration during deployment

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

Unlike JBoss, WebLogic and other servers, you can directly add JNDI data sources in the management interface

Few people study its configuration.

I recently did a small project. When releasing the version, I compiled it into a jar package through ant and then threw it to the test

The tester is the boss. He was taught to modify the data source, but he pretended not to hear it.

I was bored on the weekend and read some Tomcat configuration tutorials. Here are some summaries

Note: If your project is directly dropped under webapps, there will be no Context node corresponding to the project in server.xml

Update: Since some of the previous configurations came from the Internet and were not very useful, some updates were made

Some personal comments on each method

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

The first type is a single application with exclusive data source

In the first step, find Tomcat's server.xml, find the project's Context node, and add a private data source

<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="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"  
    url="jdbc:mysql://localhost:3306/test"  
    driverClassName = "com.mysql.jdbc.Driver"  
    username="root"  
    password="root"  
/>  
</Context>  

Advantages: Simple

Disadvantages: poor reusability

The second method is to configure a global JNDI data source and apply it to a single application.

Two Steps

The first step is to find the GlobalNamingResources node in Tomcat's server.xml and add a global data source 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"  
    password="root"  
/>  
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

The second step is to find the project Context node to which this JNDI data source is to be applied, and add a reference ResourceLink to the global data source.

<Context docBase="WebApp" path="/WebApp" reloadable="true">  
    <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource" />  
</Context>

Advantages: reusability, controllability

Disadvantages: The configuration is more complicated than the third method, and each project must be configured

The third method is to configure a global JNDI data source and apply it to all applications deployed under Tomcat.

Also divided into two steps

first step

Refer to the first step of the second method

The second step is to find Tomcat's context.xml 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>

<Context>  
    <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource" />  
   <WatchedResource>WEB-INF/web.xml</WatchedResource>  
<Context>  

Advantages: reusability, one-time implementation Disadvantages: no controllability

Spring reference to JNDI data source

Add a bean in applicationContext.xml to replace the original dataSource

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/mysql" />  

Configuration of C3P0 data source

The values ​​of type and factory have changed

username=>user

url=>jdbcUrl

driverClassName=>driverClass

<Resource name="jdbc/mysql_c3p0" scope="Shareable"  
    type="com.mchange.v2.c3p0.ComboPooledDataSource"   
    factory="org.apache.naming.factory.BeanFactory"  
    jdbcUrl="jdbc:mysql://localhost:3306/test" driverClass="com.mysql.jdbc.Driver"  
    user="root" password="root" />  

This concludes this article about the three ways to configure JNDI data source for Tomcat. For more information about configuring JNDI data source for Tomcat, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to configure the maxPostSize value of built-in Tomcat in Spring Boot
  • Detailed steps for configuring springboot using external tomcat in idea
  • Solution for Tomcat to place configuration files externally
  • A brief discussion on the correct posture of Tomcat memory configuration
  • Detailed explanation of optimized configuration of Tomcat user management

<<:  Summary of clipboard.js usage

>>:  Web Theory: Don't make me think Reading Notes

Recommend

Complete steps for using Echarts and sub-packaging in WeChat Mini Program

Preface Although the holiday is over, it shows up...

JS+CSS to realize dynamic clock

This article example shares the specific code of ...

How to modify the mysql table partitioning program

How to modify the mysql table partitioning progra...

A brief discussion of several browser compatibility issues encountered

background Solving browser compatibility issues i...

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as...

Using js to achieve the effect of carousel

Today, let's talk about how to use js to achi...

Example of horizontal arrangement of li tags in HTMl

Most navigation bars are arranged horizontally as...

What are the benefits of using // instead of http:// (adaptive https)

//Default protocol /The use of the default protoc...

VMware Workstation download and installation detailed tutorial

Virtual machines are very convenient testing soft...

Shell script settings to prevent brute force ssh

The shell script sets access control, and the IP ...

Example of how to configure cross-domain failure repair in nginx

Nginx cross-domain configuration does not take ef...