A connection multiplexing method and device based on database distributed communication

By using connection pool cache and multiplexed connection slots in database distributed communication systems, the problem of waste of connection resources in high concurrency scenarios is solved, and the connection utilization rate and system efficiency are improved.

CN118796897BActive Publication Date: 2025-02-07DIGITAL YI TECH (BEIJING) CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411282927.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-13
Publication Date
2025-02-07
Estimated Expiration
2044-09-13

AI Technical Summary

Technical Problem

In high concurrency scenarios, the creation and destruction of distributed communication connections in databases lead to waste of resources and low connection utilization, which limits the concurrent processing capability and read and write performance of the database.

Method used

By caching and multiplexing connection slots in the connection pool, reducing the overhead of connection creation and destruction, multiple transactions and SQL requests share the same connection, updating the reference count of the connection slot to manage connection resources.

Benefits of technology

It improves connection utilization, reduces resource waste, and improves the system's operating efficiency and the concurrent processing capability of the database.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118796897B_ABST
    Figure CN118796897B_ABST
Patent Text Reader

Abstract

The present application belongs to the field of database technology, and specifically discloses a connection reuse method and device based on database distributed communication, the method comprising: receiving an SQL request from a business component, and starting a transaction based on the SQL request; if the current SQL statement is a start transaction statement, searching and determining the first connection slot that matches successfully from the connection pool cache, and taking the first connection slot from the connection pool cache, and executing the current SQL statement according to the first connection slot; looping and executing subsequent SQL statements in the transaction until the transaction ends when the current SQL statement is a commit statement or a rollback statement, updating the reference count of the connection slot, and returning the connection slot to the connection pool or continuing to retain it in the connection pool cache according to the reference count; when all SQL statements are executed, returning the connection slot to the connection pool. Through the present application, the connection utilization rate can be improved to improve the operating efficiency of the system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of database technology, and more specifically, relates to a connection multiplexing method and device based on database distributed communication. Background Art

[0002] Database distributed communication systems are designed to handle data interaction and synchronization in multi-node database clusters. These systems connect multiple database instances through a network to achieve distributed storage and management of data.

[0003] Currently, when managing database distributed communication connections, connection management is usually performed based on the session lifecycle. This approach may result in low utilization of database distributed communication connections, especially in high-concurrency scenarios, where the creation and destruction of connections may cause resource waste.

[0004] Due to insufficient connection utilization, database connection resources may be fixed and cannot be effectively shared in a high-concurrency environment, which will limit the concurrent processing capability of the database and may also lead to a decrease in read and write performance, that is, the overall operating efficiency of the system is low.

[0005] Therefore, how to improve connection utilization to enhance system operating efficiency is a technical problem that needs to be solved urgently. Summary of the invention

[0006] In view of the defects of the prior art, the purpose of the present application is to provide a connection multiplexing method and device based on database distributed communication, aiming to solve the problem of low system operation efficiency caused by insufficient utilization of database connections.

[0007] The present application provides a connection multiplexing method based on database distributed communication, comprising:

[0008] Receive an SQL request from a business component, and start a transaction based on the SQL request;

[0009] If the current SQL statement is a start transaction statement, search and determine the first connection slot that matches successfully from the connection pool cache, take the first connection slot from the connection pool cache, and execute the current SQL statement according to the first connection slot;

[0010] Loop through the subsequent SQL statements in the transaction until the current SQL statement is a commit statement or a rollback statement, ending the transaction, updating the reference count of the connection slot, and returning the connection slot to the connection pool or keeping it in the connection pool cache based on the reference count;

[0011] When all SQL statements are executed, the connection slot is returned to the connection pool.

[0012] Optionally, it also includes:

[0013] If the current SQL statement is not a start transaction statement, search the connection pool dictionary for a connection slot that matches the connection pool according to the database name as the second connection slot;

[0014] The second connection slot is taken from the connection pool, and the second connection slot is assigned to the connection proxy to execute the SQL request based on the second connection slot.

[0015] Optionally, it also includes:

[0016] If there is no matching connection slot in the connection pool, create a new connection slot using the database name as the third connection slot, and determine the initial value of the reference count of the connection slot to be 1;

[0017] Initialize the description information between the current coordinating node of the third connection slot and other coordinating nodes and data nodes, and initialize the network connection handle corresponding to the description information;

[0018] The third connection slot is taken from the connection pool, and the third connection slot is assigned to the connection proxy to execute the SQL request based on the third connection slot.

[0019] Optionally, assigning the third connection slot to a connection proxy to execute the SQL request based on the third connection slot includes:

