当前位置:网站首页>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
边栏推荐
- Tensorflow and torch code verify whether CUDA is successfully installed
- PMP每日一练 | 考试不迷路-7.6
- Reptiles have a good time. Are you full? These three bottom lines must not be touched!
- Qlabel marquee text display
- 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
- AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
- Interview assault 63: how to remove duplication in MySQL?
- It's super detailed in history. It's too late for you to read this information if you want to find a job
- [translation] a GPU approach to particle physics
- Leetcode topic [array] - 119 Yang Hui triangle II
猜你喜欢
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Computer network: sorting out common network interview questions (I)
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
php+redis实现超时取消订单功能
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
[depth first search] Ji suanke: a joke of replacement
Understanding disentangling in β- VAE paper reading notes
helm部署etcd集群
随机推荐
ACTF 2022圆满落幕,0ops战队二连冠!!
Pytorch common loss function
Translation D28 (with AC code POJ 26:the nearest number)
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
First day of rhcsa study
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
Excel 中VBA脚本的简单应用
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
CPU负载很低,loadavg很高处理方法
保证接口数据安全的10种方案
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
应用使用Druid连接池经常性断链问题分析
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
ModuleNotFoundError: No module named ‘PIL‘解决方法
About static type, dynamic type, ID, instancetype
QLabel 跑马灯文字显示
Leetcode topic [array] - 119 Yang Hui triangle II