Database connection-based request processing method, apparatus and device, and storage medium
By using preset connection components and connection mapping relationships in database connections, the data source identifier and target connection channel are dynamically determined, which solves the problem of low flexibility in connection switching in the existing technology and improves the efficiency and accuracy of request processing.
Patent Information
- Application Number
- CN202410872111.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-01
- Publication Date
- 2024-09-27
AI Technical Summary
The existing database connection method obtains the data source identifier during the transaction opening phase, resulting in low flexibility in connection switching and affecting the accuracy and efficiency of request processing.
By configuring the connection mapping relationship in the preset connection component, the data source identifier and target connection channel are dynamically determined according to the access request in the transaction, and flexible switching of database connections is achieved.
It improves the switching flexibility of database connections, improves the efficiency and accuracy of request processing, and meets actual business needs.
Smart Images

Figure CN118708622A
Abstract
Description
Technical Field
[0001] The present application relates to the field of big data technology, and in particular to a request processing method, device, equipment and storage medium based on database connection. Background Art
[0002] A transaction may include multiple database access requests, and different database access requests may require different connection channels to the database for processing. Currently, when connecting to a database, a pre-configured data source identifier can be obtained, and based on the data source identifier, the request is routed to the target data source, which then provides the database connection.
[0003] However, in this method, the timing of calling the data source identifier to obtain the connection is during the transaction start phase. After the transaction is started, the connection cannot be switched. The flexibility of connection switching is low, which affects the processing accuracy and efficiency of the request. Summary of the invention
[0004] The present application provides a request processing method, apparatus, device and storage medium based on database connection, so as to improve the processing accuracy and efficiency of requests.
[0005] In a first aspect, the present application provides a method for processing a request based on a database connection, the method being applied to an electronic device, wherein a preset connection component is configured in the electronic device, wherein the preset connection component includes a connection mapping relationship, wherein the connection mapping relationship represents an association relationship between a data source identifier and a connection channel, wherein the data source identifier represents a data source, and wherein the data source includes configuration information of a connection channel between a client and a database; the method comprising:
[0006] Acquire an access request of the client to the database from a transaction initiated by the client, and determine, according to the access request, a pre-configured data source identifier and a request operation corresponding to the access request;
[0007] According to the connection mapping relationship in the preset connection component, determine the connection channel corresponding to the pre-configured data source identifier as the target connection channel;
[0008] According to the target connection channel, a request operation corresponding to the access request is executed in the database.
[0009] In a possible design, determining, according to the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier as a target connection channel includes:
[0010] If the pre-configured data source identifier is not a preset identifier string, searching the pre-configured data source identifier from the connection mapping relationship in the preset connection component; wherein the preset identifier string indicates that the pre-configured data source identifier is a null value;
[0011] If the pre-configured data source identifier exists in the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier is determined as the target connection channel.
[0012] In one possible design, it also includes:
[0013] If the pre-configured data source identifier is a preset identifier string, obtaining a preset default identifier;
[0014] According to the connection mapping relationship in the preset connection component, a connection channel corresponding to the preset default identifier is determined as the target connection channel.
[0015] In a possible design, the preset connection component includes a preset data source instance object, and the preset data source instance object represents a preset connection channel; the method further includes:
[0016] If the pre-configured data source identifier does not exist in the connection mapping relationship in the preset connection component, then obtaining configuration information in the data source represented by the pre-configured data source identifier as the target information;
[0017] The target connection channel is created according to the preset data source instance object and the target information.
[0018] In a possible design, the preset connection component also includes an operation identifier of the client, and the operation identifier represents an operation behavior performed by the client on the preset connection channel represented by the preset data source instance object; creating the target connection channel according to the preset data source instance object and the target information includes:
[0019] Determining an initial connection channel according to the preset data source instance object and the target information;
[0020] The initial connection channel is initialized and configured according to the operation identifier to obtain the target connection channel.
[0021] In a possible design, initializing and configuring the initial connection channel according to the operation identifier to obtain the target connection channel includes:
[0022] Determine the operation behavior corresponding to the operation identifier;
[0023] The operation behavior corresponding to the operation identifier is executed on the initial connection channel to obtain the target connection channel.
[0024] In a possible design, the preset connection component also includes preset channel attribute information, and the preset channel attribute information represents a preset value of a corresponding attribute in the connection channel; determining the initial connection channel according to the preset data source instance object and the target information includes:
[0025] Determine the connection channel to be assigned according to the preset data source instance object and the target information;
[0026] According to the preset channel attribute information, a value assignment operation is performed on the corresponding attribute of the connection channel to be assigned a value, so as to obtain the initial connection channel.
[0027] In a possible design, before obtaining the client's access request to the database from the transaction initiated by the client, the method further includes:
[0028] In response to a transaction initiation request from a client, obtaining the preset data source instance object from a preset connection component;
[0029] A preset connection channel represented by the preset data source instance object is determined.
[0030] In a possible design, after creating the target connection channel according to the preset data source instance object and the target information, the method further includes:
[0031] The pre-configured data source identifier is associated with the target connection channel and stored in the connection mapping relationship.
[0032] In a possible design, a transaction initiated by a client includes multiple access requests; the method further includes:
[0033] If all access requests in the transaction are executed, traversing the connection channels in the connection mapping relationship;
[0034] A disconnect operation is performed on each connection channel in the connection mapping relationship.
[0035] In a second aspect, the present application provides a request processing device based on database connection, the device is applied to an electronic device, the electronic device is configured with a preset connection component, the preset connection component includes a connection mapping relationship, the connection mapping relationship represents the association relationship between the data source identifier and the connection channel, the data source identifier represents the data source, and the data source includes configuration information of the connection channel between the client and the database; the device includes:
[0036] An identification determination module, used to obtain an access request of the client to the database from a transaction initiated by the client, and determine a pre-configured data source identification and a request operation corresponding to the access request according to the access request;
[0037] A channel determination module, configured to determine, according to the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier as a target connection channel;
[0038] A request execution module is used to execute a request operation corresponding to the access request in the database according to the target connection channel.
[0039] In a third aspect, an embodiment of the present application provides an electronic device, comprising: at least one processor and a memory; the memory stores computer execution instructions; the at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor executes the request processing method based on database connection as described in the first aspect and various possible designs of the first aspect.
[0040] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which computer execution instructions are stored. When a processor executes the computer execution instructions, the request processing method based on database connection as described in the first aspect and various possible designs of the first aspect is implemented.
[0041] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program. When the computer program is executed by a processor, it implements the request processing method based on database connection as described in the first aspect and various possible designs of the first aspect.
[0042] The present application provides a method, device, equipment and storage medium for request processing based on database connection. By pre-configuring a connection component, the client can connect to the database at any time according to the access request in the transaction after initiating the transaction, without the need to connect at the transaction start stage. The data source identifier can be determined according to the access request in the transaction, and the connection channel corresponding to the data source identifier can be determined according to the connection mapping relationship in the preset connection component as the target connection channel, so that the client can connect to the database through the target connection channel. According to the target connection channel, the request operation corresponding to the access request is executed in the database. When executing another access request, other connection channels can be switched, so that the connection of the database can be switched during the processing of the access request, the flexibility of the connection switching can be improved, and the actual business needs can be met, thereby improving the efficiency and accuracy of the request processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0044] Figure 1 A flowchart of a request processing method based on database connection provided in an embodiment of the present application;
[0045] Figure 2 A schematic diagram of switching data sources provided in an embodiment of the present application;
[0046] Figure 3 A flowchart of a request processing method based on database connection provided in an embodiment of the present application;
[0047] Figure 4 A flowchart of a request processing method based on database connection provided in an embodiment of the present application;
[0048] Figure 5 A schematic diagram of the structure of a request processing device based on database connection provided in an embodiment of the present application;
[0049] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0050] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0051] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.
[0052] The acquisition, transmission, storage, use, and processing of data in the technical solution of this application are in compliance with the relevant provisions of laws and regulations. It should be noted that in the embodiments of this application, some existing solutions in the industry such as certain software, components, and models may be mentioned, and they should be considered as exemplary. Their purpose is only to illustrate the feasibility of the implementation of the technical solution of this application, but it does not mean that the applicant has or will necessarily use the solution.
[0053] Spring Boot is an open source application framework on the Java platform. The multi-data source management technology in Spring Boot applications has always been a challenge in business data source management. Because the most common usage scenario of Spring is to share data connections across methods based on annotations and thread-private caches, Spring Boot applications naturally lack the convenience of manipulating multiple data sources in the same thread.
[0054] For this purpose, a data source proxy component Dynamic RoutingData Source is provided, which is specifically designed for multi-data source management. By default, this component relies on the @DS annotation and Thread Local mechanism to switch the data source identifier during program execution, and when the program obtains the data source, it routes to the target data source based on the current data source identifier to provide data connection. This method, to a certain extent, implements the simplest data source switching problem in SpringBoot applications. Users can use the @DS annotation to mark the currently used data source outside the transaction boundary.
[0055] However, the application scope of this method is extremely limited. Spring's transaction manager shares the current connection in multi-level method calls based on the ThreadLocal mechanism. Therefore, the timing of calling the data source to obtain the connection is usually at the transaction start stage, and the subsequent switching of the data source will not work, and may even cause program errors. For this reason, it is necessary to implement a dynamic proxy data source that can switch the data source within the transaction to improve the processing efficiency and accuracy of requests in the transaction.
[0056] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0057] Figure 1 The present invention is a flowchart of a method for processing a request based on a database connection according to an embodiment of the present application. The method is applied to an electronic device, and a preset connection component is configured in the electronic device. The preset connection component includes a connection mapping relationship, and the connection mapping relationship represents the association relationship between the data source identifier and the connection channel. The data source identifier represents the data source, and the data source includes the configuration information of the connection channel between the client and the database. Figure 1 As shown, the method comprises the following steps:
[0058] S101: Obtain a client's access request to a database from a transaction initiated by the client, and determine a pre-configured data source identifier and a request operation corresponding to the access request according to the access request.
[0059] Exemplarily, a transaction is a sequence of database operations that access and possibly operate on various data items. These operations are either all executed or not executed at all, and are an indivisible unit of work. A transaction consists of all database operations performed between the start of the transaction and the end of the transaction. A transaction may include multiple requests, for example, a request to access a database. Different connections may be required to connect to the database for different requests. Each data source corresponds to a connection, and the data source includes configuration information for the database connection, for example, configuration of access rights to the database, configuration of performance constraints on the database, etc. Different data sources can connect to the same database or to different databases. During transaction processing, the database connection can be switched by switching the data source.
[0060] The client of the database can issue an access request to the database when initiating a transaction. That is, the client's access request to the database can be obtained from the transaction initiated by the client, and one access request can be obtained each time. After responding to the access request, the pre-configured data source identifier can be obtained, and the request operation corresponding to the access request can be determined. Each data source corresponds to a data source identifier, that is, the data source identifier can represent the data source. The data source identifier can be pre-configured in the @DS annotation, and the string of the data source identifier can be obtained from the @DS annotation. The obtained string is pushed onto the stack, and the string is obtained from the stack according to the last-in-first-out principle to obtain the data source identifier.
[0061] Each access request corresponds to its own request operation, which represents the operation that the client wants to perform on the database and can be represented by a SQL (Structured Query Language) statement.
[0062] S102: According to the connection mapping relationship in the preset connection component, determine the connection channel corresponding to the pre-configured data source identifier as the target connection channel.
[0063] Exemplarily, a connection component is pre-set, which can be called Dynamic RoutingConnection, and provides a connection for the transaction in the transaction start phase. The connection component stores a connection mapping relationship, which can represent the association between the data source identifier and the connection channel. The connection channel is the connection between the client and the database, and the connection channel can be called Connection. There are multiple data sources, and each data source corresponds to its own Connection.
[0064] After determining the data source identifier corresponding to the access request, the Connection corresponding to the data source identifier can be searched from the connection mapping relationship as the target connection channel.
[0065] S103: Execute a request operation corresponding to the access request in the database according to the target connection channel.
[0066] Exemplarily, a target connection channel is determined, and based on the target connection channel, a connection between the client and the database is realized, so that the request operation corresponding to the access request is executed in the database. That is, the SQL operation is executed using the current Connection. When responding to the access request, the connection is switched, that is, the data source is switched. In this embodiment, the execution process of the request operation is not specifically limited.
[0067] Figure 2 A schematic diagram for switching data sources. Figure 2 In the example, there are two data sources, data source 1 and data source 2. Both data source 1 and data source 2 can connect to the same database. When executing a request, you can connect to the database through data source 1; when executing another request, you can switch to data source 2 to connect to the database.
[0068] In this embodiment, the transaction initiated by the client includes multiple access requests; the method further includes: if all access requests in the transaction are executed, traversing the connection channels in the connection mapping relationship; and performing a disconnect operation on each connection channel in the connection mapping relationship.
[0069] Specifically, a transaction initiated by a client may include multiple access requests. When processing each access request, the data source is switched according to the corresponding data source identifier, that is, the database connection is switched. Each access request is executed under the corresponding connection.
[0070] It is possible to determine in real time or periodically whether each access request in a transaction has been executed. If there are access requests in the transaction that have not been executed, the unfinished access requests will continue to be executed until all requests are executed. If all access requests in the transaction have been executed, the connection mapping relationship in the Dynamic Routing Connection is obtained. The connection mapping relationship includes the association relationship between multiple data sources and connection channels. Each connection channel in the connection mapping relationship is traversed, and a disconnection operation is performed on each connection channel in the connection mapping relationship. That is, the connection between the client and each database is cancelled.
[0071] Disconnection operations can include commit, rollback, and close operations, among which only one of commit and rollback can be executed. After commit or rollback is executed, close operation is executed to complete a disconnection operation. For each connection, commit or rollback is executed first, and then close operation is executed.
[0072] The beneficial effect of such a setting is that after the transaction is completed, the connection is disconnected in time to avoid subsequent erroneous operations through the connection and ensure the safety of the operation.
[0073] This embodiment pre-configures a connection component so that after initiating a transaction, the client can connect to the database at any time according to the access request in the transaction, without having to connect at the transaction start stage. The data source identifier can be determined according to the access request in the transaction, and the connection channel corresponding to the data source identifier can be determined according to the connection mapping relationship in the preset connection component as the target connection channel, so that the client can connect to the database through the target connection channel. According to the target connection channel, the request operation corresponding to the access request is executed in the database. When executing another access request, other connection channels can be switched, so that the connection to the database can be switched during the processing of the access request, which improves the flexibility of the connection switching, can meet actual business needs, and thus improves the efficiency and accuracy of request processing.
[0074] Figure 3 A flowchart of a request processing method based on database connection provided in an embodiment of the present application.
[0075] In this embodiment, according to the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier is determined as the target connection channel, including: if the pre-configured data source identifier is not a preset identification string, searching the pre-configured data source identifier from the connection mapping relationship in the preset connection component; wherein the preset identification string indicates that the pre-configured data source identifier is a null value; if the pre-configured data source identifier exists in the connection mapping relationship in the preset connection component, determining the connection channel corresponding to the pre-configured data source identifier as the target connection channel.
[0076] like Figure 3 As shown, the method comprises the following steps:
[0077] S301: Obtain a client's access request to a database from a transaction initiated by the client, and determine a pre-configured data source identifier and a request operation corresponding to the access request according to the access request.
[0078] Exemplarily, this step may refer to the above-mentioned step S101 and will not be described in detail.
[0079] S302: If the pre-configured data source identifier is not a preset identifier string, search for the pre-configured data source identifier from a connection mapping relationship in a preset connection component; wherein the preset identifier string indicates that the pre-configured data source identifier is a null value.
[0080] Exemplarily, a string is pre-set as a preset identification string, for example, the preset identification string can be a null value. It is determined whether the pre-configured data source identifier is a preset identification string. If so, it is determined that there is no pre-configured data source identifier; if not, it is possible to search from the connection mapping relationship whether the pre-configured data source identifier exists. For example, if the pre-configured data source identifier is 001, it is possible to search from the connection mapping relationship whether a data source identifier of 001 exists.
[0081] In this embodiment, the method further includes: if the pre-configured data source identifier is a preset identifier string, obtaining a preset default identifier; and determining, according to a connection mapping relationship in a preset connection component, a connection channel corresponding to the preset default identifier as the target connection channel.
[0082] Specifically, if the pre-configured data source identifier is a preset identifier string, for example, the pre-configured data source identifier is a null value, then a preset default identifier is obtained. The default identifier is a default data source identifier, and when there is no pre-set data source identifier, the default identifier is obtained.
[0083] A connection channel is configured for the default identifier in advance, and the default identifier and the corresponding connection channel are associated and stored in the connection mapping relationship. After the default identifier is determined, the connection channel corresponding to the default identifier is determined from the connection mapping relationship in the preset connection component as the target connection channel. That is, when the data source identifier is not pre-configured, the default connection channel is used as the target connection channel.
[0084] The beneficial effect of such a setting is that a default identifier and a corresponding connection channel are pre-set to ensure that even when a data source identifier is not configured, requests can be processed through the default connection channel, thereby effectively improving request processing efficiency.
[0085] S303: If a pre-configured data source identifier exists in the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier is determined as the target connection channel.
[0086] Exemplarily, it is determined whether the pre-configured data source identifier exists in the connection mapping relationship. If so, a connection channel corresponding to the pre-configured data source identifier can be determined from the connection mapping relationship as the target connection channel.
[0087] In this embodiment, the preset connection component includes a preset data source instance object, and the preset data source instance object represents a preset connection channel; the method also includes: if there is no pre-configured data source identifier in the connection mapping relationship in the preset connection component, then obtaining configuration information in the data source represented by the pre-configured data source identifier as the target information; and creating a target connection channel according to the preset data source instance object and the target information.
[0088] Specifically, a preset data source instance object may also be set in the Dynamic Routing Connection. The preset data source instance object represents an instance of a specific connection channel. The preset data source instance object may be called an instance object of the Dynamic Routing Data Source, and may be used to dynamically create a Connection that meets the data source requirements later.
[0089] Determine whether there is a pre-configured data source identifier in the connection mapping relationship in the preset connection component. If not, it is determined that the client has not yet connected to the database through the data source identified by the data source, and a connection needs to be created now. Different data sources correspond to different configuration information, and the configuration information can be used to create different connection channels. The configuration information in the data source represented by the pre-configured data source identifier can be obtained as the target information. Create a target connection channel based on the preset data source instance object and target information. The data source instance object can represent the form of the target connection channel. According to the target information, a connection channel in the form of a data source instance object can be created as the target connection channel.
[0090] The beneficial effect of this setting is that a Connection that meets the requirements of the data source can be dynamically created according to actual conditions, thereby improving the flexibility of database connections and helping to meet actual business needs.
[0091] In this embodiment, the preset connection component also includes an operation identifier of the client, and the operation identifier represents the operation behavior performed by the client on the preset connection channel represented by the preset data source instance object; creating a target connection channel according to the preset data source instance object and target information includes: determining an initial connection channel according to the preset data source instance object and target information; and initializing the initial connection channel according to the operation identifier to obtain the target connection channel.
[0092] Specifically, the connection component also contains the operation identifier of the client, which represents the operation behavior performed by the client on the preset connection channel represented by the preset data source instance object, that is, the connection component records the operation behavior performed by the client on the preset connection channel. An operation sequence set can be provided, and the operation sequence set contains the operation identifiers of the operation behaviors performed by the client, and the operation identifiers are arranged in the order in which the operation behaviors are performed.
[0093] According to the preset data source instance object and target information, a connection channel is created as the initial connection channel. In this embodiment, the process of creating the connection channel is not specifically limited. There is no operation of any client in the initial connection channel, and the operation of the client on the preset connection channel needs to be copied to the initial connection channel. The initial connection channel can be initialized and configured according to the obtained operation identifier. The initialization configuration refers to the operation behavior corresponding to the operation identifier existing in the initial connection channel, and the connection channel obtained after the initialization configuration is determined as the target connection channel.
[0094] The beneficial effect of this setting is that the initial Connection is obtained and initialized according to the operation behavior, so that the created Connection is consistent with the preset Connection, thereby improving the accuracy of request processing.
[0095] In this embodiment, initializing and configuring the initial connection channel according to the operation identifier to obtain the target connection channel includes: determining the operation behavior corresponding to the operation identifier; and performing the operation behavior corresponding to the operation identifier on the initial connection channel to obtain the target connection channel.
[0096] Specifically, each operation identifier corresponds to its own operation behavior. After determining the operation identifier, the operation behavior corresponding to each operation identifier is determined. According to the arrangement order of the operation identifiers in the operation sequence set, the operation behaviors corresponding to each operation identifier are sequentially executed on the initial connection channel to obtain the target connection channel.
[0097] For example, the operation identifier includes "01" and "02", "01" indicates the set Auto Commit operation, and "02" indicates the set Read Only operation. The set Auto Commit operation is an operation for setting whether to automatically commit, and the set Read Only operation is an operation for setting whether to read only. Based on the obtained operation identifier, the operation of whether to automatically commit and whether to read only can be set for the initial connection channel, so that the state of the target connection channel is consistent with the state of the preset connection channel.
[0098] The beneficial effect of such a setting is that the operation behavior is set for the initial connection channel, ensuring that the operation behavior of the client is recorded in the target connection channel, which facilitates the correct processing of subsequent requests.
[0099] In this embodiment, the preset connection component also includes preset channel attribute information, and the preset channel attribute information represents the preset assignment of corresponding attributes in the connection channel; determining the initial connection channel according to the preset data source instance object and target information includes: determining the connection channel to be assigned according to the preset data source instance object and target information; and performing an assignment operation on the corresponding attributes of the connection channel to be assigned according to the preset channel attribute information to obtain the initial connection channel.
[0100] Specifically, the connection component also stores preset channel attribute information, which includes preset values corresponding to various attributes in the connection channel. That is, the connection channel may have multiple attributes, such as type attributes, method attributes, etc. The value of each attribute is preset as a preset value and stored in the connection component.
[0101] When creating an initial connection channel, a connection channel may be first created according to the preset data source instance object and target information as the connection channel to be assigned. In the connection channel to be assigned, the assigned value of each attribute is a null value. According to the preset assignment in the preset channel attribute information, the corresponding attribute of the connection channel to be assigned is assigned, thereby obtaining the initial connection channel.
[0102] The beneficial effect of such a setting is that, by assigning values to the attributes of the created connection channel, the creation accuracy of the target connection channel is improved, thereby improving the processing accuracy of the request.
[0103] In this embodiment, before obtaining the client's access request to the database in the transaction initiated by the client, it also includes: in response to the client's transaction initiation request, obtaining a preset data source instance object from a preset connection component; and determining a preset connection channel represented by the preset data source instance object.
[0104] Specifically, the client first initiates a transaction, and after confirming the transaction, it processes the request in the transaction. When the request in the transaction needs to be processed, the target connection channel is dynamically switched and created.
[0105] Before obtaining the client's access request to the database in the transaction initiated by the client, first respond to the client's transaction initiation request, obtain the preset data source instance object from the preset connection component, and determine the preset connection channel represented by the preset data source instance object. That is, after responding to the transaction, it is not necessary to directly connect to the database based on the data source identifier at the transaction start stage, but based on the preset Dynamic Routing Connection, replace the database connection at the transaction start stage in the current related technology. When it is necessary to process the request in the transaction, the Connection is dynamically created.
[0106] That is to say, in this embodiment, the preset connection channel is determined first, and then S301 is executed.
[0107] The beneficial effect of such a setting is that, in the transaction start phase, a preset connection component is used for connection, which facilitates the subsequent switching of data sources and realizes dynamic management of data sources.
[0108] In this embodiment, after creating the target connection channel according to the preset data source instance object and target information, the method further includes: associating the pre-configured data source identifier with the target connection channel and storing them in the connection mapping relationship.
[0109] Specifically, the connection component stores the association relationship between the data source identifier and the connection channel. If the pre-configured data source identifier does not exist in the connection mapping relationship in the preset connection component, it is necessary to dynamically create a target connection channel, which is not stored in the connection mapping relationship with the corresponding data source identifier.
[0110] After creating the target connection channel, the pre-configured data source identifier can be associated with the target connection channel and stored in the connection mapping relationship. If the target connection channel is needed to perform database operations later, the target connection channel can be directly found from the connection mapping relationship without having to create it again.
[0111] The beneficial effect of such a setting is that the data source identifier is added to the connection mapping relationship together with the created target connection channel, which facilitates the subsequent direct acquisition of the connection channel and improves the processing efficiency of the request.
[0112] S304: Execute a request operation corresponding to the access request in the database according to the target connection channel.
[0113] Exemplarily, this step may refer to the above-mentioned step S103 and will not be described in detail.
[0114] This embodiment pre-configures a connection component so that after initiating a transaction, the client can connect to the database at any time according to the access request in the transaction, without having to connect at the transaction start stage. The data source identifier can be determined according to the access request in the transaction, and the connection channel corresponding to the data source identifier can be determined according to the connection mapping relationship in the preset connection component as the target connection channel, so that the client can connect to the database through the target connection channel. According to the target connection channel, the request operation corresponding to the access request is executed in the database. When executing another access request, other connection channels can be switched, so that the connection to the database can be switched during the processing of the access request, which improves the flexibility of the connection switching, can meet actual business needs, and thus improves the efficiency and accuracy of request processing.
[0115] Figure 4 A flowchart of a request processing method based on database connection provided in an embodiment of the present application. Figure 4 As shown, the method comprises the following steps:
[0116] S401: Obtain a client's access request to a database from a transaction initiated by the client, and determine a pre-configured data source identifier and a request operation corresponding to the access request according to the access request.
[0117] Exemplarily, a client of a database can issue an access request to the database when initiating a transaction. The client's access request to the database is obtained from the transaction initiated by the client. After responding to the access request, the pre-configured data source identifier can be obtained, and the request operation corresponding to the access request can be determined. Each data source corresponds to a data source identifier. The data source identifier can be pre-configured in the @DS annotation, and the string of the data source identifier can be obtained from the @DS annotation. The obtained string is pushed onto the stack, and the string is obtained from the stack according to the last-in-first-out principle to obtain the data source identifier.
[0118] Each access request corresponds to its own request operation, which represents the operation that the client wants to perform on the database and can be expressed in an SQL statement.
[0119] S402: Determine whether the pre-configured data source identifier is a preset identifier string. If not, execute S403; if yes, execute S406.
[0120] S403: Searching for a pre-configured data source identifier from a connection mapping relationship in a preset connection component.
[0121] Exemplarily, a string is pre-set as a preset identification string, for example, the preset identification string can be a null value. It is determined whether the pre-configured data source identifier is the preset identification string. If not, it is possible to search from the connection mapping relationship whether the pre-configured data source identifier exists. For example, if the pre-configured data source identifier is 001, it is possible to search from the connection mapping relationship whether the data source identifier of 001 exists.
[0122] S404: Determine whether the pre-configured data source identifier exists in the connection mapping relationship. If so, execute S405; if not, execute S407.
[0123] S405: Determine a connection channel corresponding to a pre-configured data source identifier as a target connection channel.
[0124] S406: Obtain a preset default identifier, and determine a connection channel corresponding to the preset default identifier as the target connection channel according to a connection mapping relationship in a preset connection component.
[0125] Exemplarily, if the preconfigured data source identifier is a preset identifier string, for example, the preconfigured data source identifier is a null value, then a preset default identifier is obtained. The default identifier is a default data source identifier, and when there is no pre-set data source identifier, the default identifier is obtained.
[0126] A connection channel is configured for the default identifier in advance, and the default identifier and the corresponding connection channel are associated and stored in the connection mapping relationship. After the default identifier is determined, the connection channel corresponding to the default identifier is determined from the connection mapping relationship in the preset connection component as the target connection channel. That is, when the data source identifier is not pre-configured, the default connection channel is used as the target connection channel.
[0127] S407: Acquire configuration information in a data source represented by a pre-configured data source identifier as target information, and create a target connection channel according to a preset data source instance object and target information.
[0128] Exemplarily, determine whether there is a pre-configured data source identifier in the connection mapping relationship in the preset connection component. If not, it is determined that the client has not yet connected to the database through the data source identified by the data source, and a connection needs to be created now. Different data sources correspond to different configuration information, and the configuration information can be used to create different connection channels. The configuration information in the data source represented by the pre-configured data source identifier can be obtained as the target information. Create a target connection channel based on the preset data source instance object and target information. The data source instance object can represent the form of the target connection channel, and based on the target information, a connection channel in the form of a data source instance object can be created as the target connection channel.
[0129] S408. Execute a request operation corresponding to the access request in the database according to the target connection channel.
[0130] Exemplarily, a target connection channel is determined, and based on the target connection channel, a connection between the client and the database is realized, so that a request operation corresponding to the access request is executed in the database, that is, a SQL operation is executed using the current Connection.
[0131] Figure 5 A schematic diagram of a request processing device based on database connection provided in an embodiment of the present application, the device is applied to an electronic device, the electronic device is configured with a preset connection component, the preset connection component includes a connection mapping relationship, the connection mapping relationship represents the association relationship between the data source identifier and the connection channel, the data source identifier represents the data source, and the data source includes the configuration information of the connection channel between the client and the database. Figure 5 As shown, the request processing device based on database connection includes: an identification determination module 501, a channel determination module 502 and a request execution module 503;
[0132] The identification determination module 501 is used to obtain the client's access request to the database from the transaction initiated by the client, and determine the pre-configured data source identification and the request operation corresponding to the access request according to the access request;
[0133] A channel determination module 502 is used to determine, according to the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier as a target connection channel;
[0134] The request execution module 503 is used to execute the request operation corresponding to the access request in the database according to the target connection channel.
[0135] In some embodiments, the channel determination module 502 includes:
[0136] an identification search unit, configured to search for the pre-configured data source identification from the connection mapping relationship in the preset connection component if the pre-configured data source identification is not a preset identification string; wherein the preset identification string indicates that the pre-configured data source identification is a null value;
[0137] The channel determination unit is configured to determine, if the pre-configured data source identifier exists in the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier as a target connection channel.
[0138] In some embodiments, it also includes:
[0139] An identification default module, used for obtaining a preset default identification if the pre-configured data source identification is a preset identification string;
[0140] The channel search module is used to determine the connection channel corresponding to the preset default identifier as the target connection channel according to the connection mapping relationship in the preset connection component.
[0141] In some embodiments, the preset connection component includes a preset data source instance object, and the preset data source instance object represents a preset connection channel; the device also includes:
[0142] An information acquisition module, configured to acquire configuration information in a data source represented by the pre-configured data source identifier as target information if the pre-configured data source identifier does not exist in the connection mapping relationship in the preset connection component;
[0143] The channel creation module is used to create the target connection channel according to the preset data source instance object and the target information.
[0144] In some embodiments, the preset connection component also includes an operation identifier of the client, and the operation identifier represents the operation behavior performed by the client on the preset connection channel represented by the preset data source instance object; the channel creation module includes:
[0145] An initial creation unit, configured to determine an initial connection channel according to the preset data source instance object and the target information;
[0146] The target creation unit is used to initialize the configuration of the initial connection channel according to the operation identifier to obtain the target connection channel.
[0147] In some embodiments, the target creation unit is specifically configured to:
[0148] Determine the operation behavior corresponding to the operation identifier;
[0149] The operation behavior corresponding to the operation identifier is executed on the initial connection channel to obtain the target connection channel.
[0150] In some embodiments, the preset connection component also includes preset channel attribute information, and the preset channel attribute information represents the preset value of the corresponding attribute in the connection channel; the initial creation unit is specifically used to:
[0151] Determine the connection channel to be assigned according to the preset data source instance object and the target information;
[0152] According to the preset channel attribute information, a value assignment operation is performed on the corresponding attribute of the connection channel to be assigned a value, so as to obtain the initial connection channel.
[0153] In some embodiments, it also includes:
[0154] An instance acquisition module, used for acquiring the preset data source instance object from a preset connection component in response to a transaction initiation request from the client before acquiring the client's access request to the database in a transaction initiated from the client;
[0155] The preset channel determination module is used to determine the preset connection channel represented by the preset data source instance object.
[0156] In some embodiments, it also includes:
[0157] The storage module is used to associate the pre-configured data source identifier with the target connection channel and store them in the connection mapping relationship after creating the target connection channel according to the preset data source instance object and the target information.
[0158] In some embodiments, a transaction initiated by a client includes multiple access requests; the apparatus further includes:
[0159] A channel traversal module, configured to traverse the connection channels in the connection mapping relationship if all access requests in the transaction are executed;
[0160] The channel disconnection module is used to perform a disconnection operation on each connection channel in the connection mapping relationship.
[0161] The request processing device based on database connection provided in the embodiment of the present application can be used to execute the technical solution of the request processing method based on database connection in the above embodiment. Its implementation principle and technical effect are similar and will not be repeated here.
[0162] It should be noted that it should be understood that the division of the various modules of the above device is only a division of logical functions. In actual implementation, they can be fully or partially integrated into one physical entity, or they can be physically separated. And these modules can all be implemented in the form of software called by processing elements; they can also be all implemented in the form of hardware; some modules can also be implemented in the form of software called by processing elements, and some modules can be implemented in the form of hardware. For example, the identification determination module 501 can be a separately established processing element, or it can be integrated in a chip of the above device. In addition, it can also be stored in the memory of the above device in the form of program code, and called and executed by a processing element of the above device. The function of the above identification determination module 501. The implementation of other modules is similar. In addition, these modules can be fully or partially integrated together, or they can be implemented independently. The processing element here can be an integrated circuit with signal processing capabilities. In the implementation process, each step of the above method or each module above can be completed by an integrated logic circuit of hardware in the processor element or instructions in the form of software.
[0163] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 6 As shown, the electronic device may include: a transceiver 61 , a processor 62 , and a memory 63 .
[0164] The processor 62 executes the computer execution instructions stored in the memory, so that the processor 62 executes the scheme in the above embodiment. The processor 62 can be a general-purpose processor, including a central processing unit CPU, a network processor (NP), etc.; it can also be a digital signal processor DSP, an application-specific integrated circuit ASIC, a field programmable gate array FPGA or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components.
[0165] The memory 63 is connected to the processor 62 via a system bus and completes communication between them. The memory 63 is used to store computer program instructions.
[0166] The transceiver 61 may be used to obtain a client's access request to a database from a transaction initiated by the client.
[0167] The system bus can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The transceiver is used to realize the communication between the database access device and other computers (such as clients, read-write libraries, and read-only libraries). The memory may include random access memory (RAM) and may also include non-volatile memory.
[0168] The electronic device provided in the embodiment of the present application may be the terminal device of the above embodiment.
[0169] An embodiment of the present application also provides a chip for executing instructions, which is used to execute the technical solution of the request processing method based on database connection in the above embodiment.
[0170] An embodiment of the present application also provides a computer-readable storage medium, in which computer instructions are stored. When the computer instructions are executed on a computer, the computer executes the technical solution of the request processing method based on database connection in the above embodiment.
[0171] An embodiment of the present application also provides a computer program product, which includes a computer program stored in a computer-readable storage medium. At least one processor can read the computer program from the computer-readable storage medium. When at least one processor executes the computer program, it can implement the technical solution of the request processing method based on database connection in the above embodiment.
[0172] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.
[0173] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to implement the solution of this embodiment.
[0174] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.
[0175] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the methods of various embodiments of the present application.
[0176] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the invention may be directly implemented as being executed by a hardware processor, or may be implemented by a combination of hardware and software modules in the processor.
[0177] The memory may include a high-speed RAM memory, and may also include a non-volatile storage NVM, such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.
[0178] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.
[0179] The above storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general or special purpose computer.
[0180] An exemplary storage medium is coupled to a 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 application specific integrated circuit (ASIC). Of course, the processor and the storage medium can also exist as discrete components in an electronic control unit or a main control device.
[0181] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.
[0182] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A request processing method based on database connection, characterized in that: The method is applied to an electronic device, wherein a preset connection component is configured in the electronic device, wherein the preset connection component includes a connection mapping relationship, wherein the connection mapping relationship represents an association relationship between a data source identifier and a connection channel, wherein the data source identifier represents a data source, and wherein the data source includes configuration information of a connection channel between a client and a database; The method comprises: Acquire an access request of the client to the database from a transaction initiated by the client, and determine, according to the access request, a pre-configured data source identifier and a request operation corresponding to the access request; According to the connection mapping relationship in the preset connection component, determine the connection channel corresponding to the pre-configured data source identifier as the target connection channel; According to the target connection channel, a request operation corresponding to the access request is executed in the database.
2. The method according to claim 1, characterized in that Determining, according to the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier as a target connection channel, including: If the pre-configured data source identifier is not a preset identifier string, searching the pre-configured data source identifier from the connection mapping relationship in the preset connection component; wherein the preset identifier string indicates that the pre-configured data source identifier is a null value; If the pre-configured data source identifier exists in the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier is determined as the target connection channel.
3. The method according to claim 2, characterized in that Also includes: If the pre-configured data source identifier is a preset identifier string, obtaining a preset default identifier; According to the connection mapping relationship in the preset connection component, a connection channel corresponding to the preset default identifier is determined as the target connection channel.
4. The method according to claim 2, characterized in that: The preset connection component includes a preset data source instance object, and the preset data source instance object represents a preset connection channel; the method further includes: If the pre-configured data source identifier does not exist in the connection mapping relationship in the preset connection component, then obtaining configuration information in the data source represented by the pre-configured data source identifier as the target information; The target connection channel is created according to the preset data source instance object and the target information.
5. The method according to claim 4, characterized in that The preset connection component also includes an operation identifier of the client, and the operation identifier represents the operation behavior performed by the client on the preset connection channel represented by the preset data source instance object; Creating the target connection channel according to the preset data source instance object and the target information includes: Determining an initial connection channel according to the preset data source instance object and the target information; The initial connection channel is initialized and configured according to the operation identifier to obtain the target connection channel.
6. The method according to claim 5, characterized in that Initializing the initial connection channel according to the operation identifier to obtain the target connection channel includes: Determine the operation behavior corresponding to the operation identifier; The operation behavior corresponding to the operation identifier is executed on the initial connection channel to obtain the target connection channel.
7. The method according to claim 6, characterized in that The preset connection component also includes preset channel attribute information, and the preset channel attribute information represents the preset value of the corresponding attribute in the connection channel; Determining an initial connection channel according to the preset data source instance object and the target information includes: Determine the connection channel to be assigned according to the preset data source instance object and the target information; According to the preset channel attribute information, a value assignment operation is performed on the corresponding attribute of the connection channel to be assigned a value, so as to obtain the initial connection channel.
8. The method according to claim 7, characterized in that Before obtaining the client's access request to the database from the transaction initiated by the client, the method further includes: In response to a transaction initiation request from a client, obtaining the preset data source instance object from a preset connection component; A preset connection channel represented by the preset data source instance object is determined.
9. The method according to any one of claims 4 to 8, characterized in that: After creating the target connection channel according to the preset data source instance object and the target information, the method further includes: The pre-configured data source identifier is associated with the target connection channel and stored in the connection mapping relationship.
10. The method according to claim 1, characterized in that The transaction initiated by the client includes multiple access requests; the method further includes: If all access requests in the transaction are executed, traversing the connection channels in the connection mapping relationship; A disconnect operation is performed on each connection channel in the connection mapping relationship.
11. A request processing device based on database connection, characterized in that: The device is applied to an electronic device, wherein the electronic device is configured with a preset connection component, wherein the preset connection component includes a connection mapping relationship, wherein the connection mapping relationship represents an association relationship between a data source identifier and a connection channel, wherein the data source identifier represents a data source, and wherein the data source includes configuration information of a connection channel between a client and a database; The device comprises: An identification determination module, used to obtain an access request of the client to the database from a transaction initiated by the client, and determine a pre-configured data source identification and a request operation corresponding to the access request according to the access request; A channel determination module, configured to determine, according to the connection mapping relationship in the preset connection component, a connection channel corresponding to the pre-configured data source identifier as a target connection channel; A request execution module is used to execute a request operation corresponding to the access request in the database according to the target connection channel.
12. An electronic device, characterized in that: include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the method according to any one of claims 1 to 10.
13. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 10 when executed by a processor.
14. A computer program product, characterized in that The invention comprises a computer program, which implements the method according to any one of claims 1 to 10 when being executed by a processor.
Citation Information
Patent Citations
Method and device for automatically switching data source, data source switching device
CN101295304A
A database switching method and terminal device
CN109325066A
Database access method, system and device and storage medium
CN111367983A
Method and system for accessing multiple data sources
CN111414391A
Extending types hosted in database to other platforms
US20110099166A1
