Getting Started with Druid, Alibaba's Mainstream Database Connection Pool

content

1. The necessity of database connection pool

(1). The steps of the traditional database connection mode

( 2). Problems existing in traditional database connection mode

2. Database connection pool technology

(1). The idea of ​​data connection pool:

( 2). Tasks of the database connection pool:

( 3). The scale of the database connection pool:

(4). Working principle:

( 5). Advantages of database connection pool:

1. Resource reuse:

2. Faster response speed:

3. Database sharing mechanism

4. Avoid memory leaks:

3. Various open source database connection pools

Four. Learn the most mainstream database connection pool Druid

( 1). Create a properties file for the druid database connection pool

( 2). Create a database connection pool

(3). The test obtains the connection object through the database connection pool

( 4). Test results (successfully created)

Five. Common configuration parameters of Druid database connection pool


1. The necessity of database connection pool

(1). The steps of the traditional database connection mode

        1. Create a connection in the main program

        2. Perform sql operation

        3. Close the database connection

(2). Problems existing in traditional database connection mode

1. Waste of time: verifying login and loading conn into memory every time you connect,

2. Inability to access the database on a large scale: when the database accesses too many people, it will take up a lot of system resources, which will cause the server to crash

3. There is a memory leak problem: every time the connection needs to be disconnected, if it is not disconnected and the program runs, there will be a created connection object that cannot be closed in the memory, which will lead to the problem of java memory leak .

Memory leak : means that the created object cannot be recycled

2. Database connection pool technology

(1). The idea of ​​data connection pool:

Establish a buffer pool in memory in advance to store a certain number of connection objects, call it in it when needed, and put it back into the buffer pool at the end.

(2). Tasks of the database connection pool:

Manages and releases database connections, allowing users to use pooled connection objects without creating objects.

(3). The scale of the database connection pool:

The number of initialization: set by the minimum number of database connections;

Maximum number: Determined by the maximum number of database connections.

When the number of connections exceeds the maximum number of connections , the excess connections will stop waiting for the release of the connection object.

(4). Working principle:

 (5). The advantages of database connection pool:

1. Resource reuse:

Objects in the connection pool are taken out when needed and do not need to be recycled by the connection pool

2. Faster response speed:

Reserve the connection object in the pool in advance, the initialization has been completed, and it is called directly.

3. Database sharing mechanism

Multiple users accessing the same database can avoid resource monopoly through configuration at the application layer.

4. Avoid memory leaks:

The connection objects are managed uniformly, the time slice of the connection object is set, and the timeout is forced to be recycled.

3. Various open source database connection pools

JDBC 's database connection poolis represented by javax.sql.DataSource , which is an interface that is usually provided by the server .

(1). Common open source database connection pools:

DBCP: faster than C3P0 but has bugs

c3p0: slow, but relatively stable

Proxool: Open source connection pool, has the function of monitoring connection pool, but the stability is worse than C3P0

BoneCP: Fast, Open Source

Druid: The connection pool provided by Alibaba is fast (not as fast as BoneCP), has good stability, and has the function of monitoring the connection pool.

Four. Learn the most mainstream database connection pool Druid

(1). Create a properties file for the druid database connection pool

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc_db
username=root
password=3.141592654
initialSize=10
maxActive=20

(2). Create a database connection pool

public Class JDBCUtils{ 
   private static DataSource source;
    static {
        try{
            //创建properties对象,用来封装从文件中获取的流数据
            Properties pros = new Properties();
            //采用类加载方式获取文件的内容,并封装成流
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("Druid.properties");
            //将流传入到pros对象中
            pros.load(is);
            //利用工厂类创建数据库连接池
            source = DruidDataSourceFactory.createDataSource(pros);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Connection getConnByDruid() throws Exception {
        //所有的数据连接池要想被java所使用,都必须先实现sun公司提供的一个接口DateSource
        //获取数据库连接池对象
        Connection conn = source.getConnection();
        return conn;
    }
}

(3). The test obtains the connection object through the database connection pool

public class TestDruid {
    @Test
    public void getDruidConn() throws Exception {
        Properties pros = new Properties();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("Druid.properties");
        pros.load(is);
        //所有的数据连接池要想被java所使用,都必须先实现sun公司提供的一个接口DateSource
        //获取数据库连接池对象
        DataSource source = DruidDataSourceFactory.createDataSource(pros);
        Connection conn = source.getConnection();
        System.out.println(conn);

    }
}

(4). Test results (successfully created)

Five. Common configuration parameters of Druid database connection pool

 

Guess you like

Origin blog.csdn.net/qq_52655865/article/details/124055945