As I've said before, DBCP can be used for database connection pooling management. Another technology c3p0 can also be used for database connection pool management, where spring and other frameworks are based on C3P0 technology for database connection pool management.
Before using the C3p0-0.9.5.2.jar and Mchange-commons-java-0.2.11.jar packages, the main classes are combopooleddatasource, and there are two ways to set them up. One is set in the code, and one is set in the configuration file. The main difference is that this approach has only one major class Combopooleddatasource.
They may have different method names, but the functionality is the same.
1) Configure it in the code as follows
Combopooleddatasource DataSource =NewCombopooleddatasource (); Try{Datasource.setjdbcurl ("jdbc:mysql:///mydb?usessl=true"); Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setuser ("Root"); Datasource.setpassword ("123456"); Datasource.setmaxidletime ( -); Datasource.setmaxpoolsize ( -); Datasource.setinitialpoolsize (5); Connection Conn=datasource.getconnection (); String SQL="SELECT * from user where id=?"; Queryrunner qr=NewQueryrunner (); User User= Qr.query (conn, SQL,NewBeanhandler<user> (User.class),2); System. out. println (user); Datasource.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }
2) Set up in the configuration file
Note: Configuration file naming is C3p0-config.xml. The system defaults back to the CLASSPATH, web-inf/classes folder to search for configuration files, so put him in the SRC directory.
Combopooleddatasource DataSource =NewCombopooleddatasource ("Mysql_config"); Configuration using the specified configuration nameTry{Connection conn=datasource.getconnection (); String SQL="SELECT * from user where id=?"; Queryrunner qr=NewQueryrunner (); User User= Qr.query (conn, SQL,NewBeanhandler<user> (User.class),2); System. out. println (user); Datasource.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }
The parameters that can be set in the configuration file are as follows:
acquireincrement acquireretryattempts Acquireretrydelay Autocommitonclose Automatictesttable breakafteracquirefailure checkouttimeout Connectioncustomizerclassname Connectiontesterclassname Contextclassloadersource DataSourceName Debugunreturnedconnectionstacktraces Driverclass Extensions |
factoryclasslocation Forceignoreunresolvedtransactions Forcesynchronouscheckins Forceusenameddriverclass Idleconnectiontestperiod initialpoolsize Jdbcurl maxadministrativetasktime Maxconnectionage MaxIdleTime maxidletimeexcessconnections maxpoolsize maxstatements maxstatementsperconnection |
minpoolsize numhelperthreads overridedefaultuser Overridedefaultpassword Password Preferredtestquery privilegespawnedthreads propertycycle Statementcachenumdeferredclosethreads Testconnectiononcheckin Testconnectiononcheckout unreturnedconnectiontimeout User |
Configuration file samples such as the following
<c3p0-config> <default-config> <property name="Checkouttimeout"> the</property> <property name="Idleconnectiontestperiod"> -</property> <property name="initialpoolsize">5</property> <property name="MaxIdleTime"> -</property> <property name="maxpoolsize"> -</property> <property name="minpoolsize">5</property> <user-overrides user="Test-user"> <property name="maxpoolsize">Ten</property> <property name="minpoolsize">1</property> <property name="maxstatements">0</property> </user-overrides> </default-config> <named-config name="Mysql_config"> <property name="initialpoolsize">5</property> <property name="Jdbcurl">jdbc:mysql://localhost:3306/mydb?usessl=true</property><property name="Driverclass">com.mysql.jdbc.Driver</property> <property name="maxpoolsize"> -</property> <property name="initialpoolsize">5</property> <property name="MaxIdleTime"> -</property> <property name="Password">123456</property> <property name="User">root</property> </named-config> </c3p0-config>
C3P0 Database Connection Pool Management