The most common connection pooling technology, spring supports C3P0 connection pooling by default.
Core class
1. Combopooleddatasource ds;
Introduced, C3P0 jar package
2. Use a link pool to create a link
A) hard-coded mode
Public voidTestcode ()throwsexception{//Create connection Pooling tool classCombopooleddatasource DataSource =NewCombopooleddatasource (); //Setting Connection ParametersDatasource.setjdbcurl ("Jdbc:mysql://localhost:3306/learnstruts"); Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setuser ("Root"); Datasource.setpassword ("33269456.cx"); Datasource.setinitialpoolsize (3); Datasource.setmaxpoolsize (6); //get linked objectJava.sql.Connection Connection =datasource.getconnection (); Connection.preparestatement ("DELETE from user WHERE userId = ' 1 '"). Executeupdate (); //CloseConnection.close (); }
b) How XML is configured
C3p0-config.xml
<C3p0-config> <Default-config> < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/learnstruts</ Property> < Propertyname= "User">Root</ Property> < Propertyname= "Password">33269456.cx</ Property> < Propertyname= "Initialpoolsize">5</ Property> < Propertyname= "Maxpoolsize">10</ Property> </Default-config><!--<named-config name= "Oracleconfig" > <property name= "Driverclass" >com.mysql.jdbc.driver</ property> <property name= "Jdbcurl" >jdbc:mysql:///day17</property> <property name= "user" >roo t</property> <property name= "password" >root</property> <property name= "Initialpoolsize" > 5</property> <property name= "maxpoolsize" >10</property> </named-config> -</C3p0-config>
@Test Public void throws exception{ // in this class will automatically load c3p0 config file under src new Combopooleddatasource (); = datasource.getconnection (); Connection.preparestatement ("DELETE from user WHERE userId = ' 1 '"); Connection.close (); }
Jdbcutil.jav package com.cx.utils;
ImportJavax.sql.DataSource;ImportOrg.apache.commons.dbutils.QueryRunner;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classJdbcutils {//Initializing the connection pool Private StaticDataSource DataSource;
Static code blocks load connection pools, load only onceStatic { //get the data source from the connection poolDataSource =NewCombopooleddatasource (); } //encapsulate Common Objects Public StaticDataSource Getdatasource () {returnDataSource; } Public StaticQueryrunner Getqueryrunner () {return NewQueryrunner (DataSource); }
}
JAVAWEB-C3P0 Connection Pool