1. Database Connection Pool
Dbcp
Apache's Products
The Core class: Basicdatasource
commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
http://commons.apache.org/proper/commons-pool/download_pool.cgi
C3p0
Spring/hibernet
Currently the most widely used.
have free auto-recycle link function
Executes asynchronously.
https://sourceforge.net/projects/c3p0/
The Core class: Combopooleddatasource
Config file: must be called C3p0-config.xml
Druid
Tomcat Configuring connection Pooling
DBCP Configuring the database connection pool, you need to import two jar packages, namely Commons-pool-1.6.jar and Commons-dbcp-1.4.jar
//Method One, set connection parameters by string
Public classJDBCUtil1 {Private StaticBasicdatasource ds =NULL; Private StaticString url= "Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8"; Private StaticString user= "Root"; Private StaticString password= "123123"; Private StaticString driverclass= "Com.mysql.jdbc.Driver"; Static{DS=NewBasicdatasource (); //The most basic link configurationds.seturl (URL); Ds.setusername (user); Ds.setpassword (password); Ds.setdriverclassname (Driverclass); //Initialize the number of connectionsDs.setinitialsize (5); //Maximum number of connectionsDs.setmaxactive (10); //Maximum wait timeDs.setmaxwait (3000); } Public StaticConnection getconnection () {Try { returnds.getconnection (); } Catch(SQLException e) {e.printstacktrace (); } return NULL; } }
//Method Two read the connection parameters through the configuration file
Public classJdbcutil {Private StaticBasicdatasource ds =NULL; Static{ //key in properties cannot be written in a random//by Basicdatasource the set method to get the key, remove the set, the first letter lowercase. Properties prop =NewProperties ();
Read the configuration file, the configuration file is below InputStream InputStream= Jdbcutil.class. getResourceAsStream ("/db.properties"); Try{prop.load (InputStream); DS=(Basicdatasource) Basicdatasourcefactory.createdatasource (prop); } Catch(Exception e) {e.printstacktrace (); } } Public StaticConnection getconnection () {Try { returnds.getconnection (); } Catch(SQLException e) {e.printstacktrace (); } return NULL; }}
Profile Name Db.properties
Url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8username= rootpassword=123123driverclassname=com.mysql.jdbc.DriverinitialSize=5maxactive =10maxwait=3000
C3P0 Configuration database connection pool, import C3p0-0.9.5.2.jar and Mchange-commons-java-0.2.11.jar required
Configuration Method One:
Public classJDBCUtil1 {Private StaticCombopooleddatasource ds =NULL; Private StaticString url= "Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8"; Private StaticString user= "Root"; Private StaticString password= "123123"; Private StaticString driverclass= "Com.mysql.jdbc.Driver"; Static{DS=NewCombopooleddatasource (); //Basic Configurationds.setjdbcurl (URL); Ds.setuser (user); Ds.setpassword (password); Try{ds.setdriverclass (driverclass); } Catch(propertyvetoexception e) {e.printstacktrace (); } ds.setinitialpoolsize (5); Ds.setmaxpoolsize (10); //setting the time-out periodDs.setcheckouttimeout (3000); } Public StaticConnection getconnection () {Try { returnds.getconnection (); } Catch(SQLException e) {e.printstacktrace (); } return NULL; } }
Configuration mode two: By reading the configuration file C3p0-config.xml, the configuration file must be named unique, can only be called C3p0-config.xml
Configuration file Information
<?xml version= "1.0" encoding= "UTF-8"?><c3p0-config> <!--default configuration-<default-config> <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/test</property><property name= "User" >root</property> <property name= "password" >123123</property> &L T;property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Initialpoolsize" >5& lt;/property> <property name= "maxpoolsize" >10</property> <property name= "Checkouttimeout" & Gt;3000</property> </default-config> <!----> <named-config name= "Intergalactoapp" > <property name= "Jdbcurl" >jd Bc:mysql://localhost:3306/test</property><property name= "User" >root</property> <property name= "password" >123123</property> &L T;property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Initialpoolsize" >10 </property> <property name= "maxpoolsize" >20</property> <property name= "Checkouttimeout" >5000</property> </named-config></c3p0-config>
Code Demonstration
Public classJdbcutil {Private StaticCombopooleddatasource ds =NULL; Static { //it reads default-config.//ds = new Combopooleddatasource (); //read <named-config name= "Intergalactoapp" >DS =NewCombopooleddatasource ("Intergalactoapp"); } Public StaticConnection getconnection () {Try { returnds.getconnection (); } Catch(SQLException e) {e.printstacktrace (); } return NULL; }}
Database connection Pooling Technology