Configure the Tomcat server data connection pool

Source: Internet
Author: User

Advantage: Because the HTTP protocol Web program is stateless, when the application uses JDBC, multiple request processing clients will reestablish the connection and will consume a lot of resources if the request is busy.

The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing it.

To configure a global database connection pool:

To configure a local database connection pool:

In the Tomcat installation directory, Conf\server.xml

<GlobalNamingResources> under Tags

Add to

<resource name= "jdbc/webdb"              auth= "Container"      type= "Javax.sql.DataSource"             driverclassname= " Com.mysql.jdbc.Driver "             url=" Jdbc:mysql://localhost:3306/shop?characterencoding=utf-8 "             username=" root "              password= "root"              maxactive= "maxidle="              maxwait= "    />"

In Conf\catalina\localhost

To create a web-named XML

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><context Path = "/webdemo" docbase= "Webdemo" debug= "0" >< ResourceLink name= "jdbc/webdb" global= "jdbc/webdb" type=  "Javax.sql.DataSource"/></context>

Create a servlet

Package Com.demo.servlet;import Java.io.ioexception;import Java.io.printwriter;import java.sql.Connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Javax.naming.context;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.sql.datasource;public class Helloworldservlet extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexception{//Sets the ContentType field value of the HTTP response header Response.setcontenttype ("text/html; Charset=utf-8 ");//Gets the PrintWriter object used to input the output message printwriter out = Response.getwriter (); try {//or Context object Context context = New InitialContext ();D atasource ds = (DataSource) context.lookup ("java:/comp/env/jdbc/webdb");// Get Connetion Object Connection connection = Ds.getconnection (); PreparedStatement statement = Connection.prepArestatement ("SELECT * from Memberlevel"); ResultSet rs = Statement.executequery (); StringBuffer table = new StringBuffer () table.append ("<table border= ' 1 ' >"); Table.append ("<tr><td> Id</td><td> level </td><td> points </td></tr> "), while (Rs.next ()) {Table.append (" <tr ><td> "+rs.getstring" ("ID") + "</td><td>" +rs.getstring ("levelname") + "</td><td>" + Rs.getstring ("favourable") + "</td></tr>");} Table.append ("</table>"); Out.println (table.tostring ());} catch (Exception e) {out.println (E.getmessage ());}}}

Add the MySQL jar package to Lib

Web. XML configuration

After the request

Http://localhost:8080/webdemo/servlet/HelloWorldServlet

Results:

Configure the local database connection pool to replace the contents of the Web-named XML

<context Path = "/webdemo" docbase= "Webdemo" debug= "0" >
。。。。。。
Configuration
</Context>

Configure the Tomcat server data connection pool

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.