[0020] Assigning the third connection slot to the connection agent, and determining whether the network connection handle of the third connection slot can communicate normally;

[0021] If normal communication is not possible, a network connection is created according to the connection description information in the third connection slot, and a connection handle of the network connection is stored in the third connection slot, so as to execute the SQL request based on the third connection slot carrying the connection handle;

[0022] Use the connection handle to send SQL requests to the database node and set the connection context according to the session description information.

[0023] Optionally, it also includes:

[0024] Initializing a connection proxy, determining that the connection proxy has stored description information of a current session, and that the connection proxy holds a connection slot obtained from a connection pool;

[0025] The description information of the current session includes the connection properties and parameters of the session, the user name, and the associated database name.

[0026] Optionally, updating the reference count of the connection slot and returning the connection slot to the connection pool or continuing to retain it in the connection pool cache according to the reference count includes:

[0027] When the SQL request is executed, the transaction is determined to be over, and the reference count of the connection slot is reduced by 1;

[0028] If the reference count of the connection slot is 0, the connection slot is returned to the connection pool;

[0029] If the reference count of the connection slot is not 0, the connection slot is placed in the connection pool cache.

[0030] Optionally, the method for determining the first connection slot includes:

[0031] Select the connection slot whose database name, user name connection attributes and connection parameters all match successfully from the connection pool cache as the target connection slot;

[0032] It is determined that the idle time ratio of the target connection slot is greater than a preset threshold, and the target connection slot is used as a first connection slot for executing the SQL request.

[0033] The present application also provides a connection multiplexing device based on database distributed communication, comprising:

[0034] A request receiving module, used to receive an SQL request from a business component and start a transaction based on the SQL request;

[0035] A matching module, configured to search and determine a first connection slot that matches successfully from a connection pool cache if the current SQL statement is a start transaction statement, and to take the first connection slot from the connection pool cache, and to execute the current SQL statement according to the first connection slot;

[0036] A loop execution module is used to loop execute subsequent SQL statements in the transaction until the transaction is ended when the current SQL statement is a commit statement or a rollback statement, update the reference count of the connection slot, and return the connection slot to the connection pool or continue to keep it in the connection pool cache according to the reference count;

[0037] The return module is used to return the connection slot to the connection pool when all SQL statements are executed.

[0038] In a third aspect, the present application provides an electronic device comprising: at least one memory for storing programs; and at least one processor for executing the programs stored in the memory. When the program stored in the memory is executed, the processor is used to execute the method described in the first aspect or any possible implementation of the first aspect.

[0039] In a fourth aspect, the present application provides a computer-readable storage medium, which stores a computer program. When the computer program runs on a processor, the processor executes the method described in the first aspect or any possible implementation of the first aspect.

[0040] In a fifth aspect, the present application provides a computer program product. When the computer program product runs on a processor, the processor executes the method described in the first aspect or any possible implementation of the first aspect.

[0041] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here.

[0042] In general, the above technical solutions conceived by this application have the following beneficial effects compared with the prior art:

[0043] (1) The connection pool in this application allows multiple transactions and SQL requests to share the same connection. By taking an existing connection slot from the connection pool instead of creating a new connection each time, the overhead of connection creation and destruction can be reduced. By caching the connection slot in the connection pool, frequent connection establishment and release can be avoided. The connection pool allocates a connection slot to a transaction and returns the connection slot after the transaction ends, which can ensure the effective use of the connection slot within the transaction life cycle, thereby improving the connection utilization rate and improving the system's operating efficiency.

[0044] (2) This application determines whether a connection slot is still in use by counting the reference count of the connection slot. If the reference count drops to zero, the connection slot can be safely returned to the connection pool for use by other transactions. By deciding whether to return the connection slot to the connection pool or continue to retain it based on the reference count, the connection resources in the connection pool can be effectively managed, thereby further improving the utilization efficiency of the connection resources.

[0045] (3) This application fully utilizes the network connection and reduces the idle time of the network connection. Therefore, in the scenario where the network connection resources are fixed, this application can improve the concurrency capability of the database, and can also improve the read and write performance in the scenario where the network concurrency reaches the bottleneck. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] Figure 1 This is one of the flow charts of the connection multiplexing method based on database distributed communication provided in the embodiment of the present application;

[0047] Figure 2 This is a distributed communication architecture diagram of a distributed database in an embodiment of the present application;

[0048] Figure 3 This is the second flow chart of the connection multiplexing method based on database distributed communication provided in the embodiment of the present application;

[0049] Figure 4 It is a structural schematic diagram of a connection multiplexing device based on database distributed communication provided in an embodiment of the present application;

