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:
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