当前位置:网站首页>Druid 数据库连接池 详解
Druid 数据库连接池 详解
2022-07-06 11:22:00 【斯文~】
推荐阅读:JDBC详解
概述
1.数据库连接池是个容器,负责分配、管理数据库连接(Connection);
2.它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;
3.释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏;
可以类比线程池去理解。
优点
1.资源重用
2.提升系统响应速度
3.避免数据库连接遗漏
连接池是在一开始就创建好了一些连接(Connection)对象存储起来。用户需要连接数据库时,不需要自己创建连接,而只需要从连接池中获取一个连接进行使用,使用完毕后再将连接对象归还给连接池;这样就可以起到资源重用,也节省了频繁创建连接销毁连接所花费的时间,从而提升了系统响应的速度。
常见的数据库连接池: DBCP、C3P0、Druid
我们现在使用更多的是Druid,它的性能比其他两个会好一些。
Druid连接池是阿里巴巴开源的数据库连接池项目,功能强大,性能优秀,是Java语言最好的数据库连接池之一。
数据库连接池实现
标准接口:DataSource
官方(SUN) 提供的数据库连接池标准接口,由第三方组织实现此接口。该接口提供了获取连接的功能:Connection getConnection()
那么以后就不需要通过 DriverManager
对象获取 Connection
对象,而是通过连接池(DataSource)获取 Connection
对象。
Driud使用流程
1.导入jar包 druid-1.1.12.jar
2.定义配置文件 druid.properties
3.加载配置文件
4.获取数据库连接池对象
5.获取连接
代码示例
配置文件druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/sd?useSSL=false&useServerPrepStmts=true
username=root
password=root
# 初始连接数
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
Java代码
public static void main(String[] args) throws Exception {
//1.导入jar包
//2.定义配置文件
//3. 加载配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("C:\\Users\\斯文\\Desktop\\Java\\demo\\index\\src\\druid.properties"));
//4. 获取连接池对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//5. 获取数据库连接 Connection
Connection conn=dataSource.getConnection();
//6.操作数据库
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);
}
}
推荐阅读:JDBC详解
边栏推荐
- 受益匪浅,安卓面试问题
- Leetcode topic [array] - 119 Yang Hui triangle II
- php+redis实现超时取消订单功能
- [paper notes] transunet: transformers make strongencoders for medical image segmentation
- When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
- On AAE
- 五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
- Pytorch common loss function
- 【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
- Tensorflow and torch code verify whether CUDA is successfully installed
猜你喜欢
Deep circulation network long-term blood pressure prediction [translation]
Helm deploy etcd cluster
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Computer network: sorting out common network interview questions (I)
MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
[depth first search] Ji suanke: Square
A wearable arm device for night and sleeveless blood pressure measurement [translation]
Pytorch common loss function
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
随机推荐
R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
[depth first search] Ji suanke: a joke of replacement
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
Don't miss this underestimated movie because of controversy!
上海部分招工市场对新冠阳性康复者拒绝招录
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
Leetcode topic [array] - 119 Yang Hui triangle II
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
Digital "new" operation and maintenance of energy industry
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
Helm deploy etcd cluster
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
第五期个人能力认证考核通过名单公布
The list of people who passed the fifth phase of personal ability certification assessment was published
The nearest library of Qinglong panel
Installation and management procedures
Multithreading Basics: basic concepts of threads and creation of threads