A brief introduction to C3P0 connection pool

c3p0 is an easy-to-use library for extending traditional (DriverManager-based) JDBC drivers through JNDI bindable data sources (including data sources that implement connections and statement pools), as described in the jdbc3 specification and jdbc2 std extension .

Introduction to C3P0 connection pool:

C3P0 address: https://sourceforge.net/projects/c3p0/?source=navbar

C3P0 is an open source connection pool. For the Hibernate framework, C3P0 is recommended as a connection pool implementation by default.

C3P0 jar package:c3p0-0.9.1.2.jar

C3P0 commonly used configuration parameter explanation:

parameter Description
initialPoolSize The number of connections when the connection pool is just created
maxPoolSize How many connections can be placed in the connection pool
checkoutTimeout Maximum waiting time when there is no connection in the connection pool
maxIdleTime Idle connections in the connection pool will be recycled as long as they have not been used. The default is 0, 0 means no recycling

API introduction:

com.mchange.v2.c3p0.ComboPooledDataSourceThe class represents the connection pool object of C3P0. There are two common ways to create a connection pool:

1.无参构造,使用默认配置

2.有参构造,使用命名配置

  1. public ComboPooledDataSource()
    无参构造使用默认配置(使用xml中default-config标签中对应的参数)
  2. public ComboPooledDataSource(String configName)
    有参构造使用命名配置(configName:xml中配置的名称,使用xml中named-config标签中对应的参数)
  3. public Connection getConnection() throws SQLException
    从连接池中取出一个连接

Steps to use C3P0:
    1. Download the jar package and guide the package

c3p0-0.9.5.2.jar
mchange-commons-java-0.2.12.jar

    2. The xml configuration file used for configuration must be in the src directory (javaSE project), and the file name must be c3p0-config.xml 

<c3p0-config>
  <!-- 使用默认的配置读取连接池对象 -->
  <default-config>
  	<!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/JDBC</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- 连接池参数 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">10</property>
    <property name="checkoutTimeout">3000</property>
  </default-config>

  <named-config name="otherc3p0"> 
    <!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/JDBC</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- 连接池参数 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">6666</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
  
  <named-config name="abc"> 
    <!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/JDBC</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- 连接池参数 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">6</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
  
</c3p0-config>

    3. Create the core class object ComboPooledDataSource
    4. Call the method of the data source, Connection getConnection() 

package C3p0_01;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class C3p0Demo {
    public static void main(String[] args) throws SQLException {
        //3.创建核心类对象
        //在创建c3p0核心类对象时,自动去src目录中,找指定的配置文件c3p0-config.xml,自动解析得到数据库连接四要素
 
        //可以使用其实现类进行核心类对象的创建
//       ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();

        //可以使用其接口的多态核心类对象的创建
        DataSource dataSource = new ComboPooledDataSource();
        for (int i = 0; i < 11; i++) {
            Connection connection = dataSource.getConnection();
            System.out.println(i+"_"+connection);
            if(i == 5){
                connection.close();//数据库改写了连接的close方法,变成了返回连接池
            }
        }
    }
}

 

 

Guess you like

Origin blog.csdn.net/LOVE_Me__/article/details/104649725