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

Source: Internet
Author: User

collection (+)

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:

<Resource Name="Jdbc/testdb" 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 =Nulltry {future<connection> future = Datasource.getconnectionasync ();While (!future.isdone ()) {System.out.println ("Connection are 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;PublicClass Simplepojoexample{Public Static void Main(string[] args) ThrowsException {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 ();p. Setinitialsize ();p. Setmaxwait (10000);p. Setremoveabandonedtimeout ();p. Setminevictableidletimemillis (30000);p. Setminidle ();p. setlogabandoned (true);p. setremoveabandoned (true);p. Setjdbcinterceptors ("Org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" +"Org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");D atasource DataSource =New DataSource ();d atasource.setpoolproperties (p); Connection con =Nulltry {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

Using the Tomcat 7 new connection pool--tomcat JDBC 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.