Database connection method and device, computer equipment and readable storage medium
By detecting and managing database connection status in the connection pool, the problem of inefficiency in database connections in traditional methods is solved, and more efficient connection management and rapid recovery capabilities are achieved.
Patent Information
- Application Number
- CN202411520644.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-29
- Publication Date
- 2025-02-28
AI Technical Summary
Traditional database connection methods perform connection availability checks before or after each use, resulting in inefficient database connections.
By selecting the target connection object in the connection pool, it obtains its most recent detection time. When the detection time exceeds the preset interval, it detects the connection status and resets the preset interval duration of the connection pool in an abnormal situation to re-establish a database connection.
Reduces unnecessary connection status checks, improves database connection efficiency, and can automatically and quickly restore normal use after the database service is restarted.
Smart Images

Figure CN119537429A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a database connection method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art
[0002] With the development of computer technology, connection pool technology has emerged. Connection pool is a technology often used in the field of WEB applications. Compared with the original solution of reconnecting every time, initializing a certain number of available connections and putting them into the connection pool can achieve a significant performance improvement.
[0003] However, the traditional method is to check whether the connection is available before or after each use, which is equivalent to doubling the query operation, resulting in low database connection efficiency. Summary of the invention
[0004] The embodiments of the present application provide a database connection method, apparatus, computer equipment, computer-readable storage medium, and computer program product, which can improve the efficiency of database connection.
[0005] A database connection method, comprising:
[0006] When receiving a connection establishment request for a database, determining a connection pool corresponding to the database;
[0007] Selecting a target connection object from the connection pool, and obtaining the most recent detection time of the target connection object;
[0008] When the interval between the detection time and the current time exceeds the preset interval, detecting the connection status of the target connection object;
[0009] When the connection state indicates that the target connection object is in an abnormal state, the preset interval duration of the connection pool is reset, and the connection with the database is reestablished based on the target connection object.
[0010] On the other hand, the present application also provides a database connection device, including:
[0011] A determination module, configured to determine a connection pool corresponding to a database upon receiving a connection establishment request for the database;
[0012] An acquisition module, used to select a target connection object from the connection pool and obtain the most recent detection time of the target connection object;
[0013] A detection module, configured to detect a connection state of the target connection object when an interval between the detection time and the current time exceeds a preset interval;
[0014] A connection module is configured to reset the preset interval of the connection pool when the connection state indicates that the target connection object is in an abnormal state, and to reestablish the connection with the database based on the target connection object.
[0015] On the other hand, the present application also provides a computer device. The computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:
[0016] When receiving a connection establishment request for a database, determining a connection pool corresponding to the database;
[0017] Selecting a target connection object from the connection pool, and obtaining the most recent detection time of the target connection object;
[0018] When the interval between the detection time and the current time exceeds the preset interval, detecting the connection status of the target connection object;
[0019] When the connection state indicates that the target connection object is in an abnormal state, the preset interval duration of the connection pool is reset, and the connection with the database is reestablished based on the target connection object.
[0020] On the other hand, the present application also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0021] When receiving a connection establishment request for a database, determining a connection pool corresponding to the database;
[0022] Selecting a target connection object from the connection pool, and obtaining the most recent detection time of the target connection object;
[0023] When the interval between the detection time and the current time exceeds the preset interval, detecting the connection status of the target connection object;
[0024] When the connection state indicates that the target connection object is in an abnormal state, the preset interval duration of the connection pool is reset, and the connection with the database is reestablished based on the target connection object.
[0025] On the other hand, the present application also provides a computer program product. The computer program product includes a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0026] When receiving a connection establishment request for a database, determining a connection pool corresponding to the database;
[0027] Selecting a target connection object from the connection pool, and obtaining the most recent detection time of the target connection object;
[0028] When the interval between the detection time and the current time exceeds the preset interval, detecting the connection status of the target connection object;
[0029] When the connection state indicates that the target connection object is in an abnormal state, the preset interval duration of the connection pool is reset, and the connection with the database is reestablished based on the target connection object.
[0030] In the above database connection method, when a connection establishment request for a database is received, a connection pool corresponding to the database is determined, and a target connection object is selected from the connection pool. The connection object that can connect to the database can be saved in the connection pool in advance, avoiding the need to create each time the database is connected, thereby improving the connection efficiency of the database. The most recent detection time of the target connection object is obtained, and when the interval between the detection time and the current time exceeds the preset interval, the connection state of the target connection object is detected to determine whether the current target connection object is available through the connection state. When the connection state indicates that the target connection object is in an abnormal state, it means that the target connection object is unavailable, and it means that the state abnormality is detected only at the preset interval, resulting in the target connection object being in an abnormal state for too long, and resetting the preset interval of the connection pool can shorten the abnormal connection time, so that the next connection can be executed normally. In addition, when the target connection object is in an abnormal state, the connection between the target connection object and the database is rebuilt based on the target connection object, so that the database service can automatically and quickly resume normal use after being restarted without selecting a new connection, thereby improving the connection efficiency of the database. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] 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 work.
[0032] FIG1 is an application environment diagram of a database connection method in one embodiment;
[0033] FIG2 is a flow chart of a database connection method in one embodiment;
[0034] Figure 3 A flowchart of a database connection method in one of the embodiments;
[0035] Figure 4 A flowchart of a database connection method in one embodiment;
[0036] Figure 5 is a structural block diagram of a database connection device in one embodiment;
[0037] Figure 6 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0038] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0039] The database connection method provided in the embodiment of the present application can be applied to the application environment shown in Figure 1. Among them, the first server 102 communicates with the second server 104 through the network. The data storage system can store the data that the second server 104 needs to process. The data storage system can be integrated on the second server 104, or it can be placed on the cloud or other network servers. When a connection establishment request for a database is received, a connection pool corresponding to the database is determined, a target connection object is selected from the connection pool, and the most recent detection time of the target connection object is obtained. When the interval between the detection time and the current time exceeds the preset interval, the connection state of the target connection object is detected. When the connection state indicates that the target connection object is in an abnormal state, the preset interval of the connection pool is reset, and the connection with the database is rebuilt based on the target connection object. Among them, the first server 102 and the second server 104 can be implemented with an independent server or a server cluster consisting of multiple servers.
[0040] In one embodiment, as shown in FIG. 2 , a database connection method is provided, which is described by taking the method applied to a computer device (the computer device is the first server in FIG. 1 ) as an example, and includes the following steps:
[0041] Step 202: When a connection establishment request for a database is received, a connection pool corresponding to the database is determined.
[0042] The connection establishment request is a request for establishing a connection with a database. A connection pool is a container for storing database connections. A connection pool is a technology for creating and managing a pool of connections that can be used by any thread that needs them. A connection object represents an object of a database connection, and a connection object may include at least one of a connection address, a port, or a usage status.
[0043] Specifically, when receiving a connection establishment request for a database, the computer device determines a connection pool corresponding to the database. The connection pool stores a plurality of candidate connection objects, each of which is used to connect to the database.
[0044] In this embodiment, the computer device may be a code server or a service terminal.
[0045] In this embodiment, the method further includes: creating a connection pool corresponding to the database. Creating a connection pool corresponding to the database includes: obtaining log information of the database processing connection requests within a historical period, determining the number of connection requests based on the log information; creating candidate connection objects equal to the number of connection requests, saving the created candidate connection objects to the connection pool, and binding the connection pool to the database.
[0046] Furthermore, a preset interval duration is set for the connection pool, and the preset interval duration is used to automatically detect each candidate connection object in the connection pool to detect whether the connection state of each candidate connection object is normal. When the connection state of a candidate connection object is abnormal, the abnormal candidate connection object is updated to update the abnormal candidate connection object to a normal candidate connection object, so that the connection pool always maintains normal candidate connection objects.
[0047] Furthermore, when the connection state of a candidate connection object is abnormal, the preset interval duration of the connection pool is reset.
[0048] In this embodiment, a preset interval duration may be set for each candidate connection object in the connection pool. For each candidate connection object in the connection pool, the connection state of the candidate connection object is detected according to the preset interval duration of the candidate connection object, and when the connection state indicates that the candidate connection object is in an abnormal state, the candidate connection object is updated. Furthermore, when the connection state indicates that the candidate connection object is in an abnormal state, the preset interval duration of the candidate connection object may be reset.
[0049] In this embodiment, a time reset interface is configured for the connection pool. When a candidate connection object is in an abnormal state, the time reset interface can be called to reset the preset interval duration for the connection pool or the candidate connection object.
[0050] Step 204: Select a target connection object from the connection pool and obtain the most recent detection time of the target connection object.
[0051] The detection time refers to the time when the target connection object is detected to be abnormal. The latest detection time refers to the latest time when the target connection object is detected to be abnormal.
[0052] Specifically, the connection pool stores multiple candidate connection objects. The computer device can select a target connection object from the multiple candidate connection objects in the connection pool and obtain the most recent detection time of the target connection object.
[0053] In this embodiment, there is no limitation on the method of selecting the target connection object from the connection pool, which may be array polling, taking from a queue, etc.
[0054] In some embodiments, the computer device may select a candidate connection object that meets a preset condition from a plurality of candidate connection objects in a connection pool as a target connection object.
[0055] Among them, the preset condition may be a condition related to the idle time. The computer device may determine the idle time of each candidate connection object, and select the candidate connection object with the longest idle time from multiple candidate connection objects as the target connection object.
[0056] In this embodiment, when there is no candidate connection object that meets the preset conditions in the connection pool, a new connection object can be created for the database, and the new connection object is used as the target connection object to establish a connection with the database through the target connection object. After the connection with the database is subsequently disconnected, the target connection object can be placed in the connection pool for subsequent requests.
[0057] Step 206: When the interval between the detection time and the current time exceeds the preset interval, the connection status of the target connection object is detected.
[0058] The connection status includes a normal status and an abnormal status. The normal status indicates that the target connection object is available, and the abnormal status indicates that the target connection object is unavailable.
[0059] Specifically, the computer device may determine the current time and the interval between the most recent detection time corresponding to the target connection and the current time. The computer device may obtain a preset interval corresponding to the connection pool and compare the interval between the most recent detection time and the current time with the preset interval.
[0060] When the interval between the detection time and the current time exceeds the preset interval, the computer device may detect the connection status of the target connection object to determine whether the target connection object is abnormal based on the connection status.
[0061] Step 208: When the connection status indicates that the target connection object is in an abnormal state, the preset interval of the connection pool is reset, and the connection with the database is reestablished based on the target connection object.
[0062] Specifically, when the connection state indicates that the target connection object is in an abnormal state, the computer device may reset the preset interval time corresponding to the connection pool. The computer device reestablishes the connection with the database based on the target connection object.
[0063] In this embodiment, the connection pool includes multiple nodes, each node is used to store a connection object; selecting a target connection object from the connection pool includes: determining a target node from the multiple nodes in the connection pool, and using a candidate connection object stored at the target node as the target connection object;
[0064] When the connection status indicates that the target connection object is in an abnormal state, the preset interval of the connection pool is reset, and the connection with the database is rebuilt based on the target connection object, including:
[0065] When the connection state indicates that the target connection object is in an abnormal state, the preset interval of the connection pool is reset. Also, when the connection state indicates that the target connection object is in an abnormal state, the target connection object at the target node is updated, and the connection with the database is rebuilt based on the updated target connection object.
[0066] In one of the embodiments, when the connection status indicates that the target connection object is in an abnormal state, resetting the preset interval duration of the connection pool includes: when the connection status indicates that the target connection object is in an abnormal state, obtaining the abnormality generation time of the target connection object, and updating the preset interval duration of the connection pool based on the abnormality generation time.
[0067] Furthermore, the interval between the time when the exception occurs and the most recent detection time may be determined, and the interval may be updated to a preset interval of the connection pool.
[0068] In some embodiments, after establishing a connection with a database, the computer device can query database information, query data in the database, and modify data in the database through the target connection object. After completing the processing for the database, the target connection object is disconnected and the target connection object is recycled to the connection pool.
[0069] In the above database connection method, when a connection establishment request for a database is received, a connection pool corresponding to the database is determined, and a target connection object is selected from the connection pool. The connection object that can connect to the database can be saved in the connection pool in advance, avoiding the need to create each time the database is connected, thereby improving the connection efficiency of the database. The most recent detection time of the target connection object is obtained, and when the interval between the detection time and the current time exceeds the preset interval, the connection state of the target connection object is detected to determine whether the current target connection object is available through the connection state. When the connection state indicates that the target connection object is in an abnormal state, it means that the target connection object is unavailable, and it means that the state abnormality is detected only at the preset interval, resulting in the target connection object being in an abnormal state for too long, and resetting the preset interval of the connection pool can shorten the abnormal connection time, so that the next connection can be executed normally. In addition, when the target connection object is in an abnormal state, the connection between the target connection object and the database is rebuilt based on the target connection object, so that the database service can automatically and quickly resume normal use after being restarted without selecting a new connection, thereby improving the connection efficiency of the database.
[0070] In one embodiment, when the connection state indicates that the target connection object is in an abnormal state, resetting the preset interval of the connection pool and reestablishing the connection with the database based on the target connection object includes:
[0071] When the connection status indicates that the target connection object is in an abnormal state, the time reset interface of the connection pool is called, and the preset interval length of the connection pool is reset through the time reset interface; the connection with the database is rebuilt based on the target connection object.
[0072] The time reset interface is used to reset the preset interval of the connection pool. The preset interval is used to determine whether to perform connection status detection on the connection objects in the connection pool.
[0073] Specifically, the computer device pre-configures a corresponding time reset interface for the connection pool, and when the connection status indicates that the target connection object is in an abnormal state, the time reset interface of the connection pool is called.
[0074] By calling the time reset interface, the preset interval length of the connection pool is adjusted to obtain the preset interval length after reset.
[0075] Furthermore, when the connection status indicates that the target connection object is in an abnormal state, the computer device reestablishes the connection with the database based on the target connection object.
[0076] In this embodiment, a time reset interface is preset to reset the automatic detection duration of the connection pool. When the connection status indicates that the target connection object is in an abnormal state, the time reset interface of the connection pool is called, so that the preset interval duration of the connection pool can be reset through the time reset interface, which can shorten the abnormal time of the target connection object so that the target connection object can be used normally next time. In addition, when the target connection object is in an abnormal state, the connection with the database is rebuilt based on the target connection object, so that the database service can automatically and quickly resume normal use after being restarted without selecting a new connection, thereby improving the connection efficiency of the database.
[0077] In one embodiment, the method further comprises:
[0078] When the interval between the detection time and the current time does not exceed the preset interval, a connection with the database is established based on the target connection object.
[0079] Specifically, the computer device may obtain a preset interval duration corresponding to the connection pool, and compare the interval duration between the most recent detection time and the current time with the preset interval duration.
[0080] When the interval between the detection time and the current time does not exceed the preset interval, the computer device may establish a connection with the database based on the target connection object.
[0081] In this embodiment, the computer device may update the target connection object and use the updated target connection object to establish a connection with the database.
[0082] In this embodiment, when the interval between the detection time and the current time does not exceed the preset interval, it means that the target connection object is available, and a connection with the database is established based on the target connection object without creating a new connection, effectively improving the connection efficiency of the database.
[0083] In one embodiment, the method further comprises:
[0084] When the connection state indicates that the target connection object is in a normal state, a connection with the database is established based on the target connection object.
[0085] Specifically, the computer device may obtain a preset interval duration corresponding to the connection pool, and compare the interval duration between the most recent detection time and the current time with the preset interval duration.
[0086] When the interval between the detection time and the current time exceeds the preset interval, the computer device may detect the connection status of the target connection object to determine whether the target connection object is abnormal based on the connection status.
[0087] When the connection status indicates that the target connection object is in a normal state, it means that the target connection object is valid, and the computer device can establish a connection with the database based on the target connection object.
[0088] In this embodiment, when the connection state indicates that the target connection object is in a normal state, it indicates that the target connection object is valid, and a connection with the database is established based on the target connection object, so that the connection with the database can be quickly established.
[0089] In one embodiment, when the interval between the detection time and the current time exceeds a preset interval, detecting the connection state of the target connection object includes:
[0090] When the interval between the detection time and the current time exceeds the preset interval, the connection status of the target connection object is detected and the time of detecting the connection status is recorded; the most recent detection time of the target connection object is updated as the time of detecting the connection status.
[0091] Specifically, the computer device calculates whether the interval between the most recent detection time and the current time exceeds a preset interval, detects the connection status of the target connection object, and records the time of detecting the connection status.
[0092] The computer device uses the time of the connection state as the most recent detection time of the target connection to update the most recent detection time.
[0093] In this embodiment, when the interval between the detection time and the current time exceeds the preset interval, the computer device detects the connection status of the target connection object and records the time of detecting the connection status, so as to update the most recent detection time of the target connection object to the time of detecting the connection status, thereby realizing the update of the most recent detection time.
[0094] In one embodiment, the connection pool includes a plurality of nodes, each node storing at least one candidate connection object; selecting a target connection object from the connection pool includes: selecting a target node from the plurality of nodes in the connection pool, and selecting the target connection object from at least one candidate connection object stored in the target node;
[0095] Rebuilding the connection with the database based on the target connection object includes: updating the target connection object at the target node, and rebuilding the connection with the database based on the updated target connection object.
[0096] Specifically, the connection pool includes multiple nodes, each node is used to store at least one connection object, and the computer device can determine the target node from the multiple nodes in the connection pool and select the target connection object from at least one candidate connection object stored in the target node.
[0097] When the connection state indicates that the target connection object is in an abnormal state, the preset interval of the connection pool is reset. Also, when the connection state indicates that the target connection object is in an abnormal state, the target connection object at the target node is updated, and the connection with the database is rebuilt based on the updated target connection object.
[0098] In this embodiment, the target connection object includes at least one of a connection address, a port or a usage status. Updating the target connection object at the target node may specifically update at least one of the connection address, the port or the usage status to obtain an updated target connection object.
[0099] In this embodiment, the connection pool includes multiple nodes, each node stores at least one candidate connection object, selects a target node from the multiple nodes of the connection pool, and selects a target connection object from at least one candidate connection object stored in the target node, and updates the target connection object at the target node, so that the connection with the database can be rebuilt based on the updated target connection object at the same node without selecting a connection object at another node from the connection pool to execute the connection process again, thereby effectively improving the connection efficiency.
[0100] In one embodiment, Figure 3 As shown, a database connection method is provided, including:
[0101] During the specific execution of the business application, the code server determines whether the execution is abnormal. Otherwise, it ends or retries the business. If it is abnormal, it determines whether the connection is abnormal. If so, it can call the interface method of resetting the connection pool check time to reset the connection pool check time, thereby shortening the business abnormality time and making the next execution normal.
[0102] In one embodiment, Figure 4 As shown, a database connection method is provided, including:
[0103] When the code server needs to connect to a database during the process of processing business code, a connection establishment request for the database is generated, and the database is deployed on the database server;
[0104] The code server determines a connection pool corresponding to the database in response to the connection establishment request; the connection pool includes a plurality of nodes, each node stores at least one candidate connection object;
[0105] The code server selects a target node from multiple nodes in the connection pool, and selects a target connection object from at least one candidate connection object saved by the target node;
[0106] The code server obtains the most recent detection time of the target connection object;
[0107] When the interval between the detection time and the current time does not exceed the preset interval, the code server establishes a connection with the database based on the target connection object.
[0108] When the interval between the detection time and the current time does not exceed the preset interval, the code server uses the target connection object to establish a connection with the database;
[0109] When the interval between the detection time and the current time exceeds the preset interval, the connection status of the target connection object is checked and the time of detecting the connection status is recorded;
[0110] Update the most recent detection time of the target connection object to the time of checking the connection status.
[0111] When the connection status indicates that the target connection object is in an abnormal state, that is, the check fails, the time reset interface of the connection pool is called, and the preset interval length of the connection pool is reset through the time reset interface;
[0112] When the connection state indicates that the target connection object is in an abnormal state, updating the target connection object at the target node, and reestablishing the connection with the database based on the updated target connection object;
[0113] When the connection state indicates that the target connection object is in a normal state, that is, the check is normal, a connection with the database is established based on the target connection object.
[0114] In this embodiment, the scope of application of the database connection method includes but is not limited to connection pool applications of network services such as databases, Redis, message queues, etc. In this embodiment, the status check program is not limited, and a certain statement can be executed to determine whether the result meets expectations.
[0115] The database may specifically be a database management system, which is a computer software system designed for managing databases. It generally has basic functions such as storage, interception, security, and backup. It includes relational databases, non-relational databases, etc. Common products include MYSQL, Oracle, SQLServer, etc.
[0116] Redis is an open source log-based, key-value database written in ANSI C language, supporting networks, and can be memory-based or persistent.
[0117] Structured Query Language (SQL) is a special-purpose programming language. It is a database query and programming language used to access data and query, update and manage relational database systems.
[0118] An interface refers to a function with a special function in a programming language.
[0119] In traditional solutions, the connection object is checked for availability before or after each use, which has a significant impact on performance, equivalent to doubling the query operation, which has a significant impact on device performance. However, in this solution, since only one check operation is performed within a time interval, the impact on performance is almost negligible, especially for high-concurrency applications, which may perform more than 1,000 transactions within 60 seconds, but the check operation is only performed once, which has little impact on the overall application performance.
[0120] In the traditional solution, the connection pool program and the database, Redis and other execution programs are completely independent public components in actual use. It is difficult for the two to call each other, and it is very difficult to customize the development of both. In this solution, an interface for resetting the check time is provided, so that the database connection method has high availability. When a connection exception is captured during the specific execution of the business application, the interface method of the time reset interface can be called, thereby shortening the business exception time and making the next execution normal. In addition, by providing a time reset interface, the independence and maintainability of the general connection pool component can be maintained, and customized development is not required when using it, avoiding high-cost customized development.
[0121] In traditional solutions, when the database has a large amount of business access and the connection is in a non-idle state for a long time, the check condition cannot be triggered. Once the connection is abnormally disconnected, it cannot be detected and is still used by the business, causing an execution error, and the error will continue for a long time. In this solution, a detection time interval (for example, 60 seconds) is set, so that after the interval (60 seconds), the abnormality can be automatically detected and reconnected, so that the business application can automatically return to normal. Through the database connection solution of this solution, when a short-term network abnormality occurs and the database and other services are restarted, the network application can automatically and quickly resume normal use.
[0122] It should be understood that, although the steps in the flowcharts involved in the above 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 is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above embodiments may 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.
[0123] Based on the same inventive concept, the embodiment of the present application also provides a database connection device for implementing the database connection method involved above. The implementation solution provided by the device to solve the problem is similar to the implementation solution recorded in the above method, so the specific limitations in one or more database connection device embodiments provided below can refer to the limitations on the database connection method above, and will not be repeated here.
[0124] In one embodiment, Figure 5 As shown, a database connection device 500 is provided, comprising:
[0125] The determination module 502 is configured to determine a connection pool corresponding to a database when a connection establishment request for a database is received.
[0126] The acquisition module 504 is used to select a target connection object from the connection pool and acquire the most recent detection time of the target connection object.
[0127] The detection module 506 is used to detect the connection status of the target connection object when the interval between the detection time and the current time exceeds a preset interval.
[0128] The connection module 508 is configured to reset a preset interval of the connection pool when the connection status indicates that the target connection object is in an abnormal state, and to reestablish the connection with the database based on the target connection object.
[0129] In this embodiment, when a connection establishment request for a database is received, a connection pool corresponding to the database is determined, and a target connection object is selected from the connection pool. The connection object that can connect to the database can be saved in the connection pool in advance, avoiding the need to create each time the database is connected, thereby improving the connection efficiency of the database. The most recent detection time of the target connection object is obtained, and when the interval between the detection time and the current time exceeds the preset interval, the connection state of the target connection object is detected to determine whether the current target connection object is available through the connection state. When the connection state indicates that the target connection object is in an abnormal state, it means that the target connection object is unavailable, and it means that the state abnormality is detected only at the preset interval, resulting in the target connection object being in an abnormal state for too long, and resetting the preset interval of the connection pool can shorten the abnormal connection time, so that the next connection can be executed normally. In addition, when the target connection object is in an abnormal state, the connection between the target connection object and the database is rebuilt based on the target connection object, so that the database service can automatically and quickly resume normal use after being restarted without selecting a new connection, thereby improving the connection efficiency of the database.
[0130] In one embodiment, the connection module 508 is also used to call the time reset interface of the connection pool when the connection status indicates that the target connection object is in an abnormal state, and reset the preset interval duration of the connection pool through the time reset interface; and rebuild the connection with the database based on the target connection object.
[0131] In this embodiment, a time reset interface is preset to reset the automatic detection duration of the connection pool. When the connection status indicates that the target connection object is in an abnormal state, the time reset interface of the connection pool is called, so that the preset interval duration of the connection pool can be reset through the time reset interface, which can shorten the abnormal time of the target connection object so that the target connection object can be used normally next time. In addition, when the target connection object is in an abnormal state, the connection with the database is rebuilt based on the target connection object, so that the database service can automatically and quickly resume normal use after being restarted without selecting a new connection, thereby improving the connection efficiency of the database.
[0132] In one embodiment, the connection module 508 is further configured to establish a connection with the database based on the target connection object when the interval between the detection time and the current time does not exceed a preset interval.
[0133] In this embodiment, when the interval between the detection time and the current time does not exceed the preset interval, it means that the target connection object is available, and a connection with the database is established based on the target connection object without creating a new connection, effectively improving the connection efficiency of the database.
[0134] In one embodiment, the connection module 508 is configured to establish a connection with a database based on the target connection object when the connection state indicates that the target connection object is in a normal state.
[0135] In this embodiment, when the connection state indicates that the target connection object is in a normal state, it indicates that the target connection object is valid, and a connection with the database is established based on the target connection object, so that the connection with the database can be quickly established.
[0136] In one embodiment, the detection module 506 is also used to detect the connection status of the target connection object and record the time of detecting the connection status when the interval between the detection time and the current time exceeds a preset interval; and update the most recent detection time of the target connection object to the time of detecting the connection status.
[0137] In this embodiment, when the interval between the detection time and the current time exceeds the preset interval, the computer device detects the connection status of the target connection object and records the time of detecting the connection status, so as to update the most recent detection time of the target connection object to the time of detecting the connection status, thereby realizing the update of the most recent detection time.
[0138] In one embodiment, the connection pool includes a plurality of nodes, each of which stores at least one candidate connection object; the acquisition module 504 is used to select a target node from the plurality of nodes in the connection pool, and select the target connection object from the at least one candidate connection object stored in the target node;
[0139] The connection module 508 is used to update the target connection object at the target node and rebuild the connection with the database based on the updated target connection object.
[0140] In this embodiment, the connection pool includes multiple nodes, each node stores at least one candidate connection object, selects a target node from the multiple nodes of the connection pool, and selects a target connection object from at least one candidate connection object stored in the target node, and updates the target connection object at the target node, so that the connection with the database can be rebuilt based on the updated target connection object at the same node without selecting a connection object at another node from the connection pool to execute the connection process again, thereby effectively improving the connection efficiency.
[0141] Each module in the above-mentioned database connection device can be implemented in whole or in part by software, hardware or a combination thereof. Each module 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 operations corresponding to each module.
[0142] In one embodiment, a computer device is provided, which may be a server. The server may be a code server or a database server. Its internal structure diagram may be as follows: Figure 6 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store database connection data. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a database connection method is implemented.
[0143] Those skilled in the art will understand that Figure 6The 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.
[0144] The present application also provides a computer-readable storage medium, one or more non-volatile computer-readable storage media containing computer-executable instructions, which, when executed by one or more processors, cause the processors to execute the steps of the above-mentioned methods.
[0145] The embodiments of the present application also provide a computer program product including instructions, which, when executed on a computer, enables the computer to execute the methods of the above embodiments.
[0146] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards of relevant countries and regions.
[0147] Those skilled 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. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. 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). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0148] 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.
[0149] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A database connection method, characterized in that: include: When receiving a connection establishment request for a database, determining a connection pool corresponding to the database; Selecting a target connection object from the connection pool, and obtaining the most recent detection time of the target connection object; When the interval between the detection time and the current time exceeds the preset interval, detecting the connection status of the target connection object; When the connection state indicates that the target connection object is in an abnormal state, the preset interval duration of the connection pool is reset, and the connection with the database is reestablished based on the target connection object.
2. The method according to claim 1, characterized in that When the connection state indicates that the target connection object is in an abnormal state, resetting the preset interval of the connection pool, and reestablishing the connection with the database based on the target connection object, includes: When the connection state indicates that the target connection object is in an abnormal state, calling the time reset interface of the connection pool, and resetting the preset interval length of the connection pool through the time reset interface; The connection with the database is reestablished based on the target connection object.
3. The method according to claim 1, characterized in that The method further comprises: When the interval between the detection time and the current time does not exceed the preset interval, a connection with the database is established based on the target connection object.
4. The method according to claim 2, characterized in that: The method further comprises: When the connection state indicates that the target connection object is in a normal state, a connection with the database is established based on the target connection object.
5. The method according to claim 1, characterized in that When the interval between the detection time and the current time exceeds a preset interval, detecting the connection state of the target connection object includes: When the interval between the detection time and the current time exceeds the preset interval, detecting the connection status of the target connection object and recording the time of detecting the connection status; The most recent detection time of the target connection object is updated as the time of detecting the connection state.
6. The method according to any one of claims 1 to 5, characterized in that: The connection pool includes a plurality of nodes, each of which stores at least one candidate connection object; the step of selecting a target connection object from the connection pool includes: Selecting a target node from a plurality of nodes in the connection pool, and selecting a target connection object from at least one candidate connection object stored in the target node; The reestablishing the connection with the database based on the target connection object comprises: The target connection object at the target node is updated, and the connection with the database is reestablished based on the updated target connection object.
7. A database connection device, characterized in that: include: A determination module, configured to determine a connection pool corresponding to a database upon receiving a connection establishment request for the database; An acquisition module, used to select a target connection object from the connection pool and obtain the most recent detection time of the target connection object; A detection module, configured to detect a connection state of the target connection object when an interval between the detection time and the current time exceeds a preset interval; A connection module is configured to reset the preset interval of the connection pool when the connection state indicates that the target connection object is in an abnormal state, and to reestablish the connection with the database based on the target connection object.
8. A computer device comprising a memory and a processor, wherein a computer program is stored in the memory, wherein: When the computer program is executed by the processor, the processor is caused to perform the steps of the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.