当前位置:网站首页>Druid connection pool - strong self-study from 0. Those who don't understand Druid can click in. If you know not to click in, you will think I'm wordy
Druid connection pool - strong self-study from 0. Those who don't understand Druid can click in. If you know not to click in, you will think I'm wordy
2022-07-25 04:34:00 【Liu Chu, Ge Nian】
List of articles
Official document encyclopedia :https://github.com/alibaba/druid/wiki/Druid%E8%BF%9E%E6%8E%A5%E6%B1%A0%E4%BB%8B%E7%BB%8D
introduction
It's all equipped in the project , In addition to the code matching , I didn't write the relevant code , We don't understand what this is , Today, let's start from scratch , Learn to be strong .

What is? Druid Connection pool ?
Druid Connection pool is an open source database connection pool project of Alibaba .
Druid Connection pools are built for monitoring , Built in powerful monitoring function , Monitoring features do not affect performance . Powerful , Can prevent SQL Inject , built-in Loging Able to diagnose Hack Applied behavior .
Oh , First Druid It's a database connection pool , For the first time , I don't know either , Then let's take a look at the database connection pool
What is a database connection pool ?
Database connection pool (Database Connection Pooling) When the program is initialized, a certain number of database connection objects are created and saved in a memory area , It allows applications to reuse an existing database connection , Instead of re establishing a ; Release the database connection whose idle time exceeds the maximum idle time to avoid database connection omission caused by not releasing the database connection .
That is, a certain number of database connections are created during program initialization , You can put it back after use , The next one goes on , The initial number of connections in the connection pool is controlled by configuring the parameters of the connection pool 、 The minimum connection 、 Maximum connection 、 Maximum idle time these parameters ensure that the number of database accesses is within a certain controllable range , Prevent system crash , Make the user experience better
Oh , I see , Like that thread pool
Let's see what it does ?
Just now I mentioned his role : Druid Connection pools are built for monitoring , Built in powerful monitoring function , Monitoring features do not affect performance . Powerful , Can prevent SQL Inject , built-in Loging Able to diagnose Hack Applied behavior .
Oh , I see , Monitor the behavior of the database
What's the point of him ?
The meaning of database connection pool is , Ability to reuse database connections , Improve response time to requests and server performance .
Multiple database connection objects are established in advance in the connection pool , Then save the connection object to the connection pool , When a customer request comes , Take a connection object directly from the pool to serve customers , When the request is complete , The client program calls close() Method , Put the connection object back into the pool .
Oh , I see , Improve the efficiency of database connection ( Improve response time to requests and server performance )
Then why use it ?
The performance indicators of each product are as follows :
Druid Connection pooling is in performance 、 monitor 、 The diagnosis 、 Security 、 These aspects of scalability are far beyond the competitive products 
Oh , I see , because Druid It cow
Oh , How to use it? ?
After checking for a long time, someone said that it was necessary to manage the configuration first DataSource Connect pool objects
Let's first see what it is DataSource, Don't panic about this , Just translate it into Chinese , It's the data source ( A data source is a source of data , This is equivalent to a database )
Speaking of this, expand what is DataSource object :
What is? DataSource object ?
DataSource The object is javax.sql An interface in the package , In fact, it can be identified as a database connection resource , The data source object should store the connected url, User name, password and other connection information .
Oh ,DataSource Object is an interface , Put database connection information
In the use of JDBC When connecting to the database , All use through DriverManager To get Connection object ,getConnection Methods need to be passed url,name.passwd Etc , In fact, this information can be used as a data source object .
What is DataSource Connection pool ?
Translated into Chinese is the data source connection pool . A data source is a source of data , It's like a database .
But the program does not directly access the database , Through an agent , That is, data connection pool , Every object referenced by a data connection pool must have a data source .DataSource There are many implementation subclasses of , Many third-party connection pools need to be implemented DataSource Of , If you create a data source with a data pool , Then there is the connection pool function .
Oh , It's actually a database connection pool
such as :Mybatis There is a default data source implementation class ( You are here. xml Configured in )
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_test?characterEncoding=UTF-8&allowMultiQueries=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
You've been walking around for a long time , It's just a sentence : First, configure the database connection pool , What is a database connection pool? I just said
For example, what we are talking about now is Druid Connection pool , Then the data source connection pool used in the project is :DruidDataSource.
Druid Connection pool configuration
【 First step 】 add to Druid Connection pool dependency
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
Be careful : In addition to adding the above two dependencies , Don't forget to add spring-context rely on .
【 The second step 】 To configure DruidDataSource Connection pool Bean object
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
【 The third step 】 From... In the test class IOC Get the connection pool object in the container and print
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
}
}
The result is :
Oh , There is a connection pool
Configuration parameters
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="6000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- Basic attributes url、user、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
<!-- Configure initialization size 、 Minimum 、 Maximum -->
<property name="initialSize" value="5" />
<property name="minIdle" value="10" />
<property name="maxActive" value="20" />
<!-- Configure the timeout time for getting connection waiting -->
<property name="maxWait" value="6000" />
<!-- Configure how often to test , Detects idle connections that need to be closed , In milliseconds -->
<property name="timeBetweenEvictionRunsMillis" value="2000" />
<!-- Configure the minimum lifetime of a connection in the pool , In milliseconds -->
<property name="minEvictableIdleTimeMillis" value="600000" />
<property name="maxEvictableIdleTimeMillis" value="900000" />
<property name="validationQuery" value="select 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- Configure monitoring statistics interception filters -->
<property name="filters" value="stat" />
</bean></bean>
Well, the later principle is not written
I have no time to learn , Have a look at what the big guys have sorted out
Make complaints
It's hard to learn a habit , Now? Java Ecology is getting better , There are more and more messy old knowledge left , Ah , For a new person , There is no teacher to teach , It's hard to learn .
Online articles are also messy , I don't know what to say , The ranking is quite high
It may also be that I have no good way , There are no good resources , If you guys have " Very orthodox " new Java Learning resources 、 The team can pull me down , Seeking to take flight 
边栏推荐
- 看问题的角度
- Data link layer protocol -- Ethernet protocol
- Current characteristics of steering gear with great resistance
- Construction of Seata multilingual system
- What causes the wait event of TCP socket (kgas) in oracle?
- 开源之夏专访|“00 后” PMC member 白泽平
- [golang from introduction to practice] poker licensing game
- tiny-emitter.js:一个小型的事件订阅发布库
- Has baozi ever played in the multi merchant system?
- Impala2.12 environmental installation
猜你喜欢