[0050] Figure 5 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0051] 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.

[0052] The term "and / or" in this article is a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The symbol " / " in this article indicates that the associated objects are in an or relationship, for example, A / B means A or B.

[0053] The terms "first" and "second" in the specification and claims herein are used to distinguish different objects rather than to describe a specific order of the objects. For example, a first response message and a second response message are used to distinguish different response messages rather than to describe a specific order of the response messages.

[0054] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.

[0055] In the description of the embodiments of the present application, unless otherwise specified, "multiple" means two or more than two. For example, multiple processing units refer to two or more processing units, etc.; multiple elements refer to two or more elements, etc.

[0056] Next, the technical solutions provided in the embodiments of the present application are introduced.

[0057] Reference Figure 1 The present application provides a connection multiplexing method based on database distributed communication, comprising:

[0058] S101. Receive an SQL request from a business component and start a transaction based on the SQL request;

[0059] S102. If the current SQL statement is a start transaction statement, search and determine the first connection slot that matches successfully from the connection pool cache, take the first connection slot from the connection pool cache, and execute the current SQL statement according to the first connection slot;

[0060] S103. Loop through the subsequent SQL statements in the transaction until the current SQL statement is a commit statement or a rollback statement, end the transaction, update the reference count of the connection slot, and return the connection slot to the connection pool or continue to retain it in the connection pool cache according to the reference count;

[0061] S104. When all SQL statements are executed, the connection slot is returned to the connection pool.

[0062] First of all, it should be noted that the embodiment of the present application is implemented based on the architecture of a distributed database. Figure 2 , Figure 2 This is a distributed communication architecture diagram of the distributed database of an embodiment of the present application.

[0063] Next, the above steps are described in detail. In this application, step S101 first receives an SQL request from a business component. The system determines that this is an SQL request that needs to be managed, and then opens a new transaction environment according to the received request information.

[0064] In step S102, if the current SQL statement is a start transaction statement, a first connection slot that successfully matches is searched and determined from the connection pool cache, and the first connection slot is taken from the connection pool cache, and the current SQL statement is executed according to the first connection slot.

[0065] In this step, the system checks whether the current SQL statement is a transaction start statement (such as "STARTTRANSACTION" or "BEGIN"). If so, this step will enter the connection processing phase. The system will search the connection pool cache for the first connection slot that matches the database name, user, and connection properties of the current session. At this time, if a valid connection slot is found, the system takes the connection slot from the connection pool and assigns it to the current session for use in subsequent SQL execution. The selection and preparation of connection slots are important measures to ensure efficient resource management and performance optimization. Effective connection management not only reduces resource consumption, but also improves concurrent processing capabilities.

[0066] Once the initial transaction start statement is executed in step S103, the system will enter a loop to process subsequent SQL statements in the transaction. For each SQL statement, the system receives and executes them until a COMMIT or ROLLBACK statement is encountered.

[0067] After executing each SQL statement, the system will update the reference count of the connection slot according to the execution result. The management of the reference count ensures that the connection usage can be effectively tracked when the same connection slot is used by multiple sessions. At the end of the transaction, if the reference count is 0, the connection slot is returned to the connection pool; if the reference count is not 0, the connection slot is retained in the cache for subsequent use.

[0068] In step S104, when all SQL statements are successfully executed, the system returns the required connection resources. At this time, the connection slot is marked as available for use by other sessions. By returning the connection slot to the connection pool, the system effectively manages the database connection resources and ensures the overall performance and high efficiency of resource utilization.

[0069] Furthermore, after the transaction is started, the following steps are also included:

[0070] Initializing a connection proxy, determining that the connection proxy has stored description information of a current session, and that the connection proxy holds a connection slot obtained from a connection pool;

[0071] The description information of the current session includes the connection properties and parameters of the session, the user name, and the associated database name.

[0072] Specifically, after a connection slot is successfully assigned to a connection proxy, an initialization operation needs to be performed first to ensure that the connection proxy can correctly manage the interaction with the database.

[0073] The system generates an instance of a connection proxy that will wrap the connection to the database and provide an operation interface.

[0074] The connection agent stores the description information of the current session so that it can be accessed and used in subsequent operations. The description information of the session usually includes the following key contents: connection properties and parameters, user name, and associated database name.

[0075] The connection pool in the embodiment of the present application allows multiple transactions and SQL requests to share the same connection. By taking an existing connection slot from the connection pool instead of creating a new connection each time, the overhead of connection creation and destruction can be reduced. Caching the connection slot in the connection pool can avoid frequent connection establishment and release. The connection pool allocates a connection slot to a transaction and returns the connection slot after the transaction ends, which can ensure the effective use of the connection slot within the transaction life cycle, thereby improving connection utilization and improving the operating efficiency of the system.

