Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of database connection pool

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the principle of database connection pooling, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Background introduction

The significance of pool technologies such as database connection pool and thread pool is to solve the problem of reuse of resources. In a computer, creating a new resource is often very expensive. On the other hand, pooling technology can allocate and manage a certain type of resource uniformly, it allows our programs to reuse this resource, and only in extreme cases (such as connection pool full) will new resources be created.

The resource of database connection is particularly expensive, it is very expensive to create, and the impact of a large number of create connections and release operations on the program is very obvious.

Database connection pool is proposed to solve this problem.

Realization principle

It is important to note that the implementations we provide below are based on simple prototypes and are designed to show you some of the fundamentals of connection pooling implementation. Real database connection pooling technology needs to consider more complex details.

So the following code cannot be used directly in production.

Java.sql.Connection will be used in the implementation, because this is only an interface and can not create an instance. For the convenience of demonstration, I inherit this interface and write a simple test class, but add a delay simulation submission in the commit method.

Implementation mode 1

It's easy to think of a solution right away. We use a map to store the connection object and use it from the map when we need it.

The main point here is that although we use thread-safe ConcurrentHashMap to store connection resources, the getConnection method still needs to add the synchronized keyword to avoid concurrency problems. This is the easiest to overlook.

Imagine that in a scenario, we want multiple threads of an application to share connection resources.

Suppose two threads execute to pool.containsKey (key) at the same time and then both return false, then both threads create a connection. Although only one of them is added to ConcurrentHashMap's put method, it still generates an extra connection.

The reason is that although every operation of ConcurrentHashMap itself is thread-safe, when these operations are combined, atomicity is not guaranteed, so concurrency problems can occur.

With a friendly hint here, you will often encounter this test site in the interview.

Mode 2

The second way of implementation is the optimization based on 1. The problem with the 1. 1 solution is that every time you visit getConnection, you have to lock and release the lock, which is inefficient.

The second scheme is to use the Future mechanism in the java concurrent package to solve the problem of creating redundant connections in concurrent scenarios.

Let's straighten out whether there will be concurrency problems with this implementation. Assuming that two threads enter the else branch at the same time, ConcurrentHashMap at 28 lines of code ensures that only one thread will execute, that is, only one task will be added. None of the other threads will join successfully.

So there is only one thread connectionFutureTask = = null, which starts the task of creating the connection asynchronously, while the other threads call the get method of FutureTask to get the results directly.

Mode 3

Another problem with the implementation of 1 and 2 is that multiple threads get the same connection. This scheme is not allowed in some scenarios. For example, the transaction manager of the spring database requires independent connection resources for each transaction processing thread.

The following scheme is based on the linked list structure, with relatively complete acquisition and release operations, and different threads can get independent connection resources.

Note that in this scheme, we introduce a timeout when obtaining the connection. If the method can get the result within a period of time, the result will be returned immediately, otherwise, the timeout will return the default result.

Implementation principle of druid connection Pool

After understanding the general idea of realizing connection pooling, we can continue to learn about the more mature connection pooling products on the market. Among them, Alibaba's open source druid open source connection pool is a representative.

As one of the best connection pooling technologies in the field of java, Druid connection pooling itself is only a part of its function. In addition, it also needs supporting monitoring functions. Of course, this is not the focus of our article.

Let's take a look at how to use Druid connection pooling in your code.

So move on to the getConnection method in DataSource.

The main function of the init method is to initialize the connection pool according to the configuration file, which generates some real physical connections and puts them into an array. Of course, this method is guaranteed to be called only once.

Moving on, you will eventually call the private method getConnectionInternal

The part circled in red is the core. Let's take a look at the takeLast method by going into different branches according to the incoming wait time.

The code logic is also clear, and poolCount is the current number of connections available for the connection pool.

If 0, the producer thread is awakened by emptySignal to create a new connection, while the current thread suspends waiting for the notEmpty signal. What notEmptyWaitCount maintains is the number of consumers waiting.

If it is not 0, the last connection from the array is taken back. Some people may wonder, what is returned here is DruidConnectionHolder, not Connection?

In fact, you can see the definition of the former.

DruidConnectionHolder encapsulates Connection and connected datasource information, as well as a number of statement, etc., which are managed uniformly.

After reading the above, do you have any further understanding of the principle of database connection pooling? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report