Use the Tomcat 7 new connection pool--tomcat jdbc Pool_jndi

Source: Internet
Author: User

Tomcat in the previous version of 7.0 are using COMMONS-DBCP as a connection pool, but DBCP was criticized for the reasons: DBCP is single-threaded, in order to ensure that the line Cheng Ann Plenary lock the entire connection pool dbcp poor performance dbcp too complex, more than 60 classes DBCP Using static interfaces, compiling problems with JDK 1.6 dbcp development Lag

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

To this end, Tomcat introduced a new module starting with 7.0: Tomcat JDBC Pool tomcat JDBC Pool near-compatible DBCP, higher performance asynchronous way to get connection tomcat JDBC Pool is a module of Tomcat, based on t Omcat Juli, using Tomcat's log framework to get connections using the Javax.sql.PooledConnection interface support high concurrency application environment is very simple, core files only 8, better than C3P0 idle connection processing mechanism support JMX support XA C Onnection

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

The Tomcat JDBC pool can be used directly in Tomcat, or it can be used in stand-alone applications.

Methods that are used directly in Tomcat:

Data source configuration: <resource name= "Jdbc/testdb"       auth= "Container"        type= "Javax.sql.DataSource"       factory= " Org.apache.tomcat.jdbc.pool.DataSourceFactory "      testwhileidle=" true "06       testonborrow= "true"       testonreturn= False "      validationquery=" SELECT 1 "       Validationinterval= "30000"       timebetweenevictionrunsmillis= "30000" one        maxactive= "      minidle="        maxwait= "10000"       initialsize= "ten"        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=" Password "       Driverclassname= "Com.mysql.jdbc.Driver"       url= "jdbc:mysql://localhost:3306/ MySQL "/>

Methods for obtaining a connection asynchronously: Connection con = null; try {  Future<Connection> Future = datasource.ge Tconnectionasync ();   while  (!future.isdone ()) {      system.out.println (" Connection is not yet available. Do some background work ");       try {      thread.sleep (MB);  Simulate work       }catch  (Interruptedexception x) {a        thread.currentthread (). interrupted (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}      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; Modified import org.apache.tomcat.jdbc.pool.poolproperties; Modified   Public class simplepojoexample {  Ten     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");

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.