Do you really understand images? (machine vision)

01 create project warehouse

GDT,LDT,GDTR,LDTR

中创算力荣获「2022年科技型中小企业」认定
![[ CTF 学习 ] CTF 中的隐写集合 —— 图片隐写术](/img/32/2da78bd5866cfab9ee64dfcb1c1204.png)
[ CTF 学习 ] CTF 中的隐写集合 —— 图片隐写术

# 1. Excel的IF函数

Pandora IOT development board learning (RT thread) - Experiment 16 WiFi module experiment (learning notes)

How to test cookies

Druid连接池——从0开始坚强的一点点的自学,Druid一点不懂的可以点进来,懂得别点进来,点进来你会嫌我啰嗦的

To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China
随机推荐
When the development of the meta universe begins to show more and more the style of the Internet, we need to be vigilant
Salt and ice particles cannot be distinguished
[detailed tutorial] a thorough article on mongodb aggregation query
【浅析STM32之GPIO寄存器(CRL/CRH)配置 】
Dry goods | Ctrip Hongmeng application development practice
Libenent and libev
Hbuilderx eslint configuration
Sony announced the closure of Beijing mobile phone factory! The production line will be moved to Thailand, and the cost can be reduced by half!
深入掌握Pod
Perspective
Impala2.12 environmental installation
Win11 experience
MongoDB的安全认证详解
Open source summer interview | "after 00" PMC member Bai Zeping
数据链路层协议 ——— 以太网协议
Properties of trees
Serial adder / subtracter
[cloud picture theory] 247 first introduction to Huawei cloud analysis service
实战|记一次攻防演练打点
ADS1256 debugging notes based on stm32hal Library