Detailed explanation of JDBC-C3P0 database connection pool

C3P0:数据库连接池技术
		* 步骤:
			1. 导入jar包 (两个) c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar ,
				* 不要忘记导入数据库驱动jar包
			2. 定义配置文件:
				* 名称: c3p0.properties 或者 c3p0-config.xml
				* 路径:直接将文件放在src目录下即可。

			3. 创建核心对象 数据库连接池对象 ComboPooledDataSource
			4. 获取连接: getConnection
		* 代码:
			 //1.创建数据库连接池对象
	         DataSource ds  = new ComboPooledDataSource();
	         //2. 获取连接对象
	         Connection conn = ds.getConnection();

File structure (be careful not to forget to import the database driver jar package)

Insert picture description here

Code demo 1


import com.mchange.v2.c3p0.ComboPooledDataSource;

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

/**
 * c3p0的演示
 */
public class C3P0Demo1 {
    
    
    public static void main(String[] args) throws SQLException {
    
    
        //1.创建数据库连接池对象
        DataSource ds  = new ComboPooledDataSource();
        //2. 获取连接对象
        Connection conn = ds.getConnection();

        //3. 打印
        System.out.println(conn);

    }
}

Result
Insert picture description here
XML configuration file

<c3p0-config>
  <!-- 使用默认的配置读取连接池对象 -->
  <default-config>
  	<!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/db4</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/db4</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <!-- 连接池参数 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">8</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
</c3p0-config>

Code demo 2

import com.mchange.v2.c3p0.ComboPooledDataSource;

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

/**
 * c3p0演示
 */
public class C3P0Demo2 {
    
    

    public static void main(String[] args) throws SQLException {
    
    
       /* //1. 获取DataSource,使用默认配置
        DataSource ds  = new ComboPooledDataSource();

        //2.获取连接

        for (int i = 1; i <= 11 ; i++) {
            Connection conn = ds.getConnection();
            System.out.println(i+":"+conn);

            if(i == 5){
                conn.close();//归还连接到连接池中
            }
        }*/

        testNamedConfig();

    }


    public static void testNamedConfig() throws SQLException {
    
    
        // 1.1 获取DataSource,使用指定名称配置
        DataSource ds  = new ComboPooledDataSource("otherc3p0");
        //2.获取连接
        for (int i = 1; i <= 10 ; i++) {
    
    
            Connection conn = ds.getConnection();
            System.out.println(i+":"+conn);
        }
    }

}

Insert picture description here

This is because only 8 connection pools are applied for in the configuration file, but 10 of them are to be printed, so exceptions start from the eighth

Guess you like

Origin blog.csdn.net/qq_43531919/article/details/107637262