当前位置:网站首页>1. introduction to MySQL database connection pool function technology points

1. introduction to MySQL database connection pool function technology points

2022-06-23 01:39:00 For financial freedom!

Key technical points

MySQL Database programming 、 The singleton pattern 、queue Queue container 、C++11 Multithreaded programming 、 Threads are mutually exclusive 、 Thread synchronous communication and
unique_lock、 be based on CAS Atomic shaping 、 Intelligent pointer shared_ptr、lambda expression 、 producer - Consumer thread model

Project background

In order to improve the MySQL database ( be based on C/S Design ) Access bottleneck of , In addition to adding cache on the server side, the server caches commonly used data
outside ( for example redis), You can also add connection pools , To improve the MySQL Server Access efficiency , In high concurrency , a large number of TCP Three handshakes 、MySQL Server Connection authentication 、MySQL Server Close the connection to recycle resources and TCP The performance time spent on four waves is also obvious , The purpose of adding connection pool is to reduce the performance loss of this part .
The more popular connection pool in the market includes Ali's druid,c3p0 as well as apache dbcp Connection pool , It is obvious that they can improve the operation performance of a large number of databases in a short period of time , But one thing they have in common is , All by Java Realized . So the purpose of this project is to C/C++ In the project , Provide MySQL Server Access efficiency , Implementation is based on C++ Database connection pool module of code .

Function point introduction

The connection pool generally contains the database connection used by ip Address 、port Port number 、 The password and other parameters of the user name , For example, the initial number of connections , Maximum connections , Maximum free time 、 Connection timeout, etc , The project is based on C++ Connection pool implemented by language , It mainly realizes the common basic functions supported by all the connection pools mentioned above .

  1. Initial connections (initSize): Indicates that the connection pool will be in advance with MySQL Server establish initSize The number of connection Connect , When an application launches MySQL During the interview , No need to create and MySQL Server New connections , You can get a direct connection from the pool , After use , Not to release connection, It's about putting the present connection And back to the connection pool .

  2. Maximum connections (maxSize): When concurrent access MySQL Server When the number of requests for , The initial connection is not enough , At this time, more connections will be created for the application according to the number of new requests , But the maximum number of new connections created is maxSize, You can't create connections without restrictions , Because every connection takes up one socket resources , Generally, connection pooling and server programs are deployed on a single host , If the connection pool takes up too much socket resources , Then the server can't receive too many client requests . When these connections are done , Return to the connection pool again to maintain .

  3. Maximum free time (maxIdleTime): When accessing MySQL After more concurrent requests for , The number of connections in the connection pool will increase dynamically , The upper limit is maxSize individual , When these connections are used up, they are returned to the connection pool again . If at specified maxIdleTime Inside , None of these new connections have been used again , Then the newly added connection resources will be recycled , Just keep the initial connection initSize Just a connection .

  4. Connection timeout (connectionTimeout): When MySQL Too many concurrent requests for , The number of connections in the connection pool has reached maxSize 了 , There is no free connection available at this time , At this time, the application cannot obtain the connection from the connection pool , It gets the connection by blocking if it takes more than connectionTimeout Time , So get connection failed , Unable to access database .

The project mainly realizes the above four functions of connection pool , The rest of the connection pool has more extended functions , It can be realized by itself .

Function realization design

ConnectionPool.cpp and ConnectionPool.h: Connection pool code implementation
Connection.cpp and Connection.h: Database operation code 、 Add, delete, modify and check the code to realize
Connection pool mainly includes the following function points :

  1. The connection pool only needs one instance , therefore ConnectionPool Design in singleton mode
  2. from ConnectionPool You can get and MySQL The connection of Connection
  3. Free connection Connection All maintained in a thread safe Connection In line , Use thread mutex lock to ensure the line of queue
    Cheng safety
  4. If Connection The queue is empty , You need to get the connection again , At this point, you need to dynamically create a connection , The upper limit is maxSize
  5. Queue idle connection time exceeded maxIdleTime You're going to be released , Keep only the original initSize Just a connection , This
    Function points must be done in a separate thread
  6. If Connection The queue is empty , At this time, the number of connections has reached the upper limit maxSize, So wait connectionTimeout Time
    If you can't get an idle connection , So get connection failed , This is from Connection Queue to get free connections , You can use the belt
    Time out mutex Mutex to achieve connection timeout
  7. The connection obtained by the user is shared_ptr Smart pointer to manage , use lambda Expression custom connection release function ( Not really release
    Connect , Instead, return the connection to the connection pool )
  8. Connected production and connected consumption adopt producers - Consumer thread model to design , The synchronous communication mechanism between threads is used, and the condition variable
    And the mutex

 Insert picture description here

原网站

版权声明
本文为[For financial freedom!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514414529.html