[0076] Optionally, it also includes:

[0077] If the current SQL statement is not a start transaction statement, search the connection pool dictionary for a connection slot that matches the connection pool according to the database name as the second connection slot;

[0078] The second connection slot is taken from the connection pool, and the second connection slot is assigned to the connection proxy to execute the SQL request based on the second connection slot.

[0079] In this embodiment, if the current SQL statement is not a transaction start statement, the system will search for a connection slot matching the database name from the dictionary of the connection pool according to the relevant database name.

[0080] It should be noted that this process may involve the following aspects:

[0081] Database name matching: The system will check each connection slot in the connection pool to find a connection slot that matches the current database name, which will reduce the time to establish a connection and improve query efficiency.

[0082] Connection slot selection: When the system finds a suitable connection slot, it marks it as the "second connection slot". This connection slot will be used for the execution of the current SQL request.

[0083] Once the second connection slot is determined, the system will take the connection slot from the connection pool. Next, the connection slot will be assigned to the connection proxy, which is responsible for maintaining the connection status and related information of the current session, including connection properties, user identity, etc. By assigning the second connection slot to the connection proxy, it can be ensured that the connection slot can be used for subsequent SQL request processing.

[0084] After the second connection slot is assigned to the connection agent, the system will use the connection slot to execute the received SQL request, that is, send the SQL request to the database compute node (CN) and data node (DN) instances, relying on the network connection of the second connection slot to ensure efficient communication.

[0085] The embodiment of the present application effectively utilizes the existing connection slots in the connection pool to avoid the overhead of repeatedly establishing new connections, thereby optimizing the resource utilization of the system. In high-concurrency scenarios, it can significantly reduce the time for connection creation and destruction, and improve overall performance.

[0086] And by quickly finding and utilizing the matching second connection slot, the system can respond quickly when processing non-transactional SQL requests, ensuring the efficiency and immediacy of data operations.

[0087] Optionally, it also includes:

[0088] If there is no matching connection slot in the connection pool, create a new connection slot using the database name as the third connection slot, and determine the initial value of the reference count of the connection slot to be 1;

[0089] Initialize the description information between the current coordinating node of the third connection slot and other coordinating nodes and data nodes, and initialize the network connection handle corresponding to the description information;

[0090] The third connection slot is taken from the connection pool, and the third connection slot is assigned to the connection proxy to execute the SQL request based on the third connection slot.

[0091] Specifically, when the current SQL statement is not a start transaction statement, the system attempts to find a matching connection slot from the connection pool based on the database name. If the system finds that there is no matching connection slot in the connection pool during this search process, the corresponding creation steps will be taken next.

[0092] Create a new connection slot. If no suitable connection slot (i.e., the second connection slot) is found in the connection pool, the system will use the received database name to create a new connection slot, called the third connection slot. This new connection slot will be used exclusively to meet the connection requirements of the current SQL request.

[0093] To facilitate the management of the usage of this connection slot, the system sets the initial value of its reference count to 1. This indicates that the connection slot is currently occupied by a session, which helps with the subsequent management of the connection slot. The system will initialize the description information between the current coordinating node of the third connection slot and other available collaborative nodes and data nodes.

[0094] Furthermore, in addition to the node description information, the system will generate and initialize the corresponding network connection handle for the third connection slot to ensure that an effective network connection can be established to communicate and exchange data with other nodes. After the initialization is completed, the system will take the third connection slot from the connection pool and assign it to the connection agent. The role of the connection agent is still to manage and maintain the session state and connection properties related to the third connection slot.

[0095] Once the third connection slot is successfully assigned to the connection proxy, the system can use this connection slot to execute the corresponding SQL request. This embodiment ensures that even if there is no available connection, the system can still process requests from business components to ensure smooth business operations.

[0096] The embodiment of the present application ensures that when there are no available connection slots in the connection pool, the system can flexibly create new connection slots to effectively respond to changes and demands from the business. By creating new connection slots and managing them with reasonable initial reference counts, the system can meet processing needs without wasting resources. When the reference count is effectively managed, it can ensure that the connection slot is promptly recycled and returned when it is no longer needed. Using reference counts to track the usage of connection slots can effectively avoid connection leaks and resource contention problems, improve system stability and simplify connection management.

[0097] Optionally, assigning the third connection slot to a connection proxy to execute the SQL request based on the third connection slot includes:

