当前位置:网站首页>Interviewer: why does database connection consume resources? Where are the resources consumed?
Interviewer: why does database connection consume resources? Where are the resources consumed?
2022-06-29 07:42:00 【xy29981】
*
Developing applications for a long time , Always want to get to the bottom , Especially for some questions with public answers . Everyone can explain , But in the final analysis , Can't explain clearly . Everything has a reason , And it is the most intuitive to explain the problem with numbers .
*
This article mainly wants to explore the details of connecting to the database , Especially in Web In the application, the database should be used to connect to the pool , Avoid re establishing a connection every time you send a request . For this question , The answers are consistent , Establishing a database connection is time-consuming , But how much time does it take , What are the time-consuming aspects ?
This paper is connected with MySQL Database, for example , because MySQL The database is open source , Its communication protocol is open , So we can analyze the whole process of establishing connection in detail .
*
In this paper , The analysis of resource consumption mainly focuses on the network , Of course , Resources also include memory 、CPU Equal computing resources , The programming language used is
Java, However, it is not ruled out that programming languages will also have a certain impact .*
First, let's take a look at the connection to the database Java Code , as follows :
Class.forName("com.mysql.jdbc.Driver");
String name = "xttblog2";
String password = "123456";
String url = "jdbc:mysql://172.16.100.131:3306/xttblog2";
Connection conn = DriverManager.getConnection(url, name, password);
// Then the program terminates , The connection was forced to close
And then through 「Wireshark」 Analyze the whole connection establishment process , as follows :

Wireshark Grab the bag
In the connection process shown in the figure above , It can be seen that MySQL The communication protocol is based on TCP Transmission protocol , And the protocol is binary , It's not like HTTP Text protocol for , The process of establishing connection is as follows :
The first 1 Step : establish
TCPConnect , Through three handshakes ;The first 2 Step : The server sends it to the client 「 Handshake messages 」 , The client responds to the handshake message ;
The first 3 Step : client 「 Send authentication package 」 , For user authentication , After successful verification , Server return OK Respond to , Then start executing the command ;
After successful user authentication , Some connection variables will be set , For example, the character set 、 Whether to automatically commit transactions, etc , There will be multiple data interactions . After completing these steps , Will perform real data query, update and other operations .
In the test of this paper , It only took 5 Line of code to establish a connection , But no operation is performed through this connection , So after the program is executed , The connection is not through Connection.close() closed , But because the program is finished , Cause the process to terminate , Cause the connection with the database to close abnormally , So in the end TCP Of RST message . In this simplest code , No additional connection properties have been set , Therefore, the time spent on setting properties can be considered to be the least ( Actually , We didn't set any properties , But the driver still sets the character set 、 Automatic transaction submission, etc , This depends on the specific driver implementation ), So the whole connection takes the least time . But statistics show that , Excluding the last TCP Of RST When the message ( Because the message does not require the server to return any response ), However, there is still a round trip between the client and the server 「7」 Time ,「 That is, complete a connection , It can be said that , Data needs at least a round trip between the client and the server 7 Time 」 , In terms of time , From the beginning TCP Three handshakes of , Until the final connection is forcibly disconnected ( Not including the last RST message ), Total cost :
10.416042 - 10.190799 = 0.225243s = **225.243ms**!!!
It means , Establishing a database connection requires 225ms, And this can still be regarded as the least , Of course 「 The time spent may be affected by network conditions 、 Database server performance and application code efficiency 」 , But here is just the simplest example , It's enough to say !
Because the above program terminated abnormally , But in a normal application , The connection is usually closed through Connection.close() Accomplished , The code is as follows :
Class.forName("com.mysql.jdbc.Driver");
String name = "shine_user";
String password = "123";
String url = "jdbc:mysql://172.16.100.131:3306/clever_mg_test";
Connection conn = DriverManager.getConnection(url, name, password);
conn.close();
In this case , Things have changed , It is mainly reflected in the disconnection from the database , Here's the picture :

Network packet capture
The first 1 Step : At this time
MySQLCommunication protocol phase , The client sends a request to close the connection , And you don't have to wait for the response from the server ;The first 2 Step :TCP disconnect ,4 Complete the connection and disconnection with one wave ;
Here is the complete completion from the establishment to the closure of the database connection , The whole process took a long time :
747.284311 - 747.100954 = 0.183357s = 183.357ms
There may also be the impact of network conditions , More than the above 225ms Less , But it has almost reached 200ms The level of .
So here comes the question , Imagine the scene , For a day job 2 For WAN's website , Assume that each user will only send 5 A request , So one day is 10 Million requests , For establishing database connection , Let's calculate conservatively as 150ms Okay , How much time do you spend establishing database connections in a day ( It does not include performing query and update operations ):
100000 * 150ms = 15000000ms = 15000s = 250min = 4.17h
In other words, the time spent on establishing database connection every day has reached 「4 Hours 」 , So database connection pool is a must , And when the day's activity increases , Using database connection pool alone can't completely guarantee the normal operation of your service , Other solutions need to be considered :
cache
SQLPrecompilationLoad balancing
……
Of course, this is not the main content of this article ,「 The core idea of this paper is only one , Database connection is really time-consuming , So don't connect frequently 」
Please look forward to my next article , thank you .

more java Advanced information , Interview information , Official account
边栏推荐
- 【科普资料】从科学精神到科学知识的材料
- tf.to_int64
- Markdown skill tree (4): link
- Machine learning notes - time series prediction using machine learning
- tf.count_nonzero
- How to talk about salary correctly in software test interview?
- Viewing application and installation of Hana database license
- tf. to_ int64
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- A hybrid model of machine learning notes time series
猜你喜欢

Blue Bridge Cup - minimum frame

Appium automation test foundation ADB common commands (II)

解题-->在线OJ(十三)

【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解

施努卡:3d机器视觉检测系统 3d视觉检测应用行业

编译原理王者之路

excel高级绘图技巧100讲(六)-甘特图在项目进度上的实战应用案例

Concurrent idempotent anti shake

Imx6dl4.1.15 supports EIM bus (upper) - actual operation and modification.

matlab 多普勒效应产生振动信号和处理
随机推荐
Vibration signal generation and processing based on MATLAB Doppler effect
Schnuka: 3D visual inspection scheme 3D visual inspection application industry
Using cdockablepane to realize floating window in MFC
施努卡:视觉定位系统厂家 什么是视觉定位系统
Markdown skill tree (7): separator and reference
tf.compat.v1.global_variables
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Comment choisir les fournisseurs de marques CRM dans l'industrie du vêtement?
Use of LSTM neural network and general neural network
项目中 if else 的代替写法
Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.
MFC中利用CDockablePane实现悬浮窗
Dump (cl\alv\tree\base================================cp|set\items\for\column) when expanding node or clicking toolbar button
部署Prometheus-server服务 system管理
ES中配置ext.dic文件不生效的原因
施努卡:轮胎自动抓取安装,3D视觉定位,机器人自动抓取
Schnuka: 3D visual recognition system 3D visual inspection principle
postman预处理/前置条件Pre-request
阿里云访问资源:NoSuchKey
施努卡:3d视觉检测方案 3d视觉检测应用行业