Jndi Learning Summary (ii) Configuring JNDI data sources using C3P0 under--tomcat

Source: Internet
Author: User

First, c3p0 download

C3p0:http://sourceforge.net/projects/c3p0/files/?source=navbar

  

A compressed package is obtained after the download is complete.

Ii. Configuring a Jndi data source using C3P0

When configuring a Jndi data source in tomcat6.x, the default is to use the tomcat6.x dbcp connection pool, tomcat6.x Configure the Jndi data source with the DBCP connection pool as follows:

1 <resource  2     name= "Oracledatasource"  3     auth= "Container"  4     type= " Javax.sql.DataSource "  5     maxactive="  6 maxidle= "  7     maxwait=" 10000 "  8     username= "Lead_oams"  9     password= "P"     driverclassname= "Oracle.jdbc.OracleDriver"     Url= "Jdbc:oracle:thin:@192.168.1.229:1521:lead"/>

If you want tomcat6.x to configure the Jndi data source using the C3P0 connection pool, the following configuration items need to be modified when configured

1. The values of type and factory vary

2, Username=>user

3, Url=>jdbcurl

4, Driverclassname=>driverclass

Create a Web test project C3p0_jndi_config, unzip the package, and locate the relevant jar package in the C3p0-0.9.5-pre9\lib directory as shown in:

  

Add the C3P0 related jar package to the project, create a context.xml file in the project's Meta-inf directory, as shown in the directory structure:

  

Add the drive jar packages for Oracle, MySQL, SQL Server three databases under the Lib directory of the Tomcat servers, as shown in:

  

1, add the following configuration information in the Context.xml file

 1 <Context> 2 <!--use C3P0 to configure Jndi data sources for MySQL databases--3 <resource 4 Name= "Jdbc/mysqldatasourc E "5 auth=" Container "6 factory=" org.apache.naming.factory.BeanFactory "7 type=" com.mchange.v2.c 3p0. Combopooleddatasource "8 driverclass=" Com.mysql.jdbc.Driver "9 idleconnectiontestperiod=" ten Max Poolsize= "minpoolsize=" 2 "acquireincrement=" 2 "user=" root "password=" root "1 5 jdbcurl= "Jdbc:mysql://192.168.1.144:3306/leadtest"/>16 <!--using C3P0 to configure JNDI data sources for Oracle databases--& gt;18 <resource name= "Jdbc/oracledatasource" auth= "Container" factory= Aming.factory.BeanFactory "type=" Com.mchange.v2.c3p0.ComboPooledDataSource "driverclass=" ORACLE.JDBC. Oracledriver "idleconnectiontestperiod=" "Maxpoolsize=" "2" acqu Ireincrement= "2" 28        Jdbcurl= "Jdbc:oracle:thin:@192.168.1.229:1521:lead" user= "Lead_oams" and password= "P"/>31 <!--using C3P0 to configure JNDI data sources for SQL Server databases-->34 <resource name= "Jdbc/sqlserverdatasou Rce "auth=" Container "Notoginseng factory=" org.apache.naming.factory.BeanFactory "type= com.mchange.v2". C3p0. Combopooleddatasource "driverclass=" Com.microsoft.sqlserver.jdbc.SQLServerDriver "Idleconnectiontestpe riod= "maxpoolsize=" "minpoolsize=" 2 "acquireincrement=" 2 "jdbcurl=" Lserver://192.168.1.51:1433;databasename=demo "user=" sa "password=" [email protected] "/>47 &lt ;/context>

2. Reference the Jdni data source in Web. xml:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app version= "2.5" 3 xmlns= "http://java.sun.com/xml/ns/javae E "4 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "5 xsi:schemalocation=" Http://java.sun.com/xml/ns/java EE 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> 7 <welcome-file-list> 8 <welcome-file>i Ndex.jsp</welcome-file> 9 </welcome-file-list>10 <!--resource reference for JNDI configuration: 13 Res-ref-name: Presentation With the name of the resource 14 Res-type: This resource corresponds to a type of JAVAX.SQL.DATASOURCE15 Res-auth: Container Authorization Management--&GT;17 <!--Oracle database Jndi data Source reference--&gt ; <resource-ref>19 <description>oracle DB connection</description>20 <res-ref-name&gt ; jdbc/oracledatasource</res-ref-name>21 <res-type>javax.sql.datasource</res-type>22 <res- Auth>container</res-auth>23 </resource-ref>24 <!--mysql database jndi data-->26 <resource-ref> <description>mysql DB connection</description>28 <res-ref-name>jdbc/mysqldatasource</res-ref-name>29 <res-type>javax.sql.datasource</res-type>30 <res-auth>container</res-auth>31 </ resource-ref>32 <!--SQL Server database Jndi data source reference-->34 <resource-ref>35 <description>sqlserve R DB connection</description>36 <res-ref-name>jdbc/sqlserverdatasource</res-ref-name>37 < res-type>javax.sql.datasource</res-type>38 <res-auth>container</res-auth>39 </ Resource-ref>40 </web-app>

3. Deploy the C3p0_jndi_config Web app to the Tomcat server test JNDI data source

  

After you deploy the WebApps directory to the Tomcat server, the Tomcat server automatically generates a C3p0_jndi_config.xml file in the \conf\catalina\localhost directory, as shown in:

  

The content in the C3p0_jndi_config.xml file is what we configured in the context.xml file of the Meta-inf directory.

The JSP test page is as follows:

 1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <%--introduced jstl tag library--%> 3 <%@ taglib u Ri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%> 4 <%@ taglib uri= "http://java.sun.com/jsp/jstl/sql" prefix = "SQL"%> 5 <!   DOCTYPE html> 6 

C3P0 Auto-re-connect related parameters
IDLECONNECTIONTESTPERIOD:C3P0 will have a task to detect if the connection within the pool is normal, and this parameter is how often the task runs. The default value is 0, which means no detection
Acquireretryattempts:pool number of retries after requesting a new connection failure
C3P0 currently has a problem:
When the database restarts, C3P0 does not automatically reinitialize the database connection pool, when the new request needs to access the database, the return error (because the database restarts, the connection fails), while refreshing the database connection pool, discard the failed connection, when the second request comes back to normal.
C3P0 currently does not provide a parameter for the number of retries after a failed connection has been established, only the parameters (acquireretryattempts) for the number of retries after a new connection failure is obtained.
To resolve this issue, you can set the Idleconnectiontestperiod parameter tradeoff, which is to set the system to automatically check the connection pool in the connection is a normal frequency parameter, the time unit is seconds.

Category: Jndi Tags: jndi summary

Jndi Learning Summary (ii) Configuring JNDI data sources using C3P0 under--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.