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:
- DBCP is single-threaded, in order to ensure that the entire connection pool is locked by the line Shuo plenary
- DBCP Poor performance
- DBCP is too complex, more than 60 classes
- DBCP using static interface, compile in JDK 1.6 problem
- 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
- 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, the core file only 8, than C3P0 Also
- A better idle connection processing mechanism
- Support JMX
- 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