Basicdatasource Pool Configuration Properties
Pool Configuration Properties |
The specified content |
InitialSize |
Number of connections created at pool startup |
Maxactive |
The maximum number of connections that can be allocated from the pool at the same time. When set to 0, it means no limit. |
Maxidle |
The maximum number of idle connections that will not be released in the pool. When set to 0, it means no limit. |
Maxopenpreparedstatements |
The maximum number of prepared statements that can be allocated from the statement pool at the same time. When set to 0, it means no limit. |
Maxwait |
The maximum time the pool waits for a connection to be reclaimed before throwing an exception (when there is no available connection). Set to 1 to indicate an infinite wait. |
Minevictableidletimemillis |
The maximum amount of time a connection remains idle without being evicted. |
Minidle |
The minimum number of connections that remain idle in the pool without creating a new connection. |
Poolpreparedstatements |
Whether the prepared statement is pooled (a Boolean value). |
Jdbc.properties file
Jdbc.driverclassname=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/vbox? Rewritebatchedstatements=true&characterencoding=utf8jdbc.username=rootjdbc.password=root#<!--initialsize: Initializing the connection -jdbc.initialsize=10#<!--maxactive: Maximum number of connections -jdbc.maxactive=500tcp. port=28577#<!--set the interval for how many milliseconds -baidu.pinglv=5#<!--Initializing a connection -datasource.initialsize=10#<!--Maximum idle connection -datasource.maxidle=20#<!--Minimum idle connection -datasource.minidle=5#<!--Maximum number of connections -datasource.maxactive=50#<!--whether to print the connection time-out error when the time-out connection is automatically reclaimed -datasource.logabandoned=true#<!--whether to automatically reclaim timeout connections -datasource.removeabandoned=true#<!--time-out (in seconds) -datasource.removeabandonedtimeout=180#<!--timeout wait time in milliseconds of 6000 milliseconds/1000 equals 60 seconds -datasource.maxwait=1000#<!--whether validation is performed when the connection is made (that is, if the database is still connected) -datasource.testonborrow=true#<!--whether validation is performed when the connection is returned (that is, if the database is also connected) -datasource.testonreturn=true#<!--whether validation is performed when the connection is idle (that is, if the database is still connected) -datasource.testwhileidle=true#<!--their two mates can continuously update connection objects in the connection pool when Timebetweenevictionrunsmillis is greater than 0 o'clock, -#<!--every timebetweenevictionrunsmillis time, a thread is started to verify connection objects that have been idle for more than Minevictableidletimemillis in the connection pool. -datasource.timebetweenevictionrunsmillis=3datasource.minevictableidletimemillis=12
Writing for Java classes:
/*** HttpClient Practice * studyhttpclient * cn.yuchao.httpclient * Basicdatasourcedemo.java * Creator: Yuchao * Time: 2015-11-6-Am 09:5 6:03 * 2015-All rights reserved*/ Packagecn.yuchao.httpclient;Importjava.io.IOException;Importjava.sql.Connection;Importjava.sql.SQLException;Importjava.util.Properties;ImportOrg.apache.commons.dbcp.BasicDataSource;Importorg.apache.commons.lang.StringUtils;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;/*** * Basicdatasourcedemo * Created by: Yuchao * Time: 2015-11-6-Morning 09:56:03 *@version1.0.0 **/ Public classBasicdatasourcedemo {Private Static FinalLogger log = Loggerfactory.getlogger (Basicdatasourcedemo.class); Public Static voidMain (string[] args) {Properties dbprops=NewProperties (); Connection Connection=NULL; Try{dbprops.load (basicdatasourcedemo).class. getResourceAsStream ("/jdbc.properties")); System.setproperty ("Javax.xml.parsers.DocumentBuilderFactory", "Com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl "); String Driverclassname= Dbprops.getproperty ("Jdbc.driverclassname"); String URL= Dbprops.getproperty ("Jdbc.url"); String username= Dbprops.getproperty ("Jdbc.username"); String Password= Dbprops.getproperty ("Jdbc.password"); String InitialSize= Dbprops.getproperty ("Datasource.initialsize"); String Minidle= Dbprops.getproperty ("Datasource.minidle"); String Maxidle= Dbprops.getproperty ("Datasource.maxidle"); String maxwait= Dbprops.getproperty ("datasource.maxwait"); String maxactive= Dbprops.getproperty ("datasource.maxactive"); String Timebetweenevictionrunsmillis=dbprops.getproperty ("Datasource.timebetweenevictionrunsmillis"); String Minevictableidletimemillis=dbprops.getproperty ("Datasource.minevictableidletimemillis"); //whether validation is performed when the connection is made (that is, if the database is still connected) BooleanTestonborrow = boolean.valueof (Dbprops.getproperty ("Datasource.testonborrow", "true") . Booleanvalue (); //whether validation is performed when the connection is returned (that is, if the database is also connected) BooleanTestonreturn = boolean.valueof (Dbprops.getproperty ("Datasource.testonreturn", "true") . Booleanvalue (); //whether validation is performed when the connection is idle (that is, if the database is still connected) BooleanTestwhileidle = boolean.valueof (Dbprops.getproperty ("Datasource.testwhileidle", "true") . Booleanvalue (); //whether to print the connection time-out error when the time-out connection is automatically reclaimed Booleanlogabandoned = boolean.valueof (Dbprops.getproperty ("datasource.logabandoned", "false") . Booleanvalue (); //whether to automatically reclaim timeout connections Booleanremoveabandoned = (boolean.valueof (Dbprops.getproperty ("datasource.removeabandoned", "false")) . Booleanvalue (); //time-out (in seconds) intRemoveabandonedtimeout = Integer.parseint (Dbprops.getproperty ("Datasource.removeabandonedtimeout", "300")); Basicdatasource DataSource=NewBasicdatasource (); Datasource.setdriverclassname (Driverclassname); Datasource.seturl (URL); Datasource.setusername (username); Datasource.setpassword (password); //Initialize the number of connections if(Stringutils.isnotblank (initialsize)) {datasource.setinitialsize (Integer.parseint (initialsize)); } //minimum number of idle connections if(Stringutils.isnotblank (Minidle)) {Datasource.setminidle (Integer.parseint (Minidle)); } //Maximum number of idle connections if(Stringutils.isnotblank (Maxidle)) {Datasource.setminidle (Integer.parseint (Maxidle)); } //Time-out payback (in milliseconds) if(Stringutils.isnotblank (maxwait)) {Datasource.setminidle (Integer.parseint (Minidle)); } //Maximum number of connections if(Stringutils.isnotblank (maxactive)) {Datasource.setminidle (Integer.parseint (Minidle)); } if(Stringutils.isnotblank (Minevictableidletimemillis)) {}//whether to print the connection time-out error when the time-out connection is automatically reclaimeddatasource.setlogabandoned (logabandoned); //whether to automatically reclaim timeout connectionsdatasource.setremoveabandoned (removeabandoned); //time-out (in seconds)datasource.setremoveabandonedtimeout (removeabandonedtimeout); //whether validation is performed when the connection is made (that is, if the database is still connected)Datasource.settestonborrow (Testonborrow); //whether validation is performed when the connection is returned (that is, if the database is also connected)Datasource.settestonreturn (Testonreturn); //whether validation is performed when the connection is idle (that is, if the database is still connected)Datasource.settestwhileidle (Testwhileidle); if(Stringutils.isnotblank (Timebetweenevictionrunsmillis)) {Datasource.settimebetweenevictionrunsmill Is (Long.parselong (Timebetweenevictionrunsmillis)); } /*** They are two mates that can continuously update connection objects in the connection pool when Timebetweenevictionrunsmillis is greater than 0 o'clock, every timebetweenevictionrunsmillis time, * A thread is started to verify connection objects that have been idle for more than Minevictableidletimemillis in the connection pool. * */ if(Stringutils.isnotblank (Timebetweenevictionrunsmillis)) {Datasource.settimebetweenevictionrunsmill Is (Long.parselong (Timebetweenevictionrunsmillis)); } if(Stringutils.isnotblank (Minevictableidletimemillis)) {Datasource.setminevictab Leidletimemillis (Long.parselong (Minevictableidletimemillis)); } Try{Connection=datasource.getconnection (); System.out.println ("Get the connection from the Basicdatasource pool:" +connection); } Catch(SQLException e) {e.printstacktrace (); Log.info ("Sorry to connect to the database failed, please check your configuration ..."); } } Catch(IOException e) {e.printstacktrace (); }finally{ Try { if(NULL!=connection) { //releasing the connection to the Basicdatasource poolConnection.close (); } } Catch(SQLException e) {e.printstacktrace (); } } }}
Basicdatasource Pool Configuration Properties