Actual configuration of MySQL connection pool

Source: Internet
Author: User
Tags mysql connection pool

The following article describes the actual configuration of the java jsp tomcat6 MySQL connection pool. I recently upgraded tomcat5.5 to tomcat6. I thought it was just a simple upgrade, but unexpectedly, the change between tomcat5 and tomcat6 was quite large.

Here I will talk about some questions I encountered in configuring the MySQL connection pool.

The most obvious change is that the directory structure of Tomcat 5 and tomcat 6 has changed, and the common directory is not found in Tomcat 6. Instead, a lib directory is directly available, next, let's take a look at how Tomcat 6 configures the connection pool.

1. required file: Installation (Installation File)

2. Configure the context. xml file in conf under tomcat, and add the connection pool between <context> </context> as follows:

 
 
  1. <Resource name="jdbc/MySQL"   
  2. auth="Container"   
  3. type="javax.sql.DataSource"   
  4. driverClassName="com.MySQL.jdbc.Driver"   
  5. url="jdbc:MySQL://localhost/test"   
  6. username="root"   
  7. password="root"   
  8. maxActive="100"   
  9. maxIdle="30"   
  10. maxWait="10000" />   

I don't need to talk about the above parameters. I know what it means.

3. Configure <web-app> </web-app> in the web. xml file of your application to add:

 
 
  1. Xml Code
  2. <Resource-ref>
  3. <Description> DB Connection </description>
  4. <Res-ref-name> jdbc/MySQLx </res-ref-name>
  5. <Res-type> javax. SQL. DataSource </res-type>
  6. <Res-auth> Container </res-auth>
  7. </Resource-ref>

4. the success is that you do not need to use the original server. after the configuration is in xml, you can write the test program below. There will be a lot on the internet, mainly on the top. Of course, you need to put the connection drivers under the lib under Tomcat 6. the test code is as follows:

Java code
 

 
 
  1. <! Doctype html public "-// w3c // dtd html 4.0 transitional // en"
  2. Http://www.w3.org/TR/REC-html40/strict.dtd>
  3. <% @ Page import = "java. SQL. *" %>
  4. <% @ Page import = "javax. SQL. *" %>
  5. <% @ Page import = "javax. naming. *" %>
  6. <% @ Page session = "false" %>
  7. <Html>
  8. <Head>
  9. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
  10. <Title> </title>
  11. <%
  12. Out. print ("My test started ");
  13. DataSource ds = null;
  14. Try {
  15. InitialContext ctx = new InitialContext ();
  16. Ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/MySQL ");
  17. Connection conn = ds. getConnection ();
  18. Statement stmt = conn. createStatement ();

Tip: users must be an existing database table,

The database contained in the Data Source URL configuration mentioned above.

 
 
  1. String strSql = "select * from users ";
  2. ResultSet rs = stmt.exe cuteQuery (strSql );
  3. While (rs. next ()){
  4. Out. print (rs. getString (1 ));
  5. }
  6. Out. print ("My test ended ");
  7. }
  8. Catch (Exception ex ){
  9. Out. print ("exception, message:" + ex. getMessage ());
  10. Ex. printStackTrace ();
  11. }
  12. %>
  13. </Head>
  14. <Body>
  15. </Body>
  16. </Html>

The above content is an introduction to the configuration of the java jsp tomcat6 MySQL connection pool. I hope you will gain some benefits.

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.