Tomcat Three Ways to Configure JNDI Data Sources
- 2021-11-01 05:33:33
- OfStack
In the course of my past work, the development server 1 was generally Tomcat
The configuration of data source is usually to configure one bean of dataSource in applicationContext. xml
Then modify the JNDI configuration at deployment time
I guess it's because the configuration of Tomcat needs to be changed to the configuration file
Unlike servers such as JBoss and Weblogic, JNDI data sources can be added directly in the management interface
Few people have studied its configuration
Recently did a small project, when the release version is compiled through ant jar package and then thrown to the test
Is the test uncle, taught him to modify the data source or when he didn't hear it
Weekend idle to bored, looked at a few Tomcat configuration tutorials, the following do some summary
Note: If your project is dropped directly under webapps, there is no Context node corresponding to the project in server. xml
Update: As some previous configurations come from the network, they are not very easy to use, so some updates have been made
A few personal evaluations were made for each method
PS: The following configuration has been tested under apache-tomcat-6. 0.35 and can access the database
First, a single application has exclusive data sources
In step 1, find server. xml of Tomcat, find Context node of the project, 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
Second, configure the global JNDI data source and apply it to a single application
In two steps
Step 1: Find the GlobalNamingResources node in server. xml of Tomcat, 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>
Step 2: Find the project Context node to apply this JNDI data source, and add the 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 and controllability
Disadvantages: Compared with the third method, the configuration is 1 point cumbersome, and every project has to be equipped
Third, configure the global JNDI data source and apply it to all applications deployed under Tomcat
It is also divided into two steps
Step 1
Refer to Step 1 of Type 2
Step 2: Find context. xml of Tomcat, and add an ResourceLink node under Context node to reference the data source configured in Step 1
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 and firmness are in place
Disadvantages: No controllability
Spring Reference to JNDI Data Source
Add an bean to 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 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" />