[0098] Assigning the third connection slot to the connection agent, and determining whether the network connection handle of the third connection slot can communicate normally;

[0099] If normal communication is not possible, a network connection is created according to the connection description information in the third connection slot, and a connection handle of the network connection is stored in the third connection slot, so as to execute the SQL request based on the third connection slot carrying the connection handle;

[0100] Use the connection handle to send SQL requests to the database node and set the connection context according to the session description information.

[0101] Specifically, in this embodiment, after the third connection slot is created, initialized, and taken out from the connection pool, the system allocates the third connection slot to the connection proxy, which ensures that the connection proxy can effectively manage the connection state related to the third connection slot and is responsible for subsequent request processing.

[0102] The connection agent will then check the network connection handle of the third connection slot to verify whether the connection handle can communicate normally. If the network connection handle can communicate normally, the system will proceed to the next step and use the connection slot to directly execute the SQL request. If the network connection handle cannot communicate normally, the system needs to perform troubleshooting to ensure that the connection can be established and used normally.

[0103] Specifically, if the network connection handle of the third connection slot cannot communicate normally, the system will create a new network connection based on the connection description information in the third connection slot. The system will use the connection description information stored in the third connection slot (such as the address, port, user credentials, etc. of the target database) to create a new network connection. Once the new network connection is successfully established, the system will store the new connection handle of this connection in the third connection slot to update the status of the connection slot.

[0104] After the network is established, the system can use the third connection slot carrying the new connection handle to execute the SQL request. Through the newly established and normal network connection handle, the connection agent sends the received SQL request to the database node.

[0105] When sending an SQL request, the system also needs to set the connection context based on the session description information. This usually includes the user's permissions, the specific state of the connection, the transaction context, etc., to ensure that the SQL request is executed in the correct context.

[0106] The embodiment of the present application enhances the reliability of the connection by automatically creating a new network connection when a network connection handle communication failure occurs, thereby ensuring that the system can still operate normally in the face of network fluctuations or node problems.

[0107] Optionally, updating the reference count of the connection slot and returning the connection slot to the connection pool or continuing to retain it in the connection pool cache according to the reference count includes:

[0108] When the SQL request is executed, the transaction is determined to be over, and the reference count of the connection slot is reduced by 1;

[0109] If the reference count of the connection slot is 0, the connection slot is returned to the connection pool;

[0110] If the reference count of the connection slot is not 0, the connection slot is placed in the connection pool cache.

[0111] Specifically, after processing SQL requests and transactions, the system needs to adjust the reference count of the connection slot, as shown below:

[0112] Reference count minus 1: The reference count of the current connection slot is reduced by 1. This is because after a session is completed, the previous reference can be released, indicating that the number of times the connection slot is used is reduced.

[0113] Check reference count: After decrementing the reference count, the system checks the current reference count state of the connection slot.

[0114] Reference count is 0: If the reference count is reduced to 0, it means that this connection slot is no longer used by any session. In this case, the system will return the connection slot to the connection pool so that it can be reused by other requests.

[0115] Reference count is not 0: If the reference count of the connection slot is still greater than 0, it means that other sessions are still using this connection slot. At this time, the system needs to put the connection slot into the cache of the connection pool to keep its status for subsequent use.

[0116] Put the connection slot back into the connection pool, the connection pool will mark the connection slot as available and make it available for other requests to obtain, ensuring the effective recycling of connection resources. If the reference count is not 0, the connection slot will be put into the connection pool cache, so that the connection slot maintains its state, facilitating quick acquisition by subsequent requests and avoiding repeated initialization and creation overhead.

[0117] Optionally, the method for determining the first connection slot includes:

[0118] Select the connection slot whose database name, user name connection attributes and connection parameters all match successfully from the connection pool cache as the target connection slot;

[0119] It is determined that the idle time ratio of the target connection slot is greater than a preset threshold, and the target connection slot is used as a first connection slot for executing the SQL request.

[0120] Specifically, the system will first traverse all connection slots in the connection pool and check the properties and status of each connection slot. For each connection slot, the system will check the following conditions to determine whether it meets the requirements, including but not limited to: database name, user name, connection properties and parameters, etc.

[0121] For connection slots that meet the above conditions, the system records them as candidate connection slots.

[0122] After finding a matching connection slot, the system needs to further determine its idle time ratio to ensure that the selected connection slot is reasonable in terms of performance and resource utilization. The system will calculate the idle time ratio of each candidate connection slot.

[0123] It should be noted that the idle time ratio generally refers to the ratio of the time that the connection slot is in an idle state in the connection pool to the total existence time of the connection slot.

