当前位置:网站首页>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 .
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 .
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 .
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 .
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 :
- The connection pool only needs one instance , therefore ConnectionPool Design in singleton mode
- from ConnectionPool You can get and MySQL The connection of Connection
- Free connection Connection All maintained in a thread safe Connection In line , Use thread mutex lock to ensure the line of queue
Cheng safety - 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
- 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 - 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 - 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 ) - 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


边栏推荐
- [template] KMP
- Day260: the number III that appears only once
- Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)
- 总体标准差和样本标准差
- Add expiration time for localstorage
- Module 8 job
- [22 summer reconstruction 1] codeworks round 791 (Div. 2)
- JS to paste pictures into web pages
- What aspects of language and database knowledge are needed to build a web Kanban!
- Steps to implement a container global component
猜你喜欢

Local deployment and problem solving of IIS in ArcGIS JS 4.23

Byte order: big endian vs little endian

Questions not written in the monthly contest
![[learning notes] roll back Mo team](/img/19/d374dd172b9609a3f57de50791b19e.png)
[learning notes] roll back Mo team

魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择

B tree and b+ tree

How to type Redux actions and Redux reducers in TypeScript?

MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)

Day367: valid complete square

Autumn move script a
随机推荐
MySQL-Seconds_ behind_ Master accuracy error
Template specialization template <>
Flink synchronizes MySQL data to es
Error reported when compiling basalt
[ZOJ] P3228 Searching the String
Pat class A - 1007 maximum subsequence sum
Steps to implement a container global component
[launch] redis Series 2: data persistence to improve availability
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
[cmake command notes]target_ compile_ options
Population standard deviation and sample standard deviation
Software construction course ADT and OOP understanding
SQL programming task02 job - basic query and sorting
时间复杂度
What aspects of language and database knowledge are needed to build a web Kanban!
[hdu] P6964 I love counting
Day575: divide candy
Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
Summary of the first week of winter vacation
Day260: the number III that appears only once