TOMCAT7 New database connection pool tomcat JDBC Pool introduction and configuration

Source: Internet
Author: User
Tags connection pooling

Tomcat used COMMONS-DBCP as the implementation of connection pooling in versions prior to 7.0, but there are some problems with DBCP:
(1) DBCP is single-threaded, in order to ensure that the line Shuo plenary lock the entire connection pool
(2) Poor performance of DBCP
(3) DBCP is too complex, more than 60 classes, development lag.
As a result, other high-performance connection pools, such as C3P0, and Druid of the Ali system, are often used in the Java EE. To do this, Tomcat introduced a new module starting from 7.0: Tomcat JDBC Pool
Tomcat JDBC Pool is nearly compatible with DBCP for higher performance
Get a connection asynchronously
Tomcat JDBC Pool is a module of Tomcat, based on Tomcat Juli, using Tomcat's log framework
Getting a connection using the Javax.sql.PooledConnection interface
Supports high concurrent application environments
Super simple, only 8 core files, less than c3p0
A better idle connection processing mechanism
Support JMX
Support for XA Connection.
The Tomcat JDBC pool can be used directly in Tomcat or in standalone applications.

Methods for direct use in 1.Tomcat:
Configure resource in the <GlobalNamingResources> node under Conf/server.xml, for example:

<resource name= "Jdbc/ens"      auth= "Container"      type= "Javax.sql.DataSource"      factory= " Org.apache.tomcat.jdbc.pool.DataSourceFactory "      testwhileidle=" true "      testonborrow=" true "      Testonreturn= "false"      validationinterval= "30000"      timebetweenevictionrunsmillis= "30000"      maxactive= " "      minidle=" ten "      maxwait=" 10000 "      initialsize=" "      removeabandonedtimeout=      " Removeabandoned= "true"      logabandoned= "true"      minevictableidletimemillis= "30000"      jmxenabled= "true"      jdbcinterceptors= "Org.apache.tomcat.jdbc.pool.interceptor.connectionstate;o Rg.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer "      username=" root "      password=" 123 "      Driverclassname= "Com.mysql.jdbc.Driver"      url= "Jdbc:mysql://localhost:3306/ens"/>

Then, add the following configuration to the <Context></Context> node of the context.xml file:

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

The value of the parameter in global= "Jdbc/ens" ("Jdbc/ens") must remain the same as the value of the Name property in the previous <resource > configuration. Name= "Jdbc/ens" can be arbitrarily taken, but when invoked in a program, it should be consistent with the value of name. Here, the connection pool is already configured.

Test it with a JSP:

<%@ page language= "java" pageencoding= "GBK"%><% @page import= "java.sql.Connection"%><% @page import= " Javax.naming.Context "%><% @page import=" Javax.naming.InitialContext "%><% @page import=" Javax.sql.DataSource "%><% @page import=" java.sql.Statement "%><% @page import=" Java.sql.ResultSet "%>    <%//Connection pool GET Connection conn = null;    DataSource ds = null;    ResultSet RS =null;    Statement stmt = null;    Context initctx = new InitialContext ();   ds = (DataSource) initctx.lookup ("Java:comp/env/jdbc/ens");         if (ds!=null) {out.println ("already obtained datasource!");        Out.println ("<br>");       conn = Ds.getconnection ();        try{stmt = Conn.createstatement ();        String sql = "SELECT * from Ens_area";        rs = stmt.executequery (SQL);            Out.println ("The following is the data read from the database:<br>");                while (Rs.next ()) {out.println ("<br>");            Out.println (rs.getstring ("Area_name")); }}CATCH (Exception ex) {ex.printstacktrace ();          }finally{Conn.close ();          Rs.close ();       Stmt.close (); }}%>



Http://www.open-open.com/lib/view/open1365991769687.html

TOMCAT7 New database connection pool tomcat JDBC Pool introduction and configuration

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.