当前位置:网站首页>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
边栏推荐
- Simple understanding of MySQL database
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- tensorflow和torch代码验证cuda是否安装成功
- 全套教学资料,阿里快手拼多多等7家大厂Android面试真题
- Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
- Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
- C # - realize serialization with Marshall class
- Based on butterfly species recognition
- Characteristic colleges and universities, jointly build Netease Industrial College
- R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
猜你喜欢
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
反射及在运用过程中出现的IllegalAccessException异常
Multithreading Basics: basic concepts of threads and creation of threads
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
About NPM install error 1
ACTF 2022圆满落幕,0ops战队二连冠!!
通俗的讲解,带你入门协程
Looting iii[post sequence traversal and backtracking + dynamic planning]
Druid 数据库连接池 详解
随机推荐
黑馬--Redis篇
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
Qlabel marquee text display
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
使用map函数、split函数一行键入多个元素
业务与应用同步发展:应用现代化的策略建议
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
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
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
安装Mysql报错:Could not create or access the registry key needed for the...
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
Precautions for binding shortcut keys of QPushButton
深入分析,Android面试真题解析火爆全网
IC设计流程中需要使用到的文件
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
Interface test tool - postman
MATLAB中deg2rad和rad2deg函数的使用
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting