当前位置:网站首页>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
边栏推荐
- helm部署etcd集群
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- usb host 驱动 - UVC 掉包
- Take a look at how cabloyjs workflow engine implements activiti boundary events
- 关于静态类型、动态类型、id、instancetype
- 通俗的讲解,带你入门协程
- Black Horse - - Redis Chapter
- Analysis of frequent chain breaks in applications using Druid connection pools
- Detailed idea and code implementation of infix expression to suffix expression
- R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
猜你喜欢

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

第五期个人能力认证考核通过名单公布

思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理

Interface test tool - postman
深入分析,Android面试真题解析火爆全网
Benefit a lot, Android interview questions

Synchronous development of business and application: strategic suggestions for application modernization

PMP practice once a day | don't get lost in the exam -7.6

如何提高网站权重

Druid 数据库连接池 详解
随机推荐
Synchronous development of business and application: strategic suggestions for application modernization
R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
ROS custom message publishing subscription example
English topic assignment (25)
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Tensorflow and torch code verify whether CUDA is successfully installed
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
PMP practice once a day | don't get lost in the exam -7.6
Use map function and split function to type multiple elements in one line
Word如何显示修改痕迹
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
Druid 数据库连接池 详解
A method of removing text blur based on pixel repair
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
Php+redis realizes the function of canceling orders over time
数学知识——高斯消元(初等行变换解方程组)代码实现