当前位置:网站首页>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
边栏推荐
- ModuleNotFoundError: No module named ‘PIL‘解决方法
- It's super detailed in history. It's too late for you to read this information if you want to find a job
- Understanding disentangling in β- VAE paper reading notes
- In 50W, what have I done right?
- MATLAB中deg2rad和rad2deg函数的使用
- Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
- [depth first search] Ji suanke: find numbers
- 关于静态类型、动态类型、id、instancetype
- MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
猜你喜欢
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
Documents to be used in IC design process
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
关于静态类型、动态类型、id、instancetype
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
深入分析,Android面试真题解析火爆全网
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
PMP每日一练 | 考试不迷路-7.6
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
随机推荐
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Tensorflow and torch code verify whether CUDA is successfully installed
ROS自定义消息发布订阅示例
Abstract classes and abstract methods
Test technology stack arrangement -- self cultivation of test development engineers
Camel case with Hungarian notation
黑馬--Redis篇
Looting iii[post sequence traversal and backtracking + dynamic planning]
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
Problems encountered in using RT thread component fish
[paper notes] transunet: transformers make strongencoders for medical image segmentation
思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
If you have any problems, you can contact me. A rookie ~
青龙面板最近的库
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色