This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021
Introduction to the
This article will preliminarily explore the application scenarios of database connection pooling to prepare for the subsequent source code analysis
Application scenarios of database connection pools
Before there is no connection pool, the following steps are required to access the database in use:
- 1. Create a connection
- 2. Use
- 3. Close the connection
The above is basically a connection usage life cycle, which is fine if the access is not very frequent, but if the access is frequent, there are the following problems:
- 1. Time consuming: The creation of a connection is time-consuming. Creating a connection frequently causes unnecessary time consuming
- 2. Resources: Both creation and destruction require corresponding services to destroy resources. Frequent connection creation wastes system resources
To solve the above problem, connection reuse can be adopted: by establishing a database connection pool and a set of connection usage management policies, making a database connection can be reused in a funny and safe way, avoiding the overhead of frequent establishment and closure of database connections
Design mode: Resource pool. The idea of resource pools is also applied to everyday work, such as HTTP connection pools, thread pools, and so on
Druid database connection pool: Druid database connection pool: Druid database connection pool: Druid database connection pool: Druid database connection pool
An overview of the Druid
Its main source directory is as follows:
The important parts are as follows:
- Filter: Druid supports monitoring, agents, and other functions to help developers analyze performance metrics for database operations
- Pool: Main implementation of connection pooling
- SQL: SQL statement parsing
- -Penny: The wall.
Simple example run
Let’s use the source code for an example to run: druid – spring – the boot – starter/SRC/test/Java/com/alibaba/druid/spring/boot/demo/DemoApplication. Java
/ * * *@authorLihengming <[email protected]> * * <p> A simple usage demonstration </p> * <p>1. Properties </p> * <p>2. Run DemoApplication</p> * <p>3. Visit http://127.0.0.1:8080/druid < / p > < p > 4. To access/user / ${id} interface, view the SQL, Web, AOP monitoring effect, such as: http://127.0.0.1:8080/user/1 < / p > * * /
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code
Can see the official built-in provide fast experience, based on the memory data of H2, we visit the management page: http://127.0.0.1:8080/druid/sql.html
conclusion
Since there are not many studies on this side before, I will simply heat the lower body today to prepare for the subsequent analysis:
- Search related articles to understand the application scenarios of database connection pooling
- Pull the Druid source code and run the sample
Refer to the link
- Druid connection pool introduction
- The realization and principle of database connection pool
- How to design and implement a DB connection pool?