C3P0 and Druid database connection pool technology

1. What is a database connection pool?

1. Concept

In fact, it is a container (collection) that stores database connections. After the system is initialized, the container is created, and some connection objects are applied for in the container. When the user accesses the database, the connection object is obtained from the container. After the user completes the access, the connection object is returned to the container.

2. Benefits

  • save resources
  • Efficient user access

3. Implementation

Standard interface: Methods under the DataSource javax.sql package
:
            * Get the connection: getConnection()
            * Return the connection: Connection.close(). If the connection object Connection is obtained from the connection pool, then calling the Connection.close() method will no longer close the connection. but return the connection

2. c3p0 database connection pool

1. Implementation steps

 1. Import jar packages (two) c3p0-0.9.5.2.jar and mchange-commons-java-0.2.12.jar,
            * Don’t forget to import the database driver jar package

2. Define the configuration file:
            * Name: c3p0.properties or c3p0-config.xml
            * Path: Place the file directly in the src directory.

3. Create the core object database connection pool object ComboPooledDataSource

4. Get the connection: getConnection

2. Configuration file (c3p0-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
  <!-- 默认配置,只可以出现一次 -->
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</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>
</c3p0-config>

3. Example code

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 {
        DataSource ds = new ComboPooledDataSource();          //创建数据库连接池对象
        Connection conn = ds.getConnection();                 //获取连接对象
        for(int i = 1;i <= 10;i++)                            //10个以内不会超时
            System.out.println(i + ":" + conn);
    }
}

4. Output results

 

3. Druid database connection pool

1. Steps

1. Import the jar package druid-1.0.9.jar

2. Define the configuration file:
            * It is in the form of properties
            * It can be called any name and can be placed in any directory

3. Load the configuration file. Properties

4. Obtain the database connection pool object: obtain the DruidDataSourceFactory through the factory

5. Get the connection: getConnection

 2. Configuration file (druid.properties)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db3
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

 3. Example code

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidDemo {
    public static void main(String[] args) throws Exception {
        Properties pro = new Properties();
        InputStream is = DruidDemo1.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);                  //加载配置文件

        DataSource ds = DruidDataSourceFactory.createDataSource(pro);         //获取连接池对象
        Connection conn = ds.getConnection();                      //获取连接
        System.out.println(conn);
    }
}

4. Output results

 

 

Guess you like

Origin blog.csdn.net/weixin_51418964/article/details/122970598