1

A fan who has worked for 5 years found me. He said that he was participating in an interview with Meituan, but he did not answer a basic question when he encountered it.

The question is: "What is the use of database connection pool? And what are the key parameters of it"?

I said, I don't know this question, so how do you set the connection pool configuration in your project? Guess how he answered. I know I understand.

Ok, on this question, let's take a look at the answers of ordinary people and experts.

Ordinary people:

The role of the database connection pool is to reuse this connection, so that the cost of establishing a connection between the application and the database can be reduced.

In order to improve the performance of the entire application and database access.

Expert:

I will answer this question from several aspects.

First of all, the database connection pool is a pooling technology. The core idea of the pooling technology is to realize the reuse of resources and avoid the overhead of repeated creation and destruction of resources.

In the application scenario of the database, every time the application initiates a CRUD operation to the database, it needs to create a connection

In the case of large database access, frequent connection creation will bring a large performance overhead.

The core idea of the connection pool is that the application initializes a part of the connection in advance and saves it in the connection pool when it starts. When the application needs to use the connection, it directly obtains an established link from the connection pool.

The design of the connection pool avoids the overhead caused by the establishment and release of each connection.

image-20220422140800456

There are many parameters of the connection pool, but there are only a few key parameters:

The first is that there are several key parameters when the connection pool is initialized:

  1. The number of initialized connections, indicating how many initial connections are stored in the connection pool at startup.
  2. The maximum number of connections indicates the maximum number of connections that can be supported at the same time. If the number of connections is not enough, the subsequent thread to obtain the connection will be blocked.
  3. The maximum number of idle connections, indicating the maximum idle connections to be retained in the connection pool when there is no request.
  4. The minimum idle connection. When the number of connections is less than this value, the connection pool needs to create a connection to supplement this value.

Then, the key parameters when using the connection:

  1. The maximum waiting time is the time for a new request to wait after the connection in the connection pool is used up. If this time is exceeded, a timeout exception will be prompted.
  2. Invalid connection cleanup, clean up invalid connections in the connection pool to avoid errors when using this connection operation.

Different connection pool frameworks, in addition to the core parameters, there are many business-type parameters, such as whether to detect the validity of the connection SQL, connection initialization SQL, etc. These configuration parameters can be used to query the api document. Know.

The above is my understanding of the problem.

Summarize

This question goes a step further and asks how to set the maximum number of connections and the minimum number of connections?

The realization principle of connection pool and so on.

Therefore, it is recommended that fans still have a systematic study.

If you have any interview questions, career development questions, or study questions, you can PM me.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。