[0124] If the idle time ratio of the target connection slot is greater than the preset threshold, it means that the connection slot maintains a good active state in the connection pool and is suitable for executing SQL requests.

[0125] If the idle time ratio of the target connection slot is not greater than the preset threshold, the system will not select the connection slot and may continue to look for other candidate connection slots.

[0126] Once a target connection slot that meets the conditions is found, the system selects it as the first connection slot to execute the SQL request. This connection slot will be used to send SQL requests to achieve efficient database interaction.

[0127] Reference Figure 3 , Figure 3 This is a complete flow chart of the connection multiplexing method based on database distributed communication provided by the present application, which includes the following steps:

[0128] 1. The database receives an SQL request from a business component.

[0129] 2. Open a transaction.

[0130] 3. Initialize the connection proxy. It is used to save the description information of the current session and hold the connection slot obtained from the connection pool. (Tips: The session description information includes the connection properties and parameters of the session, the user name, and the associated databaseName.)

[0131] 4. If the current SQL is a start transaction (or Begin) statement, jump to step 4.6, otherwise execute step 4.1 in sequence.

[0132] 4.1. According to the databaseName of the current session, search for a matching connection slot from the connection pool dictionary. If a matching connection slot exists, jump to step 4.3, otherwise execute step 4.2 in order.

[0133] 4.2. Use databaseName to create a new connection slot. The initial value of the connection slot reference count is 1. Initialize the connection description information from the current CN to other CNs and DNs in the connection slot, and initialize the network connection handle corresponding to the connection description information.

[0134] 4.3. Take the connection slot from the connection pool and assign it to the connection agent. If the network connection handle in the connection slot can communicate normally, jump to 4.5, otherwise execute 4.4 in order.

[0135] 4.4. Create a network connection based on the connection description information in the connection slot and store the connection handle in the connection slot.

[0136] 4.5. Use the network connection handle to issue SQL according to the session description information and set the session context of the connection. This step ensures that the same network connection can be used for different session contexts. Go to step 5.

[0137] 4.6. In the connection pool cache, if there are one or more connection slots that match the databaseName, user name, connection attributes, and connection parameters, and there is a connection slot whose idle time ratio is above the configured threshold, then execute step 4.7 in sequence, otherwise jump to step 4.1.

[0138] 4.7. Remove the connection slot from the connection pool cache, and increase the reference count of the connection slot by 1. Execute step 5 in order.

[0139] 5. Use the network connection to send business SQL to the CN and DN instances.

[0140] 6. If the current SQL is a start transaction (or Begin) statement, execute step 6.1, otherwise jump to step 7.

[0141] 6.1. After SQL execution is completed, the connection slot is placed in the connection pool cache.

[0142] 6.2. Read the next SQL statement in the transaction. If it is commit or rollback, jump to step 6.5, otherwise execute step 6.3 in sequence.

[0143] 6.3. Take the matching connection slot from the connection pool cache.

[0144] 6.4. Execute SQL using the connection in the connection slot. SQL execution is complete. Jump to step 6.2.

[0145] 6.5 Take the matching connection slot from the connection pool cache and execute the commit or rollback statement. The transaction ends and the connection slot reference count is -1. If the connection slot reference count is 0, the connection slot is returned to the connection pool, otherwise the connection slot is put into the connection pool cache and the process ends.

[0146] 7. After the SQL execution is completed, the transaction ends and the connection slot is returned to the connection pool, and the process ends.

[0147] Reference Figure 4 The present application also provides a connection multiplexing device based on database distributed communication, including:

[0148] A request receiving module 410 is used to receive an SQL request from a business component and start a transaction based on the SQL request;

[0149] The matching module 420 is used for searching and determining a first connection slot that matches successfully from the connection pool cache if the current SQL statement is a start transaction statement, taking the first connection slot from the connection pool cache, and executing the current SQL statement according to the first connection slot;

[0150] A loop execution module 430 is used to loop execute subsequent SQL statements in the transaction until the transaction is ended when the current SQL statement is a commit statement or a rollback statement, update the reference count of the connection slot, and return the connection slot to the connection pool or continue to keep it in the connection pool cache according to the reference count;

[0151] The return module 440 is used to return the connection slot to the connection pool when all SQL statements are executed.

[0152] Optionally, a search module is further included, for:

[0153] If the current SQL statement is not a start transaction statement, search the connection pool dictionary for a connection slot that matches the connection pool according to the database name as the second connection slot;

[0154] The second connection slot is taken from the connection pool, and the second connection slot is assigned to the connection proxy to execute the SQL request based on the second connection slot.

