当前位置:网站首页>Druid database connection pool details
Druid database connection pool details
2022-07-06 19:20:00 【Gentle ~】
Recommended reading :JDBC Detailed explanation
List of articles
summary
1. Database connection pool is a container , To be responsible for the distribution of 、 Manage database connections (Connection);
2. It allows applications to reuse an existing database connection , Instead of building a new ;
3. Release database connection with idle time exceeding the maximum idle time to avoid missing database connection caused by no free database connection ;
It can be understood by analogy with thread pool .
advantage
1. Resource reuse
2. Improve system response speed
3. Avoid missing database connections

Connection pool is to create some connections at the beginning (Connection) Objects stored . When users need to connect to the database , You don't need to create your own connection , You only need to get a connection from the connection pool for use , Return the connection object to the connection pool after use ; In this way, we can reuse resources , It also saves time for frequent connection creation and connection destruction , Thus, the response speed of the system is improved .
Common database connection pool : DBCP、C3P0、Druid
Now we use more Druid, Its performance will be better than the other two .
Druid Connection pool is an open source database connection pool project of Alibaba , Powerful , Excellent performance , yes Java One of the best database connection pools in language .
Database connection pool implementation
Standard interface :DataSource
official (SUN) Database connection pool standard interface provided , This interface is implemented by a third-party organization . This interface provides the function of obtaining connection :Connection getConnection()
Then you don't need to pass DriverManager Object acquisition Connection object , But through the connection pool (DataSource) obtain Connection object .
Driud Usage flow
1. Import jar package druid-1.1.12.jar
2. Define configuration file druid.properties
3. Load profile
4. Get database connection pool object
5. Get the connection

Code example
The configuration file druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/sd?useSSL=false&useServerPrepStmts=true
username=root
password=root
# The number of initial connections
initialSize=5
# maximum connection
maxActive=10
# Maximum waiting time
maxWait=3000
Java Code
public static void main(String[] args) throws Exception {
//1. Import jar package
//2. Define configuration file
//3. Load profile
Properties prop=new Properties();
prop.load(new FileInputStream("C:\\Users\\ Sven \\Desktop\\Java\\demo\\index\\src\\druid.properties"));
//4. Get connection pool object
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//5. Get database connection Connection
Connection conn=dataSource.getConnection();
//6. Operating the database
String sql = "select * from st";
Statement stmt=conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(id + " " + name + " " + age);
}
}
Recommended reading :JDBC Detailed explanation
边栏推荐
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
- Based on butterfly species recognition
- 驼峰式与下划线命名规则(Camel case With hungarian notation)
- [depth first search] Ji suanke: find numbers
- R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
- 如何提高网站权重
- Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
- 冒烟测试怎么做
- Computer network: sorting out common network interview questions (I)
- Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
猜你喜欢

Php+redis realizes the function of canceling orders over time

史上超级详细,想找工作的你还不看这份资料就晚了

Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform

Interface test tool - postman

It's super detailed in history. It's too late for you to read this information if you want to find a job

openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
![Looting iii[post sequence traversal and backtracking + dynamic planning]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
Looting iii[post sequence traversal and backtracking + dynamic planning]

Cereals Mall - Distributed Advanced p129~p339 (end)

Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth

Dark horse -- redis
随机推荐
Qlabel marquee text display
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
Use of deg2rad and rad2deg functions in MATLAB
Druid 数据库连接池 详解
pytorch常见损失函数
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
In 50W, what have I done right?
CPU负载很低,loadavg很高处理方法
Interface test tool - postman
USB host driver - UVC swap
R语言使用dt函数生成t分布密度函数数据、使用plot函数可视化t分布密度函数数据(t Distribution)
Take a look at how cabloyjs workflow engine implements activiti boundary events
R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
tensorflow和torch代码验证cuda是否安装成功
swagger2报错Illegal DefaultValue null for parameter type integer
About NPM install error 1
受益匪浅,安卓面试问题
应用使用Druid连接池经常性断链问题分析
Test technology stack arrangement -- self cultivation of test development engineers
中缀表达式转后缀表达式详细思路及代码实现