Database connection management method and device, storage medium and computer equipment
By receiving the connection closing request from the user and deciding whether to release the connection object based on the number of connections, the database connection reliability problem caused by a single user closing the connection when multiple users share the connection in JDBC is solved, and the stability of the database connection is improved.
Patent Information
- Application Number
- CN202411277351.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-12
- Publication Date
- 2024-11-15
AI Technical Summary
During the use of JDBC, when multiple users share the same connection object, if a user closes the connection, other users will be forced to close the connection, resulting in poor database connection reliability.
By receiving the connection closing request from the user, obtaining the request information, and determining the number of people currently connected to the database. If the number of people is greater than N, sending a connection closing notification but not releasing the connection object; if the number of people is not greater than N, releasing the connection object to close all use this Connection between the user end and the database.
Improve the reliability of database connections, avoid database business exceptions caused by multiple users due to a single user closing connection, and ensure that other users can access the database normally.
Smart Images

Figure CN118964468A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of database technology, and in particular to a database connection management method, device, storage medium and computer equipment. Background Art
[0002] In Java development, JDBC (Java Database Connectivity) is a commonly used database connection technology that allows developers to interact with the database through Java code, allowing developers to easily execute SQL statements, obtain query results, and manage database connections.
[0003] However, when multiple users use the same connection object to connect to the database during the use of JDBC, if one of the users calls the interface to close the connection, closing the connection object will force other users to close the connection object as well, resulting in poor reliability of the database connection. Summary of the invention
[0004] The purpose of the present application is to solve at least one of the above-mentioned technical defects, especially the technical defect that when multiple users use the same connection object to connect to the database in the prior art, if one of the users calls the interface to close the connection, then if the connection object is closed at this time, other users will be forced to close the connection object as well, thereby resulting in the technical defect of poor database connection reliability.
[0005] In a first aspect, the present application provides a database connection management method, the method comprising:
[0006] When receiving a connection closing request sent by the user terminal, obtaining request information carried in the connection closing request;
[0007] Determine the number of connections to the database currently connected to the user terminal according to the request information;
[0008] If the number of connections is greater than N, a connection closing notification is sent to the client, but the connection object used by the client to connect to the database is not released; N is a positive integer not less than 1;
[0009] If the number of connections is not greater than N, the connection object is released to close the connection between all clients using the connection object and the database.
[0010] In one embodiment, when the request information is a request address, determining the number of connections to the database to which the user terminal is currently connected according to the request information includes:
[0011] Querying the number of connections to the database corresponding to the request address in a preset secondary cache table; the secondary cache table is used to record the corresponding relationship between the request address of the database and the number of connections;
[0012] The number of connections to the database to which the user terminal is currently connected is determined according to the queried number of connections.
[0013] In one embodiment, when the request information is a user identifier, determining the number of connections to the database to which the user terminal is currently connected according to the request information includes:
[0014] Determine the request address corresponding to the user identifier in a preset three-level cache table; the three-level cache table is used to record the corresponding relationship between the user identifier and the request address;
[0015] Querying the number of connections to the database corresponding to the request address in a preset secondary cache table; the secondary cache table is used to record the corresponding relationship between the request address of the database and the number of connections;
[0016] The number of connections to the database to which the user terminal is currently connected is determined according to the queried number of connections.
[0017] In one embodiment, the updating process of the secondary cache table includes:
[0018] When receiving a database connection request sent by a user terminal, obtaining a request address carried in the database connection request;
[0019] Obtaining a connection object corresponding to the request address through a preset first-level cache table, and using the connection object to establish a connection between the user terminal and a corresponding database; the first-level cache table is used to record the corresponding relationship between the request address and the connection object;
[0020] Adding the number of connections corresponding to the request address in the secondary cache table, generating a user identifier corresponding to the user terminal, and returning the user identifier to the user terminal;
[0021] When sending a connection closing notification to the client but not releasing the connection object used by the client to connect to the database, reducing the number of connections corresponding to the request address in the secondary cache table;
[0022] When the connection object is released, the record corresponding to the request address in the secondary cache table is deleted.
[0023] In one embodiment, the process of generating a user identifier corresponding to the user terminal includes:
[0024] Determine multiple target algorithms based on a preset algorithm library;
[0025] Each target algorithm is used to generate an intermediate user identifier corresponding to each target algorithm, and each intermediate user identifier is concatenated to obtain a final user identifier.
[0026] In one embodiment, the updating process of the first-level cache table includes:
[0027] When receiving a database connection request sent by a user terminal, obtaining a request address carried in the database connection request;
[0028] When there is no connection object corresponding to the request address in the first-level cache table, create a connection object corresponding to the request address, use the connection object to establish a connection between the client and the corresponding database, and write the corresponding relationship between the request address and the connection object into the first-level cache table;
[0029] When the connection object is released, the record corresponding to the request address in the first-level cache table is deleted.
[0030] In one embodiment, the updating process of the three-level cache table includes:
[0031] When a user identifier corresponding to the user terminal is generated, the corresponding relationship between the user identifier and the request address is written into the third-level cache table;
[0032] When the connection object is released, the record corresponding to the user identifier in the third-level cache table is deleted.
[0033] In a second aspect, the present application provides a database connection management device, the device comprising:
[0034] An information acquisition module, configured to acquire request information carried in a connection closing request when receiving a connection closing request sent by a user terminal;
[0035] A number determination module, used to determine the number of connections to the database to which the user terminal is currently connected according to the request information;
[0036] A notification sending module, used for sending a connection closing notification to the user terminal if the number of connections is greater than N, but not releasing the connection object used by the user terminal when connecting to the database; N is a positive integer not less than 1;
[0037] The connection closing module is used to release the connection object if the number of connections is not greater than N, so as to close the connection between all the user terminals using the connection object and the database.
[0038] In a third aspect, the present application provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the database connection management method described in any of the above embodiments.
[0039] In a fourth aspect, the present application provides a computer device, comprising: one or more processors, and a memory;
[0040] The memory stores computer-readable instructions, and when the one or more processors execute the computer-readable instructions, the steps of the database connection management method described in any one of the above embodiments are performed.
[0041] It can be seen from the above technical solutions that the embodiments of the present application have the following advantages:
[0042] In the database connection management method, device, storage medium and computer equipment provided by the present application, when a connection closing request initiated by a user terminal is received, request information is obtained through the request, and then the number of connections to the currently connected database is determined according to the request information, and the number of connections is compared with a preset threshold. When the number of connections is not greater than N, the connection object used by the user terminal to connect to the database is released to close the connection between all user terminals using the connection object and the corresponding database. When the number of connections is greater than N, it means that in addition to the user terminal, one or more user terminals use the same connection object as the user terminal to connect to the database. At this time, a connection closing notification will be sent to the user terminal, but the connection object will not be released, so that other user terminals can still perform database services through the connection object to avoid multiple user terminals using the same request object to access the database. Because a user terminal closes the connection, it causes database business abnormalities for other user terminals using the same request object, thereby improving the reliability of database connections. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0044] Figure 1 A flowchart of a database connection management method provided in an embodiment of the present application;
[0045] Figure 2 One of the flowcharts of determining the number of connections to the database currently connected to the user terminal according to the request information provided in the embodiment of the present application;
[0046] Figure 3 A second flow chart of determining the number of connections to a database currently connected to a user terminal according to request information provided in an embodiment of the present application;
[0047] Figure 4 An example diagram of a user end closing a connection with a database provided in an embodiment of the present application;
[0048] Figure 5 A timing diagram of a user terminal requesting to connect to a database provided in an embodiment of the present application;
[0049] Figure 6 A timing diagram of a user terminal operating a database provided in an embodiment of the present application;
[0050] Figure 7 A timing diagram of a user requesting to close a database provided in an embodiment of the present application;
[0051] Figure 8 A schematic diagram of the structure of a database connection management device provided in an embodiment of the present application;
[0052] Fig. 9 An internal structure diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0053] 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 ordinary technicians in this field without creative work are within the scope of protection of this application.
[0054] In one embodiment, the present application provides a database connection management method, and the following embodiment is described by applying the method to a database connection management system. It can be understood that the database connection management system is deployed in a server, and the database connection management method can be executed by a single server or a server cluster composed of multiple servers. The present application does not make specific restrictions on this, and it is referred to as a server in the following embodiments.
[0055] like Figure 1 As shown, the present application provides a database connection management method, the method comprising:
[0056] S101: When receiving a connection closing request sent by a user terminal, obtaining request information carried in the connection closing request.
[0057] The request information refers to the information carried by the user end when initiating a request and used to indicate the purpose of the request. The request information includes but is not limited to the user identifier, the request address, etc.
[0058] Specifically, the user identifier in the request information of the present application is used to identify the user end, and can be represented as a string of random numbers. The request address refers to a string used to specify the database connection information. The request address may include a protocol, a sub-protocol, a host name, a port number, a database name, a user name, and a password, etc. In an example, it is assumed that the request address is as follows: jdbc:mysql: / / hostname:port / databasename?user=username&password=password, wherein jdbc is a protocol used to indicate a protocol for connecting to a database, mysql is a sub-protocol used to indicate a specific database type or driver, hostname is a host name used to identify the IP address of a database server, port is a port number used to indicate the port number on which the database server listens, databasename is a database name used to indicate the name of the database to be connected, operated, or closed, and username and password represent the user name and password, respectively.
[0059] In this step, when the user end does not need to connect to the database, the user end can generate request information according to its own needs, and initiate a connection closing request to the server end in the form of carrying the request information by calling the connection closing interface. At this time, when the server end receives the connection closing request, it parses the received connection closing request to obtain the request information carried in the connection closing request.
[0060] S102: Determine the number of connections to the database to which the user terminal is currently connected according to the request information.
[0061] The number of connections refers to the number of users connected to the database that the user wants to close.
[0062] In this step, when the request information carried in the connection closing request is obtained, the number of connections to the database to which the user terminal is currently connected can be determined based on the information contained in the request information. Specifically, the database in this embodiment refers to the database to which the user terminal requests to close the connection.
[0063] For example, assume that there are three user terminals connected to database A at the same time, and user terminal 1 completes its database business before user terminals 2 and 3. At this time, user terminal 1 makes a request to close the connection, and the server obtains the request information of user terminal 1 through the request to close the connection. Then, based on the request information, it can be determined that the number of connections to database A is 3.
[0064] S103: Determine whether the number of connected persons is greater than N.
[0065] Wherein, N is a positive integer not less than 1. The setting of N can be considered from the aspects of the impact of closing the connection on other users connected at the same time, the impact of long-term occupation of connection resources on other users who want to connect to the database, etc., so as to set a suitable N value, and this application does not make specific restrictions on this.
[0066] Specifically, when N is set to 1, the connection object used by the user client to connect to the database will be released only when the number of connections is equal to 1. At this time, only the user client that initiates the connection closing request is connected to the database. Therefore, releasing the connection object will not affect the database business of any user client. When N is set to be equal to or greater than 2, taking N being set to 2 as an example, as long as the number of connections is equal to 2, the connection object used by the user client to connect to the database will be released.
[0067] S104: If the number of connected persons is greater than N, a connection closing notification is sent to the user terminal, but the connection object used by the user terminal to connect to the database is not released.
[0068] The connection object is an object used to establish a connection with a database. This object encapsulates all the information and methods required to interact with the database, allowing developers to easily perform database operations such as querying, updating, inserting, and deleting data.
[0069] Specifically, after multiple users establish connections with the database, if a user has completed its database business, the user will initiate a connection closing request. After receiving the connection closing request, the server will release the connection object, thereby forcing the connection objects of other users to be released as well, causing database business abnormalities. Based on this, the present application can determine the current number of connections to the database that the server requires to close the connection, so as to determine whether the connection object needs to be released.
[0070] In this step, when the number of connections is greater than N, it means that in addition to the user terminal, there are one or more user terminals connected to the database. At this time, in order to ensure the user terminal experience, the server will send a connection closing notification to feedback the connection closing request initiated by the user, but it does not actually release the connection object used by the user terminal to connect to the database. Therefore, the normal business operations of other user terminals connected to the database are not affected at this time.
[0071] S105: If the number of connections is not greater than N, the connection object is released to close the connection between all the clients using the connection object and the database.
[0072] In this step, when the number of connections is not greater than N, it means that closing the connection will not affect other user terminals connected to the database or closing the connection will not cause a large impact. At this time, the connection object can be released and cleared from the cache to release resources.
[0073] In the database connection management method, device, storage medium and computer equipment provided by the present application, when a connection closing request initiated by a user terminal is received, request information is obtained through the request, and then the number of connections to the currently connected database is determined according to the request information, and the number of connections is compared with a preset threshold. When the number of connections is not greater than N, the connection object used by the user terminal to connect to the database is released to close the connection between all user terminals using the connection object and the corresponding database. When the number of connections is greater than N, it means that in addition to the user terminal, one or more user terminals use the same connection object as the user terminal to connect to the database. At this time, a connection closing notification will be sent to the user terminal, but the connection object will not be released, so that other user terminals can still perform database services through the connection object to avoid multiple user terminals using the same request object to access the database. Because a user terminal closes the connection, it causes database business abnormalities for other user terminals using the same request object, thereby improving the reliability of database connections.
[0074] like Figure 2 As shown, in one embodiment, when the request information is a request address, determining the number of connections to the database currently connected to the user terminal according to the request information includes:
[0075] S201: Query the number of connections to the database corresponding to the request address in a preset secondary cache table.
[0076] The secondary cache table is used to record the correspondence between the request address of the database and the number of connections. For example, if the request address is represented as url and the number of connections is represented as count, then a record in the secondary cache table can be represented as<url, count> Among them, url can be called the key value of this record, and count can be called the value of this record.
[0077] In this step, the preset secondary cache table is first obtained. The secondary cache table records the correspondence between the request address of the database and the number of connections. Therefore, when the request information is the request address, the request address can be determined, and then the number of connections corresponding to the request address can be queried in the obtained secondary cache table.
[0078] Furthermore, since the secondary cache table maintains the number of connections to the database, if the secondary cache table is not thread-safe, dirty reads, update loss, data inconsistency and other problems may occur when multiple users read and write at the same time. Therefore, the secondary cache table in this application can be set as a thread-safe data structure. For example, the secondary cache table can be a thread-safe dictionary, a thread-safe map, a thread-safe hash table, etc. It only needs to meet thread safety, be stored in the form of key-value pairs, the key is unique in the data structure, and can specify records for operation when modifying and deleting.
[0079] S202: Determine the number of connections to the database to which the user terminal is currently connected according to the queried number of connections.
[0080] In this step, when the number of connections corresponding to the request address is found in the secondary cache table, the number of connections is determined as the number of connections to the database currently connected to the user end.
[0081] Specifically, when a user terminal initiates a database connection request, if the user terminal successfully connects to the database, the user terminal can obtain its corresponding user identifier. When the user terminal needs to add, delete, query or modify the database or the user terminal needs to close the connection, it can initiate a corresponding request with the user identifier or with the request address.
[0082] It is understandable that, when the request information is a request address, the number of connections corresponding to the request address can be determined by directly querying the preset secondary cache table, so that the time complexity of data query is low, thereby improving data query efficiency.
[0083] like Figure 3 As shown, in one embodiment, when the request information is a user identifier, determining the number of connections to the database currently connected to the user terminal according to the request information includes:
[0084] S301: Determine a request address corresponding to a user identifier in a preset three-level cache table.
[0085] The third-level cache table is used to record the correspondence between the user identifier and the request address. For example, if the user identifier is represented as skey and the request address is represented as url, then a record in the third-level cache table can be represented as<skey, url> Among them, skey can be called the key value of this record, and url can be called the value of this record.
[0086] In this step, firstly, a preset three-level cache table is obtained, which records the correspondence between the user identifier and the request address. Therefore, when the request information is the user identifier, the request address corresponding to the user identifier can be queried in the obtained three-level cache table.
[0087] Furthermore, since the key value in the third-level cache table is the user ID, and the user ID is unique to the user end, the key value in the third-level cache table will not conflict, and the third-level cache table is not required to be thread-safe. That is, the third-level cache table only needs to be stored in the form of key-value pairs, the key is unique in the data structure, and the record can be specified for operation when modified and deleted.
[0088] S302: Query the preset secondary cache table for the number of connections to the database corresponding to the request address.
[0089] Among them, the secondary cache table is used to record the correspondence between the database request address and the number of connections.
[0090] S303: Determine the number of connections to the database to which the user terminal is currently connected according to the queried number of connections.
[0091] The description of S302 to S303 may refer to the description of S201 to S202 .
[0092] It is understandable that when a user terminal initiates a database connection request, if the user terminal successfully connects to the database, the user terminal can obtain its corresponding user ID. When the user terminal needs to add, delete, check, or modify the database or needs to close the connection, the corresponding request can be initiated with the user ID or the request address. When a database operation request or a connection closing request is initiated with the user ID, frequent initiation of requests with the request address can be avoided, reducing the risk of user privacy leakage due to the request address being cracked. This improves the security of database connection management.
[0093] In one embodiment, the updating process of the secondary cache table includes:
[0094] S1: When receiving a database connection request sent by a user, obtain the request address carried in the database connection request.
[0095] S2: Obtain a connection object corresponding to the request address through a preset first-level cache table, and use the connection object to establish a connection between the user terminal and the corresponding database.
[0096] S3: Add the number of connections corresponding to the request address in the secondary cache table, generate a user identifier corresponding to the user end, and return the user identifier to the user end.
[0097] S4: When a connection closing notification is sent to the client, but the connection object used by the client to connect to the database is not released, the number of connections corresponding to the request address is reduced in the secondary cache table.
[0098] S5: When the connection object is released, the record corresponding to the request address in the secondary cache table is deleted.
[0099] The database in this embodiment refers to the database corresponding to the request address carried by the database connection request initiated by the user end, that is, the database that the user end wants to connect to. The first-level cache table is used to record the correspondence between the request address and the connection object. For example, if the request address is represented as url and the connection object is represented as conn, then a record in the first-level cache table can be represented as<url, conn> Among them, url can be called the key value of this record, and conn can be called the value of this record.
[0100] In this embodiment, there are several situations involving the update of the secondary cache table. First, when the user wants to establish a connection with the database and the server receives a database connection request, and second, when the server sends a connection closing notification to the user or releases the connection object. The update of the secondary cache table is further described below:
[0101] (1) When receiving a database connection request sent by a user, the user wants to establish a connection with the database. After receiving the request, the server parses the request to obtain the request address carried therein. Since the first-level cache table records the correspondence between the request address and the connection object, the connection object corresponding to the request address obtained by parsing can be obtained through the first-level cache table to establish a connection between the user and the database. After the connection between the user and the database is established, the number of users connected to the database increases. At this time, it is necessary to increase the number of connections corresponding to the request address in the second-level cache table. In addition, after the user establishes a connection with the database, the server will generate a user ID for the user. When the user initiates a database operation request or a connection closing request, it can initiate the request with the user ID.
[0102] (2) When the server sends a connection closing notification to the client but does not release the connection object used by the client to connect to the database, it means that the client has initiated a connection closing request, but because the number of connections is greater than N, the connection object is not actually released, but the number of connections corresponding to the request address needs to be reduced in the secondary cache table. When the server releases the connection object, it means that the client has initiated a connection closing request and the number of connections is not greater than N. At this time, the connection object will be actually released, and the record corresponding to the request address in the secondary cache table will be deleted to free up space in the secondary cache table.
[0103] Furthermore, since the first-level cache table maintains connection objects, if the first-level cache table is not thread-safe, the data read by different user terminals may be inconsistent. Therefore, in order to maintain data consistency, the first-level cache table in this application can be set as a thread-safe data structure. That is, the request address in the first-level cache table can uniquely identify a request object. For example, the first-level cache table can be a thread-safe dictionary, a thread-safe map (Map), a thread-safe hash table (Hash Table), etc. It only needs to meet thread safety, storage in the form of key-value pairs, the key is unique in the data structure, and the record can be specified for operation when modifying and deleting.
[0104] In one example, if Figure 4 As shown, Figure 4 This is an example diagram of a user end closing a connection with a database provided in an embodiment of the present application. Figure 4 In the example, application layer refers to the application layer, which can be understood as the server in the above embodiment. The Hash Table inside the application layer is the first-level cache table, and the HashTable connected by the dotted line is the second-level cache table. In addition, the third-level cache table is in Figure 4 Not shown.
[0105] In the figure, user A initiates a connection closing request, namely closeConnect (connA), where connA represents the connection object corresponding to the database that user A wants to close. When the secondary cache table is not set, the server receives the request from user A and releases connA, corresponding to steps 2 to 4 in the figure. First, a closing message is sent to the database, namely close (connA), and the database returns a message indicating a successful closing, namely closeOk. Finally, the record in the primary cache table is removed, namely removeConnA, which will cause a connection exception (ConnectExcetion) to user B who is using connA to connect to the database. Therefore, a secondary cache table is set to record the correspondence between the database request address and the number of connections, and to determine whether to release the connection object based on the number of connections, thereby ensuring the reliability of the database connection.
[0106] It can be understood that in this embodiment, different update operations are performed on the secondary cache table in various situations to ensure that the number of connections in the secondary cache table is updated accurately and timely, thereby ensuring the accuracy of decisions made based on the number of connections, so as to achieve the purpose of improving the reliability of database connections.
[0107] In one embodiment, the process of generating a user identifier corresponding to the user terminal includes:
[0108] S1: Determine multiple target algorithms based on a preset algorithm library.
[0109] S2: using each target algorithm to generate an intermediate user identifier corresponding to each target algorithm, and concatenating each intermediate user identifier to obtain a final user identifier.
[0110] The algorithm library includes a variety of identification generation algorithms, such as UUID, distributed ID generator, hash function, and algorithms based on timestamps or random numbers.
[0111] In this embodiment, the process of determining multiple target algorithms in the algorithm library may include: randomly selecting a target algorithm that matches the preset number of algorithms in the algorithm library, or, according to the priority of each algorithm in the algorithm library, selecting a target algorithm that matches the preset algorithm data according to the priority, wherein the priority is used to measure the adaptability or advantage of the algorithm for the user identification generation task in this application. After determining multiple target algorithms, use each target algorithm to generate multiple intermediate user identifications, and then splice the intermediate user identifications generated by each target algorithm to obtain the final user identification.
[0112] It is understandable that by determining multiple target algorithms and concatenating intermediate user identifiers generated by multiple target algorithms, the complexity and randomness of the user identifier can be enhanced, thereby improving the security of the user identifier during transmission and improving the security of database connection management.
[0113] In another embodiment, in order to simplify the user identification generation process and save resources consumed in the generation process to improve the efficiency of user identification generation, a target algorithm can be directly determined in the algorithm library, and the user identification generated by the target algorithm is used as the final user identification.
[0114] In one embodiment, the updating process of the first-level cache table includes:
[0115] S1: When receiving a database connection request sent by a user, obtain the request address carried in the database connection request.
[0116] S2: When there is no connection object corresponding to the request address in the first-level cache table, create a connection object corresponding to the request address, use the connection object to establish a connection between the user terminal and the corresponding database, and write the correspondence between the request address and the connection object into the first-level cache table.
[0117] S3: When the connection object is released, the record corresponding to the request address in the first-level cache table is deleted.
[0118] In this embodiment, there are several situations involving the update of the first-level cache table. One is when the user wants to establish a connection with the database and the server receives a database connection request. The other is when the server releases the connection object. The update of the first-level cache table is further described below:
[0119] (1) When receiving a database connection request sent by a user, the user wants to establish a connection with the database. After receiving the request, the server parses the request to obtain the request address carried therein. Since the first-level cache table records the correspondence between the request address and the connection object, the request object corresponding to the request address can be queried in the first-level cache table based on the request address. If there is a record corresponding to the request address in the first-level cache table, the corresponding request object can be directly obtained to establish a connection with the database corresponding to the request address; if there is no record corresponding to the request address in the first-level cache table, the corresponding database is determined based on the request address, and a connection object of the database is created to establish a connection with the database based on the connection object, and the correspondence between the request address and the connection object is also written into the first-level cache table. In this way, when a user requests to connect to the database before the connection object is released, the connection object can be directly obtained from the first-level cache table. On the one hand, it can avoid frequent creation of connection objects, and on the other hand, it can quickly obtain the connection objects cached in the first-level cache table, thereby saving resource consumption and improving resource reuse rate.
[0120] (2) When the server releases the connection object, it deletes the record corresponding to the request address in the first-level cache table to free up space in the first-level cache table.
[0121] In one embodiment, the updating process of the third-level cache table includes:
[0122] S1: When a user identifier corresponding to the user terminal is generated, the corresponding relationship between the user identifier and the request address is written into the three-level cache table.
[0123] S2: When the connection object is released, the record corresponding to the user ID in the third-level cache table is deleted.
[0124] In this embodiment, there are several situations involving the update of the three-level cache table. One is when the user wants to establish a connection with the database and the server receives a database connection request. The other is when the server releases the connection object. The update of the three-level cache table is further explained below:
[0125] (1) When the server receives a database connection request and establishes a connection between the client and the database, during this process, the server generates a corresponding user ID for the client, and the third-level cache table records the correspondence between the user ID and the request address. Therefore, the correspondence between the generated user ID and the request address needs to be written into the third-level cache table so that the client can initiate a request with the user ID in subsequent database operation requests or connection closing requests, so as to avoid frequently initiating requests with the request address and reduce the risk of user privacy leakage due to the cracking of the request address. This improves the security of database connection management.
[0126] (2) When the server releases the connection object, it deletes the record corresponding to the user ID in the third-level cache table to free up space in the third-level cache table.
[0127] In one example, if Figures 5 to 7 As shown, Figure 5 This is a timing diagram of a user terminal requesting to connect to a database provided in an embodiment of the present application. Figure 6 This is a timing diagram of a user terminal operating a database provided in an embodiment of the present application. Figure 7 A timing diagram of when a user terminal requests to close a database provided in an embodiment of the present application.
[0128] exist Figures 5 to 7 In the example, N is set to 1, where url represents the request address, count and connection count can both represent the number of connections, skey represents the user ID, the first-level cache in the figure is the first-level cache table, the second-level cache is the second-level cache table, the third-level cache is the third-level cache table, and the alt box represents if, that is, the steps to be performed if a situation occurs. For example, in Figure 7 In the example, when the connection count is determined by step 4, if the connection count is greater than 1, a notification can be sent to inform that the connection has been closed, but the connection object is not actually released. Steps 7 to 8 are the steps executed when the connection count is equal to 1. At this time, the connection state of the connection object will be closed, that is, the connection object will be released.
[0129] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0130] The following is a description of a database connection management device provided in an embodiment of the present application. The database connection management device described below and the database connection management method described above can be referenced to each other.
[0131] like Figure 8 As shown, the present application provides a database connection management device 400, the device comprising:
[0132] The information acquisition module 401 is used to acquire the request information carried in the connection closing request when receiving the connection closing request sent by the user end;
[0133] The number determination module 402 is used to determine the number of connections to the database currently connected to the user terminal according to the request information;
[0134] Notification sending module 403, used for sending a connection closing notification to the user terminal if the number of connected persons is greater than N, but not releasing the connection object used by the user terminal to connect to the database; N is a positive integer not less than 1;
[0135] The connection closing module 404 is used to release the connection object if the number of connections is not greater than N, so as to close the connection between all the clients using the connection object and the database.
[0136] In one embodiment, the number of people determining module includes:
[0137] The first query submodule, 1 queries the number of connections to the database corresponding to the request address in a preset secondary cache table; the secondary cache table is used to record the corresponding relationship between the request address of the database and the number of connections;
[0138] The first determination submodule is used to determine the number of connections to the database to which the user terminal is currently connected according to the queried number of connections.
[0139] In one embodiment, the number of people determining module includes:
[0140] The address determination submodule is used to determine the request address corresponding to the user identifier in a preset three-level cache table; the three-level cache table is used to record the corresponding relationship between the user identifier and the request address;
[0141] The second query submodule is used to query the number of connections to the database corresponding to the request address in a preset secondary cache table; the secondary cache table is used to record the corresponding relationship between the request address of the database and the number of connections;
[0142] The second determining submodule is used to determine the number of connections to the database to which the user terminal is currently connected according to the queried number of connections.
[0143] In one embodiment, the database connection management device includes:
[0144] An address acquisition module is used to acquire the request address carried in the database connection request when receiving the database connection request sent by the user end;
[0145] A connection establishment module is used to obtain a connection object corresponding to the request address through a preset first-level cache table, and use the connection object to establish a connection between the user terminal and the corresponding database; the first-level cache table is used to record the corresponding relationship between the request address and the connection object;
[0146] A first updating module is used to add the number of connections corresponding to the request address in the secondary cache table, and to generate a user identifier corresponding to the user terminal and return the user identifier to the user terminal;
[0147] The second updating module is used to reduce the number of connections corresponding to the request address in the secondary cache table when sending a connection closing notification to the client but not releasing the connection object used by the client to connect to the database;
[0148] The third updating module is used to delete the record corresponding to the request address in the secondary cache table when the connection object is released.
[0149] In one embodiment, the first update module includes:
[0150] An algorithm determination submodule, used to determine multiple target algorithms based on a preset algorithm library;
[0151] The identification determination submodule is used to respectively use each target algorithm to generate an intermediate user identification corresponding to each target algorithm, and to concatenate each intermediate user identification to obtain a final user identification.
[0152] In one embodiment, the database connection management device includes:
[0153] An address acquisition module is used to acquire the request address carried in the database connection request when receiving the database connection request sent by the user end;
[0154] An object creation module is used to create a connection object corresponding to the request address when there is no connection object corresponding to the request address in the first-level cache table, so as to use the connection object to establish a connection between the user terminal and the corresponding database, and write the corresponding relationship between the request address and the connection object into the first-level cache table;
[0155] The fourth updating module is used to delete the record corresponding to the request address in the first-level cache table when the connection object is released.
[0156] In one embodiment, the database connection management device includes:
[0157] A fifth updating module, configured to write the corresponding relationship between the user identifier and the request address into the third-level cache table when generating the user identifier corresponding to the user terminal;
[0158] The sixth updating module is used to delete the record corresponding to the user identifier in the third-level cache table when the connection object is released.
[0159] The division of each module in the above-mentioned database connection management device is only for illustration. In other embodiments, the database connection management device can be divided into different modules as needed to complete all or part of the functions of the above-mentioned database connection management device. Each module in the above-mentioned database connection management device can be implemented in whole or in part by software, hardware and a combination thereof. Each of the above-mentioned modules can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute the operations corresponding to each of the above modules.
[0160] In one embodiment, the present application further provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the database connection management method described in any one of the above embodiments.
[0161] In one embodiment, the present application further provides a computer device having computer-readable instructions stored therein. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the database connection management method as described in any one of the above embodiments.
[0162] Indicatively, if Fig. 9 As shown, Fig. 9This is a schematic diagram of the internal structure of a computer device provided in an embodiment of the present application. The computer device 500 may be provided as a server. Fig. 9 , the computer device 500 includes a processing component 502, which further includes one or more processors, and a memory resource represented by a memory 501, for storing instructions executable by the processing component 502, such as an application. The application stored in the memory 501 may include one or more modules, each corresponding to a set of instructions. In addition, the processing component 502 is configured to execute instructions to perform the database connection management method of any of the above embodiments.
[0163] The computer device 500 may further include a power supply component 503 configured to perform power management of the computer device 500, a wired or wireless network interface 504 configured to connect the computer device 500 to a network, and an input / output (I / O) interface 505. The computer device 500 may operate based on an operating system stored in the memory 501, such as Windows Server TM, Mac OS X TM, Unix TM, Linux TM, Free BSD TM, or the like.
[0164] Those skilled in the art will understand that Fig. 9 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0165] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish an entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the term "include", "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements not only include those elements, but also include other elements that are not clearly listed, or also include elements inherent to such process, method, article or equipment. In the absence of more restrictions, the elements limited by the sentence "comprise one..." do not exclude the existence of other identical elements in the process, method, article or equipment including the elements. Herein, the singular "one", "one" and "described / the" may also include plural forms, unless the context clearly indicates another way. It should also be understood that the terms "include / comprise" or "have" etc. specify the existence of stated features, wholes, steps, operations, components, parts or combinations thereof, but do not exclude the possibility of the existence or addition of one or more other features, wholes, steps, operations, components, parts or combinations thereof. At the same time, the term "and / or" used in this specification includes any and all combinations of the relevant listed items.
[0166] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can refer to each other.
[0167] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A database connection management method, characterized in that: The method comprises: When receiving a connection closing request sent by the user terminal, obtaining request information carried in the connection closing request; Determine the number of connections to the database currently connected to the user terminal according to the request information; If the number of connections is greater than N, a connection closing notification is sent to the client, but the connection object used by the client to connect to the database is not released; N is a positive integer not less than 1; If the number of connections is not greater than N, the connection object is released to close the connection between all clients using the connection object and the database.
2. The database connection management method according to claim 1, characterized in that: When the request information is a request address, determining the number of connections to the database to which the user terminal is currently connected according to the request information includes: Querying the number of connections to the database corresponding to the request address in a preset secondary cache table; the secondary cache table is used to record the corresponding relationship between the request address of the database and the number of connections; The number of connections to the database to which the user terminal is currently connected is determined according to the queried number of connections.
3. The database connection management method according to claim 1, characterized in that: When the request information is a user identifier, determining the number of connections to the database to which the user terminal is currently connected according to the request information includes: Determine the request address corresponding to the user identifier in a preset three-level cache table; the three-level cache table is used to record the corresponding relationship between the user identifier and the request address; Querying the number of connections to the database corresponding to the request address in a preset secondary cache table; the secondary cache table is used to record the corresponding relationship between the request address of the database and the number of connections; The number of connections to the database to which the user terminal is currently connected is determined according to the queried number of connections.
4. The database connection management method according to claim 3, characterized in that: The updating process of the secondary cache table includes: When receiving a database connection request sent by a user terminal, obtaining a request address carried in the database connection request; Obtaining a connection object corresponding to the request address through a preset first-level cache table, and using the connection object to establish a connection between the user terminal and a corresponding database; the first-level cache table is used to record the corresponding relationship between the request address and the connection object; Adding the number of connections corresponding to the request address in the secondary cache table, generating a user identifier corresponding to the user terminal, and returning the user identifier to the user terminal; When sending a connection closing notification to the client but not releasing the connection object used by the client to connect to the database, reducing the number of connections corresponding to the request address in the secondary cache table; When the connection object is released, the record corresponding to the request address in the secondary cache table is deleted.
5. The database connection management method according to claim 4, characterized in that: The process of generating a user identifier corresponding to the user terminal includes: Determine multiple target algorithms based on a preset algorithm library; Each target algorithm is used to generate an intermediate user identifier corresponding to each target algorithm, and each intermediate user identifier is concatenated to obtain a final user identifier.
6. The database connection management method according to claim 4, characterized in that: The updating process of the first-level cache table includes: When receiving a database connection request sent by a user terminal, obtaining a request address carried in the database connection request; When there is no connection object corresponding to the request address in the first-level cache table, create a connection object corresponding to the request address, use the connection object to establish a connection between the client and the corresponding database, and write the corresponding relationship between the request address and the connection object into the first-level cache table; When the connection object is released, the record corresponding to the request address in the first-level cache table is deleted.
7. The database connection management method according to claim 4, characterized in that: The updating process of the three-level cache table includes: When a user identifier corresponding to the user terminal is generated, the corresponding relationship between the user identifier and the request address is written into the third-level cache table; When the connection object is released, the record corresponding to the user identifier in the third-level cache table is deleted.
8. A database connection management device, characterized in that: The device comprises: An information acquisition module, configured to acquire request information carried in a connection closing request when receiving a connection closing request sent by a user terminal; A number determination module, used to determine the number of connections to the database to which the user terminal is currently connected according to the request information; A notification sending module, used for sending a connection closing notification to the user terminal if the number of connections is greater than N, but not releasing the connection object used by the user terminal when connecting to the database; N is a positive integer not less than 1; The connection closing module is used to release the connection object if the number of connections is not greater than N, so as to close the connection between all the user terminals using the connection object and the database.
9. A storage medium, characterized in that: The storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the database connection management method according to any one of claims 1 to 7.
10. A computer device, characterized in that: include: one or more processors, and memory; The memory stores computer-readable instructions, and when the computer-readable instructions are executed by the one or more processors, the steps of the database connection management method according to any one of claims 1 to 7 are performed.
Citation Information
Patent Citations
Method, device and system for managing database connection pool
CN107291747A
High-efficient administration method for database connection
CN1510575A
System and procedure for concurrent database access by multiple user applications through shared connection processes
US5596745A