[0155] Optionally, a creation module is also included for:

[0156] If there is no matching connection slot in the connection pool, create a new connection slot using the database name as the third connection slot, and determine the initial value of the reference count of the connection slot to be 1;

[0157] Initialize the description information between the current coordinating node of the third connection slot and other coordinating nodes and data nodes, and initialize the network connection handle corresponding to the description information;

[0158] The third connection slot is taken from the connection pool, and the third connection slot is assigned to the connection proxy to execute the SQL request based on the third connection slot.

[0159] Optionally, assigning the third connection slot to a connection proxy to execute the SQL request based on the third connection slot includes:

[0160] Assigning the third connection slot to the connection agent, and determining whether the network connection handle of the third connection slot can communicate normally;

[0161] If normal communication is not possible, a network connection is created according to the connection description information in the third connection slot, and a connection handle of the network connection is stored in the third connection slot, so as to execute the SQL request based on the third connection slot carrying the connection handle;

[0162] Use the connection handle to send SQL requests to the database node and set the connection context according to the session description information.

[0163] Optionally, an initialization module is also included, for:

[0164] Initializing a connection proxy, determining that the connection proxy has stored description information of a current session, and that the connection proxy holds a connection slot obtained from a connection pool;

[0165] The description information of the current session includes the connection properties and parameters of the session, the user name, and the associated database name.

[0166] Optionally, updating the reference count of the connection slot and returning the connection slot to the connection pool or continuing to retain it in the connection pool cache according to the reference count includes:

[0167] When the SQL request is executed, the transaction is determined to be over, and the reference count of the connection slot is reduced by 1;

[0168] If the reference count of the connection slot is 0, the connection slot is returned to the connection pool;

[0169] If the reference count of the connection slot is not 0, the connection slot is placed in the connection pool cache.

[0170] Optionally, the method for determining the first connection slot includes:

[0171] Select the connection slot whose database name, user name connection attributes and connection parameters all match successfully from the connection pool cache as the target connection slot;

[0172] It is determined that the idle time ratio of the target connection slot is greater than a preset threshold, and the target connection slot is used as a first connection slot for executing the SQL request.

[0173] It can be understood that the detailed functional implementation of each of the above-mentioned units / modules can be found in the introduction of the aforementioned method embodiment, and will not be repeated here.

[0174] It should be understood that the above-mentioned device is used to execute the method in the above-mentioned embodiment. The implementation principle and technical effect of the corresponding program module in the device are similar to those described in the above-mentioned method. The working process of the device can refer to the corresponding process in the above-mentioned method, which will not be repeated here.

[0175] Reference Figure 5 Based on the method in the above embodiment, the embodiment of the present application provides an electronic device, which may include: a processor (processor) 510, a communication interface (Communications Interface) 520, a memory (memory) 530 and a communication bus 540, wherein the processor 510, the communication interface 520, and the memory 530 communicate with each other through the communication bus 540. The processor 510 can call the logic instructions in the memory 530 to execute the method in the above embodiment.

[0176] In addition, the logic instructions in the above-mentioned memory 530 can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application.

[0177] Based on the method in the above embodiment, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program runs on a processor, the processor executes the method in the above embodiment.

[0178] Based on the method in the above embodiment, an embodiment of the present application provides a computer program product. When the computer program product runs on a processor, the processor executes the method in the above embodiment.

[0179] It is understandable that the processor in the embodiment of the present application may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. The general-purpose processor may be a microprocessor or any conventional processor.

[0180] The method steps in the embodiments of the present application can be implemented by hardware or by a processor executing software instructions. The software instructions can be composed of corresponding software modules, and the software modules can be stored in random access memory (RAM), flash memory, read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), registers, hard disks, mobile hard disks, CD-ROMs, or any other form of storage medium known in the art. An exemplary storage medium is coupled to the processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an ASIC.

[0181] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted through the computer-readable storage medium. The computer instructions may be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid state disk (SSD)), etc.

[0182] It should be understood that the various numerical numbers involved in the embodiments of the present application are only used for the convenience of description and are not used to limit the scope of the embodiments of the present application.

[0183] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present application shall be included in the scope of protection of the present application.

Claims

