Using the Tomcat 7 new connection pool--tomcat JDBC Pool

Source: Internet
Author: User

Tomcat has been using COMMONS-DBCP as a connection pool for the previous version of 7.0, but DBCP has been criticised for the following reasons:

    1. DBCP is single-threaded, in order to ensure that the entire connection pool is locked by the line Shuo plenary
    2. DBCP Poor performance
    3. DBCP is too complex, more than 60 classes
    4. DBCP using static interface, compile in JDK 1.6 problem
    5. DBCP Development Lag

So many people will choose some third-party connection pool components, such as C3P0, BONECP, Druid (@wenshao) and so on.

To do this, Tomcat introduced a new module starting from 7.0: Tomcat JDBC Pool

    1. Tomcat JDBC Pool is nearly compatible with DBCP for higher performance
    2. Get a connection asynchronously
    3. Tomcat JDBC Pool is a module of Tomcat, based on Tomcat Juli, using Tomcat's log framework
    4. Getting a connection using the Javax.sql.PooledConnection interface
    5. Supports high concurrent application environments
    6. Super simple, the core file only 8, than C3P0 Also
    7. A better idle connection processing mechanism
    8. Support JMX
    9. Support for XA Connection

The advantages of the Tomcat JDBC pool are much more than this, see here for details.

The Tomcat JDBC pool can be used directly in Tomcat or in standalone applications.

Methods for direct use in Tomcat:

Data Source configuration:

Auth= "Container"

Type= "Javax.sql.DataSource"

factory= "Org.apache.tomcat.jdbc.pool.DataSourceFactory"

Testwhileidle= "true"

Testonborrow= "true"

Testonreturn= "false"

validationquery= "SELECT 1"

Validationinterval= "30000"

Timebetweenevictionrunsmillis= "30000"

Maxactive= "100"

Minidle= "10"

Maxwait= "10000"

Initialsize= "10"

removeabandonedtimeout= "60"

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= "Password"

Driverclassname= "Com.mysql.jdbc.Driver"

Url= "Jdbc:mysql://localhost:3306/mysql"/>

Ways to get a connection asynchronously:

Connection con = null;

try {

Future future = Datasource.getconnectionasync ();

while (!future.isdone ()) {

System.out.println ("Connection is not yet available. Do some background work ");

try {

Thread.Sleep (100); Simulate work

}catch (Interruptedexception x) {

Thread.CurrentThread (). interrupted ();

}

}

con = Future.get (); should return instantly

Statement st = Con.createstatement ();

ResultSet rs = st.executequery ("SELECT * from user");

Used in stand-alone applications:

Import java.sql.Connection;

Import Java.sql.ResultSet;

Import java.sql.Statement;

Import Org.apache.tomcat.jdbc.pool.DataSource;

Import org.apache.tomcat.jdbc.pool.PoolProperties;

public class Simplepojoexample {

public static void Main (string[] args) throws Exception {

Poolproperties p = new Poolproperties ();

P.seturl ("Jdbc:mysql://localhost:3306/mysql");

P.setdriverclassname ("Com.mysql.jdbc.Driver");

P.setusername ("root");

P.setpassword ("password");

P.setjmxenabled (TRUE);

P.settestwhileidle (FALSE);

P.settestonborrow (TRUE);

P.setvalidationquery ("SELECT 1");

P.settestonreturn (FALSE);

P.setvalidationinterval (30000);

P.settimebetweenevictionrunsmillis (30000);

P.setmaxactive (100);

P.setinitialsize (10);

P.setmaxwait (10000);

P.setremoveabandonedtimeout (60);

P.setminevictableidletimemillis (30000);

P.setminidle (10);

P.setlogabandoned (TRUE);

P.setremoveabandoned (TRUE);

P.setjdbcinterceptors ("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;") +

"Org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

DataSource DataSource = new DataSource ();

Datasource.setpoolproperties (P);

Connection con = null;

try {

con = datasource.getconnection ();

Statement st = Con.createstatement ();

ResultSet rs = st.executequery ("SELECT * from user");

int cnt = 1;

while (Rs.next ()) {

System.out.println ((cnt++) + ". Host: "+rs.getstring (" host ") +

"User:" +rs.getstring ("user") + "Password:" +rs.getstring ("Password"));

}

Rs.close ();

St.close ();

} finally {

if (con!=null) try {con.close ();} catch (Exception ignore) {}

}

}

}

The above is just a simple introduction, the specific use of the method also need to refer to:

Https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

Citation: http://www.oschina.net/question/12_36910

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.