JDBC_4 database connection pool

Database connection pool


The necessity of JDBC database connection pool
When using and developing database-based web programs, the traditional model basically follows the following steps:

  1. Establish a database connection in the main program (such as servlet beans)
  2. Perform sql operations
  3. Disconnect the database connection

Problems with this mode of development:

  • Ordinary JDBC database connection is obtained using DriverManager. Each time a connection is established to the database, the Connection must be loaded into the memory, and the user name and password must be verified (about 0.05s-1s). When a database connection is required, the database Request one, and then disconnect after the execution is complete. This way will consume a lot of time. The connection resources of the database have not been well utilized.
  • For each database connection, it must be disconnected after use. Otherwise, if the program fails to close due to an abnormality, it will cause a memory leak in the database system, and eventually cause the database to be restarted.
  • This kind of development cannot control the number of connection objects created, and system resources will be allocated recklessly. If there are too many connections, it may also cause memory leaks and server crashes.

The
basic idea of database connection pool technology : establish a "buffer pool" for database connections. Put a certain number of connections in the buffer pool in advance. When you need to establish a database connection, you only need to take one out of the "buffer pool" and put it back after use.
The database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to use an existing database connection instead of re-establishing one.

JDBC database connection pool is represented by DataSource, which is just an interface. This interface is usually implemented by the server, and some open source organizations provide implementation.

Advantages of database connection pool
Insert picture description here


DBCP C3P0 Druid is the main three database connection pool technologies


c3p0

package com.atguigu4.connection;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import org.junit.Test;

import java.sql.Connection;

public class C3P0Test {
    
    
    @Test
    public void testGetConnection() throws Exception{
    
    
        //获取c3p0数据库连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        cpds.setUser("root");
        cpds.setPassword("924930871111");
        //设置初始时数据库连接池中的连接数
        cpds.setInitialPoolSize(10);

        Connection connection = cpds.getConnection();
        System.out.println(connection);
        //销毁c3p0连接池
        DataSources.destroy(cpds);
    }
}

Configuration file


    <!-- 配置c3p0的基本信息 -->
    <named-config name="helloc3p0">
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">123</property>
        <!--当数据库连接池中的连接数不够时,c3p0一次性向数据库服务器申请的连接数-->
        <property name="acquireIncrement">3</property>
        <!--c3p0初始化时的连接数-->
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">10</property>
        <!--连接池最多维护的Statement个数-->
        <property name="maxStatements">50</property>
        <!--每个连接中最多可以使用的Statement的个数-->
        <property name="maxStatementsPerConnection">2</property>
    </named-config>

Guess you like

Origin blog.csdn.net/m0_46656833/article/details/112545029