Connection pooling configuration for Tomcat+mysql databases

Source: Internet
Author: User
Tags auth connection pooling sql mysql resource stmt mysql database tomcat
mysql| Data | database
1 Download the Mm.mysql-2.0.14-bin.jar package and put it in the Common/lib folder under the Tomcat directory

2 in the Tomcat directory in the Conf/server.xml in the </Host> before the join (note: All uppercase places to replace your own things)

<!--for MySQL database connection pool--><context path= "/application_name" docbase= "Applicatioin_name" debug = "5" reloadable= "true" crosscontext= "true" >  <logger classname= "Org.apache.catalina.logger.FileLogger" Prefix= "Localhost_dbtest_log." suffix= ". txt" timestamp= "true"/>           <resource name= "Jdbc/database_name" auth= "Container" type= "Javax.sql.DataSource"/>            <resourceparams name= "Jdbc/database_name" >                       < parameter>                                <name>factory</ name>                                   <value> org.apache.commons.dbcp.basicdatasourcefactory</value>                       </parameter>                                     <!--Class name for mm.mysql JDBC driver- ->                      <parameter>                                      <name>driverClassName</name>                                        <value>org.gjt.mm.mysql.Driver</value>                       </ Parameter>                                  <!--the JDBC Connection URL for connecting to your MySQL db.                                              the Autoreconnect=true argument to The URL makes sure that the                                            mm.mysql JDBC Driver'll automatically reconnect if mysqld closed  & nbsp;                                    the connection.  mysqld By default closes idle connections after 8 hours.-->  nbsp;             <parameter>                                    <name>url</name>                                  <value>jdbc:mysql://localhost:3306/database_ name?autoreconnect=true</value>                    </parameter>                                   <!--MySQL db username and password for db connections -->     & nbsp;               <parameter>                                    <name>username</ name>                                    <value>DB_ username</value>                       </parameter>                       <parameter>                                     <name>password</name>                                   <value>DB_PASSWORD</value>                        </parameter>                                  <!--Maximum number of DB connections in pool. Make sure                                         Configure your mysqld max_connections large enough to handle       & nbsp;                                     All of your DB connections. Set to 0 for no limit.-->                    <parameter>  &nbsP                                  <name>maxactive</name >                                    <value> 100</value>                       </parameter>                                     <!--Maximum number of idle DB connections to retain in pool.                                        Set to 0 for no limit. -->                      <parameter>                                    <name>maxIdle</name>                                  <value>30</value>                    </parameter>                                 <!--Maximum A DB connection to become available in ms,       &nbsp ;                              in this example seconds. An Exception are thrown if this timeout                                       is exceeded.  Set to-1 to wait indefinitely.-->      & nbsp;               <parameter>                              <name>maxWait</name>                        <value>10000 </value>                      </parameter>           </resourceparams ></Context> so even if the connection is over

3) Test:

Database_name:testdb

Application_name:mysqltest

Create a new table in the TestDB database userinfo

ID | UserName1 AAA2 BBB

4) Web.xml

<web-app><resource-ref> <description>mysql Datasource example</description> < Res-ref-name>jdbc/testdb</res-ref-name> <res-type>javax.sql.DataSource</res-type> < Res-auth>container</res-auth> </resource-ref></web-app>

5) index.jsp

<%@ page contenttype= "text/html;charset=gb2312" language= "java" import= "java.sql.*,javax.naming.*"%>

<% context initctx = new InitialContext ();  Context CTX = (context) initctx.lookup ("java:comp/env");      Object obj = (object) ctx.lookup ("Jdbc/testdb");  Javax.sql.DataSource ds = (javax.sql.DataSource) obj;  Connection conn = Ds.getconnection ();  Statement stmt = Conn.createstatement ();  String strSQL = "SELECT * from UserInfo"; ResultSet rs = stmt.executequery (strSQL);%>

<body> <%while (Rs.next ()) {%> <p> <%=rs.getstring ("username")%> </p> <%}%>< Body>

You can see the output as:

aaabbb


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.