Database connection dynamic allocation method, device, computer equipment and storage medium
By dynamically adjusting the interface weight value in the database connection pool, and dynamically allocating database connections based on the number of query connections and success rates, the problem of inefficient utilization caused by fixed connection pool configuration parameters in the existing technology is solved, and more efficient database connection management is achieved.
Patent Information
- Application Number
- CN202310856456.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-12
- Publication Date
- 2025-04-04
- Estimated Expiration
- 2043-07-12
AI Technical Summary
The configuration parameters of the existing database connection pool remain unchanged during the operation of the information system and cannot adapt to the changing needs during actual use, resulting in the database connections in the connection pool being unable to be used to the maximum extent.
By obtaining the number of query connections and query connection success rates of the target database within the preset time period from the current time, adjusting the interface weight value, and dynamically allocating database connections in the connection pool to maximize the use of database connections.
It realizes dynamic adjustment of connection allocation in the database connection pool according to actual needs, ensuring that the database connection in the connection pool is maximized, and improving the flexibility and efficiency of the system.
Smart Images

Figure CN116932622B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer network technology, and in particular to a method, device, computer equipment and storage medium for dynamically allocating database connections. Background Art
[0002] At present, in order to fully utilize database resources to provide business support, data source connection pool technology is often used. The configuration of the database connection pool is usually achieved by pre-entering configuration parameters.
[0003] However, since the configuration parameters remain unchanged during the operation of the information system, it cannot adapt well to the changing needs in actual use. For example, the C3P0 connection pool (an open source JDBC connection pool) initializes a fixed number of connections according to the configuration after the connection pool instance is established, and provides them for the program to connect to the database instance. Summary of the invention
[0004] In view of the defects existing in the prior art, the object of the present invention is to provide a method, apparatus, computer equipment and storage medium for dynamically allocating database connections, which can maximize the utilization of database connections in a connection pool.
[0005] To achieve the above purpose, the present invention provides a method for dynamically allocating database connections, which specifically includes the following steps:
[0006] Obtain the number of query connections and the query connection success rate of the target interface instance of the target database within a preset time period from the current time;
[0007] Obtaining an interface weight value of the target interface instance at a previous moment, wherein the difference between the current moment and the previous moment is a polling query data cycle of the target database;
[0008] Modifying the interface weight value at the previous moment according to the query connection number and the query connection success rate to obtain the interface weight value of the target interface instance at the current moment;
[0009] The database connection of the target interface instance at the current moment in the connection pool is dynamically allocated according to the interface weight value.
[0010] On the basis of the above technical solution, before obtaining the query connection times and query connection success rate of the target interface instance of the target database within a preset time period from the current moment, it also includes:
[0011] Obtain environmental operation information and storage data information of each database;
[0012] Based on a preset scoring strategy, the environment operation information and the storage data information of each database are scored and weighted to obtain an interface weight value of the interface instance of each database at an initial moment.
[0013] On the basis of the above technical solution, after dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value, it also includes:
[0014] Obtaining the number of idle database connections in the connection pool, wherein the idle database connections are database connections in the connection pool that are not allocated to a database;
[0015] If the number of connections is less than or equal to a first preset threshold, releasing part of the database connections allocated to each database;
[0016] If the number of connections is greater than the first preset threshold and less than the second preset threshold, then maintaining the number of connections of the current idle database connections;
[0017] If the number of connections is greater than or equal to a second preset threshold, some idle database connections in the connection pool are allocated to each database.
[0018] On the basis of the above technical solution, if the number of connections is less than or equal to the first preset threshold, releasing part of the database connections allocated to each database, the specific steps include:
[0019] If the number of connections is less than or equal to a first preset threshold, obtaining the interface weight value of the interface instance of each database at the current moment;
[0020] Determine a first category of databases that are less than the average weight value according to the interface weight values of the interface instances of the databases at the current moment;
[0021] Obtaining a connection usage rate of a database connection of the first type of database;
[0022] According to the connection usage rate of the first-category database, some database connections in some first-category databases are released.
[0023] On the basis of the above technical solution, if the number of connections is greater than or equal to the second preset threshold, some idle database connections in the connection pool are allocated to each database, and the specific steps include:
[0024] If the number of connections is greater than or equal to a second preset threshold, obtaining the interface weight value of the interface instance of each database at the current moment;
[0025] Determine a second category of databases that is greater than or equal to the weight average value according to the interface weight values of the interface instances of the databases at the current moment;
[0026] Obtaining a connection usage rate of a database connection of the second type of database;
[0027] According to the connection usage rate of the second-category database, some idle database connections in the connection pool are allocated to some of the second-category databases.
[0028] On the basis of the above technical solution, after dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value, it also includes:
[0029] Dividing the preset time period into a plurality of time segments;
[0030] Calculate the number of query connections of the target interface instance in each time segment according to the number of query connections;
[0031] Obtain a target time segment with the same time attribute as the current time in each time segment;
[0032] The database connection of the target interface instance at the current moment is adjusted according to the query connection number of the target time segment to obtain a revised database connection of the target interface instance at the current moment.
[0033] On the basis of the above technical solution, after dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value, it also includes:
[0034] Obtain the monitoring item operating parameters of each database, including CPU usage, memory utilization, connection status, disk usage, and log space usage;
[0035] If the operating parameters of any monitoring item exceed the corresponding preset monitoring threshold, an alarm will be issued.
[0036] The present invention provides a database connection dynamic allocation device, comprising:
[0037] An information acquisition module is used to obtain the number of query connections and the query connection success rate of the target interface instance of the target database within a preset time period from the current moment; obtain the interface weight value of the target interface instance at the previous moment, and the difference between the current moment and the previous moment is the polling query data cycle of the target database;
[0038] A weight adjustment module, which is in communication with the information acquisition module and is used to modify the interface weight value at the previous moment according to the query connection number and the query connection success rate, to obtain the interface weight value of the target interface instance at the current moment;
[0039] The connection adjustment module is in communication connection with the weight adjustment module and is used for dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value.
[0040] The present invention provides a computer device, the computer device comprising:
[0041] one or more processors;
[0042] a memory; and one or more application programs, wherein the one or more application programs are stored in the memory and configured to be executed by the processor to implement the above-mentioned database connection dynamic allocation method.
[0043] The present invention provides a computer-readable storage medium, on which a computer program is stored. The computer program is loaded by a processor to execute the steps in the above-mentioned database connection dynamic allocation method.
[0044] Compared with the prior art, the advantages of the present invention are: adjusting the interface weight value of the target interface instance based on the query connection times and the query connection success rate of the interface instance every polling query data cycle, and dynamically reallocating the database connection of the target interface instance based on the adjusted interface weight value, so that the database connections in the connection pool are utilized to the maximum extent. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0046] Figure 1 It is a flowchart of a method for dynamically allocating database connections in an embodiment of the present application;
[0047] Figure 2 is a flowchart of a method for dynamically allocating database connections in another embodiment of the present application;
[0048] Figure 3 It is a schematic diagram of the structure of a database connection dynamic allocation device in an embodiment of the present application;
[0049] Figure 4 It is a schematic diagram of the structure of a computer device in an embodiment of the present application. DETAILED DESCRIPTION
[0050] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.
[0051] In the description of this application, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of this application, "plurality" means two or more, unless otherwise clearly and specifically defined.
[0052] In the description of the present application, the word "for example" is used to mean "used as an example, illustration or explanation". Any embodiment described as "for example" in the present application is not necessarily to be construed as being more preferred or advantageous than other embodiments. The following description is given to enable any technician in the field to implement and use the present invention. In the following description, details are listed for the purpose of explanation. It should be understood that a person of ordinary skill in the art can recognize that the present invention can be implemented without using these specific details. In other examples, well-known structures and processes will not be elaborated in detail to avoid obscuring the description of the present invention with unnecessary details. Therefore, the present invention is not intended to be limited to the embodiments shown, but is consistent with the widest scope consistent with the principles and features disclosed in the present application.
[0053] Database connection is a critical and limited resource. The establishment of connection is very resource-intensive. The constant creation and closing of a large number of connections will greatly affect performance. A database connection object corresponds to a physical database connection. Each operation opens a physical connection and closes it after use. The principle of database connection pool is to establish enough database connections when the application starts, and form a connection pool with these connections (in simple terms: a lot of semi-finished database connection objects are placed in a "pool"), and maintain them. The application dynamically applies for, uses and releases the connections in the pool.
[0054] See also Figure 1 The embodiment of the present application provides a method for dynamically allocating database connections, the method comprising steps S101 to S104, which are as follows:
[0055] S101, obtaining the number of query connections and the query connection success rate of the target interface instance of the target database within a preset time period from the current moment.
[0056] Specifically, the target interface instance encapsulates the target database access interface and provides functions such as database connection, data query, and data processing, that is, the various parameters of each database are interfaced. When a user performs a query, the user connects to and accesses the database through the database connection. The database connections in the connection pool include database connections allocated to each database for connecting to the corresponding database, and idle database connections that are not allocated to any database.
[0057] The polling query data cycle is the cycle for dynamic adjustment of the database connections of each database associated with the connection pool, that is, the database connections of each database are dynamically adjusted at the current moment, and the time of the last adjustment is the previous moment. The specific duration of the polling query data cycle can be set based on the processing power and the requirements of dynamic allocation of database connections, and this embodiment does not make specific limitations.
[0058] The preset time period and the polling query data cycle may be the same or different. The number of query connections and the query connection success rate of the target interface instance of the target database within the preset time period from the current moment are obtained. For example, the number of query connections and the query connection success rate of the target interface instance of the target database within one day before the current moment are obtained. The reasons for the query connection failure include but are not limited to timeout failure due to large access volume, unstable database operating environment, etc.
[0059] It should be noted that in order to avoid spending too much time calculating the query connection times and query connection success rate of the statistical target interface instance at the current moment, the query connection times and query connection success rate of this query are recorded and stored after each SQL (Structured Query Language) query request is received.
[0060] In one embodiment, this step includes: S201, obtaining the environment operation information and storage data information of each database; S202, based on a preset scoring strategy, scoring and weighting the environment operation information and storage data information of each database to obtain the interface weight value of the interface instance of each database at the initial moment.
[0061] Specifically, this embodiment dynamically allocates database connections belonging to each database in the connection pool based on the interface weight value of the interface instance of each database at each moment. The initial moment is the moment when each database begins to be used, which can be the first time each database is put into use, or the first time it is used after being updated. The interface weight value of the interface instance at each moment is determined based on the interface weight value of the previous moment and the number of query connections and the query connection success rate in the preset time period from the current moment. At the initial moment, the interface instance of each database has no historical connection data, so the interface weight value at the initial moment is directly pre-set.
[0062] Obtain the environment operation information and storage data information of each database. The environment operation information includes the product hardware information of each database operation, such as CPU (Central Processing Unit) parameters, memory parameters, disk parameters, data storage location, etc. The storage data information includes various information about the data stored in each database, such as database type, stored data type, stored data size, etc.
[0063] Based on the preset scoring strategy, the environmental operation information and storage data information of each database are scored and weighted. In the preset scoring strategy, each item of environmental operation information and storage data information is set with a corresponding score. The scores of each item of environmental operation information and storage data information of the same database are weighted to obtain the interface weight value of the interface instance of the database at the initial moment.
[0064] In addition, the environmental operation information and storage data information of multiple databases, the scores of each environmental operation information and storage data information of the database, and the interface weight value at the initial moment obtained by final weighting can also be used as a training set to train to obtain a scoring model. After that, the environmental operation information and storage data information of the database to be determined are input into the scoring model to obtain the interface weight value of the interface instance of the database at the initial moment.
[0065] At the initial moment, the interface weight value of each database interface instance is directly obtained, and the database connection in the connection pool is allocated to the interface instance of each database based on the interface weight value. More database connections are allocated to the interface instance with a large interface weight value, and fewer database connections are allocated to the interface instance with a small interface weight value. In addition, in order to avoid the database connection in the connection pool being exhausted and unable to be dynamically adjusted in time, resulting in database query being blocked, in addition to the database connection allocated to the interface instance of each database, a certain number of idle database connections need to be reserved in the connection pool, which are not allocated to any database interface instance.
[0066] S102, obtaining the interface weight value of the target interface instance at the last moment, wherein the difference between the current moment and the last moment is the polling query data period of the target database.
[0067] Specifically, the interface weight value of the target interface instance at the last moment is obtained. The last moment is relative to the current moment. The last moment is the last time the database connection is dynamically adjusted. If the current moment is the first time the database connection is dynamically adjusted, the last moment is the initial moment.
[0068] S103, modifying the interface weight value at the previous moment according to the query connection times and the query connection success rate to obtain the interface weight value of the target interface instance at the current moment.
[0069] Specifically, the interface weight value at the previous moment is modified according to the number of query connections and the query connection success rate. The more query connections, the higher the query success rate. The interface weight value at the previous moment is increased to obtain the interface weight value of the target interface instance at the current moment.
[0070] Among them, the adjustment values of the interface weight values corresponding to the query connection times and the query connection success rates are preset, for example, the query connection times in different ranges correspond to different adjustment values of the interface weight values, and the query connection success rates in different ranges correspond to different adjustment values of the interface weight values, and the adjustment values are the numerical values of the increase or decrease of the interface weight values. In addition, the query connection times and the query connection success rates of the target interface instances of the target database obtained at the current moment can be compared with the query connection times and the query connection success rates of the target interface instances of the target database obtained at the previous moment, and the number of times the query connection times at the current moment increases or decreases corresponds to the numerical value of the increase or decrease of the interface weight value, and the number of times the query connection times at the current moment increases or decreases corresponds to the numerical value of the increase or decrease of the interface weight value.
[0071] S104: Dynamically allocate a database connection of the target interface instance at the current moment in a connection pool according to the interface weight value.
[0072] Specifically, the present embodiment allocates the database connections in the connection pool to the interface instances of each database based on the interface weight value. The interface instances with large interface weight values are allocated with more database connections, and the interface instances with small interface weight values are allocated with fewer database connections. Therefore, the database connections of the target interface instances at the current moment in the connection pool are dynamically allocated according to the interface weight value of the target interface instances at the current moment. If the interface weight value of the target interface instances at the current moment decreases relative to the previous moment, the database connections decrease, and vice versa, if the interface weight value of the target interface instances at the current moment increases, the database connections increase.
[0073] In one embodiment, this step is followed by: S301, obtaining the number of idle database connections in the connection pool, where the idle database connections are database connections in the connection pool that are not allocated to a database; S302, if the number of connections is less than or equal to a first preset threshold, releasing part of the database connections allocated to each database; S303, if the number of connections is greater than the first preset threshold and less than the second preset threshold, maintaining the current number of idle database connections; S304, if the number of connections is greater than or equal to the second preset threshold, allocating part of the idle database connections in the connection pool to each database.
[0074] Specifically, in order to avoid the database connections in the connection pool being exhausted and unable to be dynamically adjusted in time, resulting in database query obstruction, in addition to the database connections allocated to the interface instances of each database, a certain number of idle database connections need to be reserved in the connection pool and not allocated to any database interface instance, and it is necessary to ensure that the idle database connections in the connection pool are within a reasonable number range.
[0075] The connection pool sets the minimum and maximum number of database connections during initialization, so the total number of all database connections in the connection pool, including idle and unassigned database connections and database connections that have been assigned to each database, is between the minimum and maximum number of connections.
[0076] Get the number of idle database connections in the connection pool. Idle database connections are database connections that are not allocated to a database in the connection pool. Therefore, if the database connections of a database are exhausted at a certain moment, but there are still query requests for the database, the idle databases in the connection pool can be allocated to meet the connection query requirements.
[0077] If the number of idle database connections in the connection pool is less than or equal to the first preset threshold, it means that the number of idle database connections in the current connection pool is small and may not be able to cope with the temporarily increased query demand of individual databases. Therefore, it is necessary to release some database connections that have been allocated to each database as idle database connection backup.
[0078] If the number of idle database connections in the connection pool is greater than or equal to the second preset threshold, it means that there are many idle database connections in the current connection pool and there are excess resources that are not fully utilized. Therefore, some idle databases in the connection pool need to be allocated to each database.
[0079] If the number of idle database connections in the connection pool is greater than the first preset threshold and less than the second preset threshold, it means that the number of idle database connections in the current connection pool is within a reasonable range. Therefore, the current number of idle database connections is maintained without adjustment.
[0080] Among them, the second preset threshold is greater than the first preset threshold. The first preset threshold and the second preset threshold can be obtained based on the analysis of historical connection data of the connection pool. The historical connection data includes the number of database connections that are idle in the connection pool within a certain target time period and the time points corresponding to each number of connections. In addition, the historical connection data also includes usage data of idle database connections in the connection pool within the target time period. The first preset threshold and the second preset threshold can be analyzed by combining the above information.
[0081] In one embodiment, step S302, if the number of connections is less than or equal to a first preset threshold, then releasing part of the database connections allocated to each database, including: S401, if the number of connections is less than or equal to the first preset threshold, obtaining the interface weight value of the interface instance of each database at the current moment; S402, determining a first category of databases that is less than the weight average value based on the interface weight value of the interface instance of each database at the current moment; S403, obtaining the connection utilization rate of the database connections of the first category of databases; S404, releasing part of the database connections in part of the first category of databases based on the connection utilization rate of the first category of databases.
[0082] Specifically, if the number of idle database connections in the connection pool is less than or equal to the first preset threshold, it is necessary to allocate some idle databases in the connection pool to each database, and the allocated object is determined based on the connection usage data of each database, so the interface weight value of the interface instance of each database at the current moment is obtained. Since the interface weight value of the interface instance of each database at the current moment is adjusted based on the query connection number and the query connection success rate, the interface instance with a lower interface weight value is a database with a relatively low usage frequency or a low connection success rate. According to the interface weight value of the interface instance of each database at the current moment, the first category of databases that are less than the weight average are determined, and some database connections in the first category of databases are released as idle database connections.
[0083] Among them, the connection usage rate of the database connection of the first category of databases is obtained, and the connection usage rate is used to characterize the usage frequency of the database connection assigned to the database. If there are idle database connections for a long time for the database connection assigned to the database, the connection usage rate of the database connection is low. The number of released database connections is determined based on the difference between the number of idle database connections in the connection pool and the first preset threshold, that is, the sum of the number of idle database connections in the connection pool and the number of released database connections is at least greater than the first preset threshold, so it may be necessary to release only some of the database connections in the first category of databases. In addition, if all database connections are released from the same database, it may cause problems in the connection query of the database. Therefore, after selecting the released database object, only some database connections are selected for release.
[0084] Some database connections in the first category database are released according to the connection usage rate of the first category database. For example, according to the number of database connections that need to be released, databases with corresponding numbers of connection usage rates in the first category database from low to high are selected, and one database connection is released respectively. If the number of databases in the first category database is less than the number of database connections that need to be released, then one database connection is released respectively in the databases of the first category database, and then the database connections are released again in order from low to high in connection usage rate.
[0085] In one embodiment, step S304, if the number of connections is greater than or equal to a second preset threshold, partially idle database connections in the connection pool are allocated to each database, including: S501, if the number of connections is greater than or equal to the second preset threshold, obtaining the interface weight value of the interface instance of each database at the current moment; S502, determining a second category of databases that is greater than or equal to the weight average value based on the interface weight value of the interface instance of each database at the current moment; S503, obtaining the connection utilization rate of the database connection of the second category of database; S504, based on the connection utilization rate of the second category of database, allocating partially idle database connections in the connection pool to part of the second category of databases.
[0086] Specifically, if the number of idle database connections in the connection pool is greater than or equal to the second preset threshold, it is necessary to allocate some idle databases in the connection pool to each database, and the allocated object is determined based on the connection usage data of each database, so the interface weight value of the interface instance of each database at the current moment is obtained. Since the interface weight value of the interface instance of each database at the current moment is adjusted based on the query connection number and the query connection success rate, the interface instance with a higher interface weight value is a database with a relatively high usage frequency or a higher connection success rate. According to the interface weight value of the interface instance of each database at the current moment, the second category of databases that are greater than or equal to the weight average value are determined, and the idle databases in the connection pool are allocated to the second category of databases.
[0087] Among them, the connection usage rate of the database connection of the second type of database is obtained, and the number of allocated idle database connections is determined based on the difference between the second preset threshold and the number of idle database connections in the connection pool, that is, the difference between the number of idle database connections in the connection pool and the number of allocated idle database connections is at least less than the first preset threshold, so only some idle database connections can be allocated. In addition, if all allocated idle database connections are allocated to the same database for release, it may cause the number of connections of the database to be redundant and cause resource waste. If all databases are evenly allocated with idle database connections, on the one hand, the number of idle database connections may be insufficient, and on the other hand, individual databases do not need to add new database connections, which may cause resource waste. Therefore, some databases are selected to allocate idle database connections.
[0088] According to the connection usage rate of the second category database, some idle databases in the connection pool are allocated to some second category databases. For example, according to the number of idle database connections that need to be allocated, the databases of the second category database with the corresponding number of connection usage rates from high to low are selected, and an idle database connection is allocated to each of them. If the number of databases of the second category database is less than the number of idle database connections that need to be allocated, an idle database connection is allocated to each of the databases of the second category database, and then the database connections are allocated in order from high to low according to the connection usage rate.
[0089] In one embodiment, step S104, after dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value, also includes: S601, dividing the preset time period into a plurality of time segments; S602, calculating the number of query connections of the target interface instance in each time segment according to the number of query connections; S603, obtaining the target time segment with the same time attribute as the current moment in each time segment; S604, adjusting the database connection of the target interface instance at the current moment according to the number of query connections of the target time segment, to obtain the revised database connection of the target interface instance at the current moment.
[0090] Specifically, user activity varies in different time periods, and the query requirements for different types of databases are different. Therefore, the time periods can be further divided to analyze the connection requirements of each database, so that the allocated database connections can be utilized to the maximum extent and avoid resource waste.
[0091] The preset time period is divided into several time segments, and the query connection times of the target interface instance in each time segment are calculated according to the query connection times. For example, one hour is used as a time segment, and the query connection times of the target interface instance in each hour are counted respectively.
[0092] Obtain the target time segment with the same time attribute as the current moment in each time segment, the time attribute is the time segment to which the current moment belongs, for example, if the current moment belongs to the current 17:00 period, then the target time segment is the 17:00 period every day in the preset time period. The query connection of the database under the same time attribute generally has the same rule, so the database connection of the target interface instance at the current moment is adjusted according to the query connection number of the target time segment to obtain the revised database connection of the target interface instance at the current moment. For example, the standard query connection number of the polling query data cycle in the target time segment is determined by fitting the query connection number of multiple target time segments, and the database connection of the target interface instance at the current moment is adjusted based on the standard query connection number. For example, if the difference between the number of database connections of the target interface instance at the current moment and the standard query connection number is within the preset range, no adjustment is made. If the difference is too large, the number of connections is increased or decreased by a preset number based on the difference to obtain the revised database connection of the target interface instance.
[0093] In one embodiment, step S104, after dynamically allocating the database connection of the target interface instance in the connection pool at the current moment according to the interface weight value, also includes: S701, obtaining the monitoring item operating parameters of each database, the operating parameters including CPU usage, memory utilization, connection status, disk usage, and log space usage; S702, if any monitoring item operating parameter exceeds the corresponding preset monitoring threshold, an alarm is issued.
[0094] Specifically, the monitoring items of each database are interfaced to obtain the monitoring item operating parameters of each database, including CPU usage, memory utilization, connection status, disk usage, and log space usage. If any monitoring item operating parameter exceeds the corresponding preset monitoring threshold, an alarm is issued. The preset monitoring threshold of each monitoring item operating parameter can be set and modified according to requirements, and this embodiment does not make specific limitations.
[0095] In addition, if Figure 2 As shown, after receiving the target database connection request, the interface weight value of the database connection of the target interface instance at the current moment is calculated according to the method steps of the above embodiment, and then the database connection of the target interface instance at the current moment in the connection pool is dynamically allocated according to the interface weight value. When the interface weight value of the database connection of the target interface instance at the current moment is high, it is supplemented from the idle database connection. When the interface weight value of the database connection of the target interface instance at the current moment is low, the database request of part of the target database is released. In addition, after the connection request, the operating parameters of each monitoring item are stored, and the query connection times and query connection success rate of this query are recorded and stored.
[0096] like Figure 3As shown in the figure, the client creates an interface instance and configures connection pool related parameters. The server dynamically obtains database connections through the connection pool according to the interface weight value, adjusts the number of database connections owned by the corresponding database according to the query situation (number of query connections and query connection success rate) and the interface weight value of the interface instance itself, obtains different monitoring item operation parameters according to different SQLs, caches the queried data in redis and time series databases, and uses scheduled tasks and websockets to push them to the client front-end page for display.
[0097] In this embodiment, the interface weight value of the target interface instance is adjusted based on the query connection times and query connection success rate of the interface instance every polling query data cycle, and the database connection of the target interface instance is dynamically reallocated based on the adjusted interface weight value, so that the database connection in the connection pool is utilized to the maximum extent. In addition, a certain number of idle database connections are reserved in the connection pool to prevent the temporary increase of connection requirements of individual databases, and the idle database connections in the connection pool are maintained within a reasonable dynamic range (between the first preset threshold and the second preset threshold), that is, while meeting the standby requirements, avoiding resource waste. Two types of database connections are configured in the connection pool, and the two types of database connections are dynamically allocated to each other, so that the database connection resources are utilized to the maximum extent.
[0098] like Figure 4 As shown, the embodiment of the present application provides a database connection dynamic allocation device 900, including:
[0099] The information acquisition module 910 is used to obtain the query connection times and query connection success rate of the target interface instance of the target database within a preset time period from the current moment; obtain the interface weight value of the target interface instance at the previous moment, and the difference between the current moment and the previous moment is the polling query data cycle of the target database;
[0100] The weight adjustment module 920 is in communication with the information acquisition module 910 and is used to modify the interface weight value at the previous moment according to the query connection number and the query connection success rate to obtain the interface weight value of the target interface instance at the current moment;
[0101] The connection adjustment module 930 is in communication connection with the weight adjustment module 920 and is used for dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value.
[0102] In some embodiments of the present application, the information acquisition module 910 is also used to obtain the environmental operation information and storage data information of each database; based on a preset scoring strategy, the environmental operation information and the storage data information of each database are scored and weighted to obtain the interface weight value of the interface instance of each database at the initial moment.
[0103] In some embodiments of the present application, the connection adjustment module 930 is also used to obtain the number of idle database connections in the connection pool, where the idle database connections are database connections in the connection pool that are not allocated to a database; if the number of connections is less than or equal to a first preset threshold, part of the database connections allocated to each database are released; if the number of connections is greater than the first preset threshold and less than the second preset threshold, the current number of idle database connections is maintained; if the number of connections is greater than or equal to the second preset threshold, part of the idle databases in the connection pool are allocated to each database.
[0104] In some embodiments of the present application, the connection adjustment module 930 is also used to obtain the interface weight value of the interface instance of each database at a current moment if the number of connections is less than or equal to a first preset threshold; determine a first category of databases that are less than the weight average value based on the interface weight value of the interface instance of each database at a current moment; obtain the connection usage rate of the database connection of the first category of databases; and release some database connections in some first category databases based on the connection usage rate of the first category of databases.
[0105] In some embodiments of the present application, the connection adjustment module 930 is also used to obtain the interface weight value of the interface instance of each database at a current moment if the number of connections is greater than or equal to a second preset threshold; determine a second category of databases that are greater than or equal to the weight average value based on the interface weight value of the interface instance of each database at a current moment; obtain the connection usage rate of the database connection of the second category of database; and allocate some idle databases in the connection pool to some of the second category of databases based on the connection usage rate of the second category of databases.
[0106] In some embodiments of the present application, the connection adjustment module 930 is also used to divide the preset time period into several time segments; calculate the number of query connections of the target interface instance in each time segment based on the number of query connections; obtain the target time segment with the same time attribute as the current moment in each time segment; adjust the database connection of the target interface instance at the current moment based on the number of query connections of the target time segment, and obtain the corrected database connection of the target interface instance at the current moment.
[0107] In some embodiments of the present application, the connection adjustment module 930 is also used to obtain the monitoring item operating parameters of each database, and the operating parameters include CPU usage, memory utilization, connection status, disk usage, and log space usage; if any monitoring item operating parameter exceeds the corresponding preset monitoring threshold, an alarm is issued.
[0108] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0109] In some embodiments of the present application, a computer device is provided, comprising one or more processors; a memory; and one or more applications, wherein the one or more applications are stored in the memory and configured to be executed by the processor in the steps of the above-mentioned method for dynamically allocating database connections. The steps of the method for dynamically allocating database connections here may be the steps of the method for dynamically allocating database connections in the above-mentioned embodiments.
[0110] In some embodiments of the present application, a computer-readable storage medium is provided, which stores a computer program, and the computer program is loaded by a processor, so that the processor executes the steps of the above-mentioned database connection dynamic allocation method. The steps of the database connection dynamic allocation method here can be the steps of the database connection dynamic allocation method in each of the above-mentioned embodiments.
[0111] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Any reference to memory, storage, database or other media used in the embodiments provided in this application may include at least one of non-volatile and volatile memory. Non-volatile memory may include read-only memory (ROM), magnetic tape, floppy disk, flash memory or optical memory, etc. Volatile memory may include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM).
[0112] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0113] The above is a detailed introduction to a database connection dynamic allocation method, device, computer equipment and storage medium provided in the embodiments of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A method for dynamically allocating database connections, characterized in that: The specific steps include: Obtain the number of query connections and the query connection success rate of the target interface instance of the target database within a preset time period from the current time; Obtaining an interface weight value of the target interface instance at a previous moment, wherein the difference between the current moment and the previous moment is a polling query data period of the target database; Modifying the interface weight value at the previous moment according to the query connection number and the query connection success rate to obtain the interface weight value of the target interface instance at the current moment; Dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value; Among them, the interface weight value of the previous moment is modified according to the number of query connections and the query connection success rate. The more query connections, the higher the query success rate. The interface weight value is increased on the basis of the interface weight value of the previous moment to obtain the interface weight value of the target interface instance at the current moment. The interface instance with a large interface weight value is allocated more database connections, and the interface instance with a small interface weight value is allocated fewer database connections.
2. A method for dynamically allocating database connections as claimed in claim 1, characterized in that: Before obtaining the query connection count and query connection success rate of the target interface instance of the target database within a preset time period from the current time, it also includes: Obtain environmental operation information and storage data information of each database; Based on a preset scoring strategy, the environment operation information and the storage data information of each database are scored and weighted to obtain an interface weight value of the interface instance of each database at an initial moment.
3. A method for dynamically allocating database connections as claimed in claim 1, characterized in that: After dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value, the method further includes: Obtaining the number of idle database connections in the connection pool, wherein the idle database connections are database connections in the connection pool that are not allocated to a database; If the number of connections is less than or equal to a first preset threshold, releasing part of the database connections allocated to each database; If the number of connections is greater than the first preset threshold and less than the second preset threshold, then maintaining the number of connections of the current idle database connections; If the number of connections is greater than or equal to a second preset threshold, some idle database connections in the connection pool are allocated to each database.
4. A method for dynamically allocating database connections as claimed in claim 3, characterized in that: If the number of connections is less than or equal to a first preset threshold, releasing part of the database connections allocated to each database, the specific steps include: If the number of connections is less than or equal to a first preset threshold, obtaining the interface weight value of the interface instance of each database at the current moment; Determine a first category of databases that are less than the average weight value according to the interface weight values of the interface instances of the databases at the current moment; Obtaining a connection usage rate of a database connection of the first type of database; According to the connection usage rate of the first-category database, some database connections in some first-category databases are released.
5. A method for dynamically allocating database connections as claimed in claim 3, characterized in that: If the number of connections is greater than or equal to a second preset threshold, partially idle database connections in the connection pool are allocated to each database, and the specific steps include: If the number of connections is greater than or equal to a second preset threshold, obtaining the interface weight value of the interface instance of each database at the current moment; Determine a second category of databases that is greater than or equal to the weight average value according to the interface weight values of the interface instances of the databases at the current moment; Obtaining a connection usage rate of a database connection of the second type of database; According to the connection usage rate of the second-category database, some idle database connections in the connection pool are allocated to some of the second-category databases.
6. A method for dynamically allocating database connections according to any one of claims 1 to 5, characterized in that: After dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value, the method further includes: Dividing the preset time period into a plurality of time segments; Calculate the number of query connections of the target interface instance in each time segment according to the number of query connections; Obtain a target time segment with the same time attribute as the current time in each time segment; The database connection of the target interface instance at the current moment is adjusted according to the query connection number of the target time segment to obtain a revised database connection of the target interface instance at the current moment.
7. A method for dynamically allocating database connections according to any one of claims 1 to 5, characterized in that: After dynamically allocating the database connection of the target interface instance at the current moment in the connection pool according to the interface weight value, the method further includes: Obtain the monitoring item operating parameters of each database, including CPU usage, memory utilization, connection status, disk usage, and log space usage; If the operating parameters of any monitoring item exceed the corresponding preset monitoring threshold, an alarm will be issued.
8. A database connection dynamic allocation device, characterized in that: include: An information acquisition module is used to obtain the number of query connections and the query connection success rate of the target interface instance of the target database within a preset time period from the current moment; obtain the interface weight value of the target interface instance at the previous moment, and the difference between the current moment and the previous moment is the polling query data cycle of the target database; A weight adjustment module, which is in communication with the information acquisition module and is used to modify the interface weight value at the previous moment according to the query connection number and the query connection success rate, to obtain the interface weight value of the target interface instance at the current moment; A connection adjustment module, which is in communication connection with the weight adjustment module and is used to dynamically allocate a database connection of the target interface instance at the current moment in the connection pool according to the interface weight value; Among them, the interface weight value of the previous moment is modified according to the number of query connections and the query connection success rate. The more query connections, the higher the query success rate. The interface weight value is increased on the basis of the interface weight value of the previous moment to obtain the interface weight value of the target interface instance at the current moment. The interface instance with a large interface weight value is allocated more database connections, and the interface instance with a small interface weight value is allocated fewer database connections.
9. A computer device, characterized in that: The computer device comprises: one or more processors; A memory; and one or more application programs, wherein the one or more application programs are stored in the memory and configured to be executed by the processor to implement the database connection dynamic allocation method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: The computer program is loaded by a processor to execute the steps in the method for dynamically allocating database connections according to any one of claims 1 to 7.
Citation Information
Patent Citations
Resource distribution method and device used for database connection pool
CN105468619A
Database connection management method and device
CN110019487A