preface

Database connection pool of the basic idea is: for the database connection to create a “buffer pool”, into the pool in advance a certain number of database connection pipe, when needed, use pipe out of the pool, after the operation, in the pipeline to be included in the pool, so as to avoid the frequent apply to the database resources, release resources bring performance loss. In today’s distributed system, the QPS bottleneck of the system is often in the database, so it is very helpful to understand the underlying construction principle and design idea of database connection pool. Database connection pool C3P0, DBCP, Druid, etc.

A few thoughts on database connection pooling

Understand a handwritten implementation of the underlying principles of database connection pooling

First, the database connection pool is the repository of the database operation pipes, not only the repository, but also the management of these pipes. Second, an external configuration file should be provided to initialize the database connection pool. Third, if a database operation pipeline is already occupied, should other requests get the pipeline? That is to say, we need to consider the allocation of pipeline in multi-threaded concurrency. Fourth, how to achieve pipeline reuse? Put it back into the pool, sign available, not really close the pipe; Write a mini-database connection pool

Understand a handwritten implementation of the underlying principles of database connection pooling

IMyPool is an interface that provides basic database connection pool services, such as obtaining a database operation pipeline. MyDefaultPool is an implementation of IMyPool. MyPooledConnection represents a database operation pipe, which can execute SQL, close a pipe, and so on. MyPoolFactory is a factory, singleton pattern used to get the IMyPool implementation. DBConfigXML represents the external configuration file. Test Is used to Test. DBConfigXML

Understand a handwritten implementation of the underlying principles of database connection pooling

To use database connection pooling in practice, we need to configure some parameters in the Spring configuration file. Here, to simplify parsing, we provide it directly. MyPooledConnection

Understand a handwritten implementation of the underlying principles of database connection pooling

A database Connection pipe encapsulates a JDBC Connection, but note the isBusy identifier. The closure of the pipeline is really just a sign change! IMyPool

Understand a handwritten implementation of the underlying principles of database connection pooling

MyDefaultPool

Understand a handwritten implementation of the underlying principles of database connection pooling

Note that MyDefaultPool holds a collection of pipes. For multithreading purposes, Vector is used. MyDefaultPool needs to be initialized

Understand a handwritten implementation of the underlying principles of database connection pooling

The database connection pool needs to load the database driver and set up the initialization pipeline according to the external configuration file. The createMyPooledConnection interface is implemented

Understand a handwritten implementation of the underlying principles of database connection pooling

When creating a database connection pool pipe, you should check to see if the limit is reached, and if not, you can create one. Not only do they create them, but they also mark each pipe with the isBusy flag. GetMyPooledConnection interface implementation

Understand a handwritten implementation of the underlying principles of database connection pooling

Note here: if you can’t manipulate pipes, you need to create pipes! getRealConnectionFromPool

Understand a handwritten implementation of the underlying principles of database connection pooling

First, synchronized is used to avoid problems in multithreading. Second, remember that Connection has a timeout mechanism. What if we get a pipe whose Connection has already timed out? Third, once you have the pipe, be sure to pay attention to the setting of isBusy. MyPoolFactory

Understand a handwritten implementation of the underlying principles of database connection pooling

The Test Test

Understand a handwritten implementation of the underlying principles of database connection pooling

The results

Understand a handwritten implementation of the underlying principles of database connection pooling

Well, there you have it, a mini-database connection pool!