[Connection pool] java connection pool learning (1)

learning target:

Learn the related knowledge of connection pool.

Learning Content:

  • Why do you need a connection pool?
    1. When the amount of concurrency is very low, the connection can be temporarily established, but when the service throughput reaches hundreds or thousands, establishing a connection and destroying a connection close will become the bottleneck. How to optimize at this time?
    (1) When the service is started, first establish a number of connections Array[DBClientConnection];
    (2) When the request arrives, then take one from the Array, perform downstream operations, and perform the replacement;
    thus avoiding repeated establishment and Destroy the connection to improve performance.
    The data structure that maintains Array[DBClientConnection] is the connection pool.
    ——Picked from: https://blog.csdn.net/wufaliang003/article/details/90746333
    2. The most primitive database use is to open a connection and use it. After use, be sure to close the connection to release resources. Frequent opening and closing of connections
    puts a certain resource load on the JVM, including the database , especially when the application pressure is high, the resource occupancy is high and it is easy to cause performance problems. The role of the connection pool is thus revealed. His principle is actually not complicated:
    first open a certain number of database connections, assign them to the caller when used, and return to the connection pool after the call is completed. Pay attention to these after returning to the connection pool. The connection is not closed, but is
    ready to be allocated to the next caller. It can be seen that the connection pool saves a lot of database connection opening and closing actions, and the benefits of improving system performance are self-evident. Several concepts:
    minimum connections-the number of connections that are opened immediately after the application is started and the minimum number of connections that are subsequently maintained. Maximum number of connections-the maximum number of connections that the application can use.
    Connection growth-the number of new connections opened by the application each time. Take an example to illustrate the operation of the connection pool:
    Assuming that the minimum and maximum connections are set to 10, 20, once the application is started, 10 database connections will be opened first, but note that the number of the database connection pool in use at this time is 0-because you are not using these connections, but idle The number is 10. Then you start logging in. Assuming that the login code uses a connection for query, then the number of in-use of the database connection pool is 1, and the number of idle is 9, which does not need to open the connection from the database-because the connection pool is ready for 10 Keep one for you. After logging in, what is the number of connections in the current connection pool? Of course it is 0, because that connection has been returned to the connection pool with the end of the transaction. Then 11 people log in in the same second at the same time. What will happen: The connection pool applies for (opens) a new connection from the database, and sends it out together with the other 10. At this moment, the number of use of the connection pool is 11, but it doesn’t matter. Under normal circumstances, it will become 0 again after a while. What if there are 21 people logged in at the same time? Then the 21st person can only wait for someone in front to log in and release the connection to him. At this time, the connection pool has opened 20 database connections-although it is likely that the number of connections in use has dropped to 0, will the 20 connections be maintained? Of course not, the connection pool will close a certain amount of connections within a certain period of time and return it to the database. In this example, the number is 20-10=10, because only the minimum number of connections needs to be maintained, and this time period is also in the connection pool Configured.
    ——Picked from: https://blog.csdn.net/m0_37893932/article/details/73930014
  • What are the commonly used connection pools now?
    Through Baidu, the commonly used connection pools are as follows:
    c3p0, dbcp, proxool, druid
    Spring recommends dbcp;
    Hibernate recommends c3p0 and proxool;
    1. DBCP: Apache
    DBCP (DataBase connection pool) database connection pool. It is a java connection pool project on Apache and a connection pool component used by tomcat. To use dbcp alone requires three packages: common-dbcp.jar, common-pool.jar, common-collections.jar. Since establishing a database connection is a very time-consuming and resource-consuming behavior, it is necessary to establish some connections with the database in advance through the connection pool. Put it in memory, when the application needs to establish a database connection, apply for one directly in the connection pool, and put it back when it is used up. dbcp does not automatically reclaim idle connections.
    ——Adopted from Du Niang: https://zhidao.baidu.com/question/873729864680886292.html
    2. C3P0:
    C3P0 is an open source JDBC connection pool, which implements data source and JNDI binding, supports JDBC3 specification and JDBC2 Standard extension. c3p0 operates asynchronously, and slow JDBC operations are done through the helper process. Extending these operations can effectively improve performance. Open source projects currently using it include Hibernate, Spring, etc. c3p0 has the function of automatically reclaiming idle connections.
    ——Picked from Du Niang: https://zhidao.baidu.com/question/873729864680886292.html
    3. Proxool: Sourceforge
    Proxool is a Java database connection pool technology. It is an open source project under sourceforge. This project provides a robust and easy-to-use connection pool. The most important thing is that this connection pool provides monitoring functions, which is easy to use and easy to find connection leaks.
    ——Picked from Du Niang: https://zhidao.baidu.com/question/873729864680886292.html
    4.
    Druid Druid is first a database connection pool. Druid is currently the best database connection pool. It surpasses other database connection pools in terms of functionality, performance, and scalability, including DBCP, C3P0, BoneCP, Proxool, and JBoss DataSource. Druid has deployed more than 600 applications in Alibaba, and has been rigorously tested in a large-scale production environment for more than a year. Druid is a database connection pool developed by Alibaba called Monitoring!
    (The area marked in red below is a bit messy)
    同时Druid不仅仅是一个数据库连接池,它包括四个部分: Druid是一个JDBC组件,它包括三个部分(这一块没看明白): 基于Filter-Chain模式的插件体系。 DruidDataSource 高效可管理的数据库连接池。 SQLParser
    Druid's function
    1. Replace DBCP and C3P0. Druid provides an efficient, powerful, and scalable database connection pool.
    2. You can monitor database access performance. Druid provides a powerful StatFilter plug-in built-in, which can perform detailed statistics on SQL execution performance, which is helpful for online database access performance analysis.
    3. The database password is encrypted. Writing the database password directly in the configuration file is a bad behavior and can easily lead to security problems. Both DruidDruiver and DruidDataSource support PasswordCallback.
    4. SQL execution log. Druid provides different LogFilters, which can support Common-Logging, Log4j and JdkLog. You can select the corresponding LogFilter as needed to monitor your application's database access.
    5. Extend JDBC. If you have programming requirements for the JDBC layer, you can use the Filter mechanism provided by Druid to easily write extension plug-ins for the JDBC layer.
    So Druid can:
    1. Act as a database connection pool.
    2. Can monitor database access performance
    3. Obtain SQL execution logs
  • Springboot2.x recommended connection pool: HikariCP
    content to be added

study-time:

Today: March 25, 2021 09:11:45


Learning output:

Guess you like

Origin blog.csdn.net/s1441101265/article/details/115194389