1. A connection multiplexing method based on database distributed communication, characterized in that: include: Receive an SQL request from a business component, and start a transaction based on the SQL request; If the current SQL statement is a start transaction statement, search and determine the first connection slot that matches successfully from the connection pool cache, take the first connection slot from the connection pool cache, increase the reference count of the connection slot by 1, and execute the current SQL statement according to the first connection slot; the first connection slot is obtained by: selecting a connection slot that matches successfully in terms of database name, user name, connection attributes, and connection parameters from the connection pool cache as the first connection slot; Loop through the subsequent SQL statements in the transaction until the current SQL statement is a commit statement or a rollback statement, ending the transaction, updating the reference count of the connection slot, and returning the connection slot to the connection pool or keeping it in the connection pool cache based on the reference count; When all SQL statements are executed, the connection slot is returned to the connection pool; The updating of the reference count of the connection slot and returning the connection slot to the connection pool or continuing to retain it in the connection pool cache according to the reference count includes: When the SQL request is executed, the transaction is determined to be over, and the reference count of the connection slot is reduced by 1; If the reference count of the connection slot is 0, the connection slot is returned to the connection pool; If the reference count of the connection slot is not 0, the connection slot is placed in the connection pool cache.

2. The connection multiplexing method based on database distributed communication according to claim 1 is characterized in that: Also includes: If the current SQL statement is not a start transaction statement, search the connection pool dictionary for a connection slot that matches the connection pool according to the database name as the second connection slot; The second connection slot is taken from the connection pool, and the second connection slot is assigned to the connection proxy to execute the SQL request based on the second connection slot.

3. The connection multiplexing method based on database distributed communication according to claim 2 is characterized in that: Also includes: If there is no matching connection slot in the connection pool, create a new connection slot using the database name as the third connection slot, and determine the initial value of the reference count of the connection slot to be 1; Initialize the description information between the current coordinating node of the third connection slot and other coordinating nodes and data nodes, and initialize the network connection handle corresponding to the description information; The third connection slot is taken from the connection pool, and the third connection slot is assigned to the connection proxy to execute the SQL request based on the third connection slot.

4. The connection multiplexing method based on database distributed communication according to claim 3 is characterized in that: The assigning the third connection slot to the connection agent to execute the SQL request based on the third connection slot includes: Assigning the third connection slot to the connection agent, and determining whether the network connection handle of the third connection slot can communicate normally; If normal communication is not possible, a network connection is created according to the connection description information in the third connection slot, and a connection handle of the network connection is stored in the third connection slot, so as to execute the SQL request based on the third connection slot carrying the connection handle; Use the connection handle to send SQL requests to the database node and set the connection context according to the session description information.

5. The connection multiplexing method based on database distributed communication according to claim 2 is characterized in that: Also includes: Initializing a connection proxy, determining that the connection proxy has stored description information of a current session, and that the connection proxy holds a connection slot obtained from a connection pool; The description information of the current session includes the connection properties and parameters of the session, the user name, and the associated database name.

6. The connection multiplexing method based on database distributed communication according to claim 1, characterized in that: Also includes: After finding a matching connection slot, it is determined that the idle time ratio of the target connection slot is greater than a preset threshold, and the target connection slot is used as the first connection slot for executing the SQL request.

7. A connection multiplexing device based on database distributed communication, characterized in that: include: A request receiving module, used to receive an SQL request from a business component and start a transaction based on the SQL request; A matching module is used for searching and determining a first connection slot that matches successfully from a connection pool cache if the current SQL statement is a start transaction statement, taking the first connection slot from the connection pool cache, adding 1 to the reference count of the connection slot, and executing the current SQL statement according to the first connection slot; the first connection slot is obtained by selecting a connection slot that matches successfully in terms of database name, user name, connection attributes, and connection parameters from the connection pool cache as the first connection slot; A loop execution module is used to loop execute subsequent SQL statements in the transaction until the transaction is ended when the current SQL statement is a commit statement or a rollback statement, update the reference count of the connection slot, and return the connection slot to the connection pool or continue to keep it in the connection pool cache according to the reference count; A return module, used to return the connection slot to the connection pool when all SQL statements are executed; The updating of the reference count of the connection slot and returning the connection slot to the connection pool or continuing to retain it in the connection pool cache according to the reference count includes: When the SQL request is executed, the transaction is determined to be over, and the reference count of the connection slot is reduced by 1; If the reference count of the connection slot is 0, the connection slot is returned to the connection pool; If the reference count of the connection slot is not 0, the connection slot is placed in the connection pool cache.

8. An electronic device, characterized in that: include: at least one memory for storing a computer program; At least one processor is used to execute the program stored in the memory. When the program stored in the memory is executed, the processor is used to execute the method according to any one of claims 1 to 6.

9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program runs on a processor, the processor is caused to execute the method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • System and method for providing access to a sharded database using a cache and a shard topology

    CN107408128A

  • Transactional database read-write separation implementation method

    CN111061801A