Configure MySQL database connection pool under Tomcat

Source: Internet
Author: User
Tags auth connection pooling sql mysql requires resource mysql database tomcat
mysql| Data | database | database connection
After a long day of practice and groping, and finally under Tomcat successfully configured MySQL database connection pool. Summarized as follows:

1. Add the configuration information to the $catalina_home/conf/server.xml, declare the connection pool specific information, add the following content:

<!--declaring connection pool-->

<resource name= "Jdbc/mysql" auth= "Container" type= "Javax.sql.DataSource"/>

<!--Set the parameters of the connection pool-->

<resourceparamsname= "Jdbc/mysql" >

<parameter>

<name>factory</name>

<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

</parameter>

<parameter>

<name>maxWait</name>

<value>5000</value>

</parameter>

<parameter>

<name>maxActive</name>

<value>20</value>

</parameter>

<parameter>

<name>username</name>

<value>shopadm</value>

</parameter>

<parameter>

<name>password</name>

<value>123</value>

</parameter>

<parameter>

<name>url</name> <value>jdbc:mysql://localhost/shopdb?useunicode=true&charact-erencoding= Gb2312</value>

</parameter>

<parameter>

<name>driverClassName</name>

<value>com.mysql.jdbc.Driver</value>

</parameter>

<parameter>

<name>maxIdle</name>

<value>10</value>

</parameter>

</ResourceParams>

2. Add the following information before the </web-app> of $catalina_home/conf/web.xml:

<resource-ref>

<description>db connection</description>

<res-ref-name>jdbc/mysql</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

The parameter names in <res-ref-name> must be the same as the connection names declared in Server.xml.



3. Locate the configuration information for the current program that requires a database connection in the $catalina_home/conf/catalina/localhost directory, such as Shopping.xml, and add the following information to this file:

<context ...>

...

<resourcelink name= "Jdbc/mysql" global= "Jdbc/mysql" type= "Javax.sql.DataSource"/>

...

</Context>



Done!



On this basis, refer to the Tomcat official website user manual, explore another way to configure the connection pool, this method does not need to modify the Server.xml, as long as the need to use the connection pool of the program's configuration document to modify it. The method is as follows:

1. $CATALINA _home/conf/catalina/localhost directory to find the configuration document for the program that requires the database connection pool, this is shopping.xml. Add the following information between <Context></Context> to declare a database connection pool:

<resource name= "Jdbc/mysql" auth= "Container" type= "Javax.sql.DataSource"/>



<resourceparams name= "Jdbc/mysql" >

<parameter>

<name>factory</name>

<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

</parameter>

<parameter>

<name>maxWait</name>

<value>5000</value>

</parameter>

<parameter>

<name>maxActive</name>

<value>20</value>

</parameter>

<parameter>

<name>password</name>

<value>123</value>

</parameter>

<parameter>

<name>url</name>

<value>jdbc:mysql://localhost/shopdb?useUnicode=true&characterEncoding=gb2312</value>

</parameter>

<parameter>

<name>driverClassName</name>

<value>com.mysql.jdbc.Driver</value>

</parameter>

<parameter>

<name>maxIdle</name>

<value>10</value>

</parameter>

<parameter>

<name>username</name>

<value>shopadm</value>

</parameter>

</ResourceParams>

The parameters here are almost exactly the same as the information added to the server.xml in the previous method.



2. Create a Web.xml document under the Web-inf of the corresponding program and add the following information:

<?xml version= "1.0" encoding= "Iso-8859-1"?>



<web-appxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee

Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "

version= "2.4" >

<resource-ref>

<description>DBConnection</description>

<res-ref-name>jdbc/mysql</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

</web-app>

The key is the Italic section, pointing to the connection pool that was declared earlier.



Both of these methods can be configured successfully, and obviously their scope is not the same. In addition, the configuration of the internal reference and operating mechanism, left to further study. The next step is to learn how to work together with database connection pooling and JSP.


Related Article

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.