C3p0 creates a connection pool,
C3p0 is currently the most widely used database connection pool
Feature: the auto-collect connection feature is available;
Asynchronous execution is available.
Use c3p0 to export packages, link https://sourceforge.net/projects/c3p0/
Package to be imported: c3p0-0.9.5.2-sources.jar mchange-commons-java-0.2.11-sources.jar
Detailed usage of the c3p0 decompression package under doc (all in English)
Core class: ComboPooledDataSource
Profile: Must be called c3p0-config.xml
Java code:
Public class C3p0 {
Private static ComboPooledDataSource ds = null;
// Static code block
Static {
Ds = new ComboPooledDataSource ();
}
Public static Connection getConnection (){
Try {
// Get a Connection object through ds
Return ds. getConnection ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return null;
}
Public static void main (String [] args ){
For (int I = 0; I <5; I ++ ){
// Output the connection in the console
System. out. println (getCon ());
}
}
}
Configuration file:
<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>
<! -- Database-driven -->
<Property name = "driverClass"> com. mysql. jdbc. Driver </property>
<! -- Initialization value in connection pool -->
<Property name = "initialPoolSize"> 5 </property>
<! -- Maximum value in connection pool -->
<Property name = "maxPoolSize"> 10 </property>
<! -- Exceeds the maximum wait time -->
<Property name = "checkoutTimeout"> 3000 </property>
</Default-config>
<! -- Configure by name intergalactoApp -->
<Named-config name = "intergalactoApp">
<Property name = "jdbcUrl"> jdbc: mysql: // localhost: 3306/test </property>
<Property name = "user"> root </property>
<Property name = "password"> 123123 </property>
<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>