Database transaction submission method and device, storage medium and electronic equipment

By determining the target data source of the target transaction and establishing the corresponding database connection when submitting transactions from multiple data sources, the problem of data inconsistency and dirty data is solved, and efficient data processing and accurate database operations are achieved.

CN119322659APending Publication Date: 2025-01-17YGSOFT INC
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411338383.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-24
Publication Date
2025-01-17

Smart Images

  • Figure CN119322659A_ABST
    Figure CN119322659A_ABST
Patent Text Reader

Abstract

The invention discloses a database transaction submission method and device, a storage medium and electronic equipment, the method is applied to the field of database processing, and the method comprises the steps of determining a target data source of a target transaction under the condition that it is detected that the target transaction is started by a target thread; establishing a target database connection of the target transaction according to the target data source and the first data source; executing the structured query statement in the target transaction according to the target database connection, and determining an execution state of the target transaction; and under the condition that a submission instruction of the target transaction is detected, submitting the first transaction and the target transaction according to the transaction state of the first transaction. By means of the method and device, the problems that when multiple data sources submit the transaction, due to the fact that the database connections corresponding to the multiple data sources point to the same data source, data is inconsistent, the submission opportunity of the multiple database connections cannot be managed in a unified mode, and dirty data is generated when the transaction is submitted in the related technology are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of database technology, and in particular to a method, device, storage medium and electronic device for submitting a database transaction. Background Art

[0002] To facilitate the work of application developers, the enterprise application framework defines an abstraction layer on top of different transaction management application programming interfaces (APIs), so that application developers can use the transaction management mechanism without having to understand the underlying transaction management APIs. The transaction management mechanism defines the transaction management boundary through Aspect-Oriented Programming (AOP) technology. When a transaction starts, a database connection is borrowed from the connection pool. When a transaction ends, a commit or rollback operation is performed, and the connection is returned after the transaction is completed.

[0003] When an application needs to use multiple data sources, multiple transaction managers can be specified, and the transaction managers will directly or indirectly bind different data sources. During the AOP processing, the transaction manager will perform fixed transaction opening and submission operations before and after the execution of the real business method. This frequent opening and closing process will lead to poor performance of connection pool management. When transactions are nested, the connection held by the upper-level transaction can only be released after the lower-level transaction ends. When there are nested loops, this situation of occupied connections is very serious. In addition, due to the nesting of transactions, the submission order is uncertain when the transaction is submitted, resulting in the generation of dirty data, which reduces the accuracy of data processing.

[0004] When submitting transactions to multiple data sources in related technologies, the database connections corresponding to the multiple data sources point to the same data source, resulting in data inconsistency, and the inability to uniformly manage the submission timing of multiple database connections, resulting in the problem of dirty data when the transaction is submitted. No effective solution has been proposed yet. Summary of the invention

[0005] The main purpose of the present application is to provide a database transaction submission method, device, storage medium and electronic device to solve the problem in the related art that when multiple data sources submit transactions, the database connections corresponding to the multiple data sources point to the same data source, resulting in data inconsistency, and the submission timing of multiple database connections cannot be uniformly managed, resulting in dirty data when the transaction is submitted.

[0006] To achieve the above-mentioned purpose, according to one aspect of the present application, a method for submitting a database transaction is provided, the method comprising: determining a target data source of the target transaction when it is detected that a target thread starts a target transaction; establishing a target database connection of the target transaction based on the target data source and a first data source, wherein the first data source is a data source of a first transaction and the target transaction needs to be executed during the execution of the first transaction; executing a structured query statement in the target transaction based on the target database connection and determining an execution status of the target transaction; and submitting the first transaction and the target transaction based on the transaction status of the first transaction when a commit instruction of the target transaction is detected.

[0007] Further, establishing a target database connection for the target transaction based on the target data source and the first data source includes: when the target data source and the second data source are the same, determining that the target transaction reuses the first database connection of the first transaction, and using the first database connection as the target database connection, wherein the second data source is any data source that has established a database connection; and when the target data source and the first data source are different, creating a target database connection for the target transaction.

[0008] Furthermore, before determining that the target transaction reuses the first database connection of the first transaction, the method also includes: determining whether first identification information is recorded in the target thread, wherein the first identification information is identification information of the first data source; if the target thread records the first identification information and the first identification information is the same as the target identification information, determining that the target data source is the same as the first data source, wherein the target identification information is identification information of the target data source; if the target thread does not record the first identification information, querying in the cache according to the target identification information to obtain a query result, wherein the target identification information is identification information of the target data source; if the query result is not empty, determining the target database connection according to the query result.

[0009] Further, submitting the first transaction and the target transaction according to the transaction status of the first transaction includes: judging whether the transaction status of the first transaction belongs to the target status, wherein the target status refers to the status of waiting for the transaction to be submitted after the structured query statement in the transaction is executed; if the transaction status of the first transaction does not belong to the target status, not submitting the target transaction; if the transaction status of the first transaction belongs to the target status, submitting the first transaction and the target transaction.

[0010] Furthermore, before detecting that the target thread starts a target transaction, it includes: defining a virtual database connection object to obtain a first object; establishing a virtual database connection according to the target data source through the first object to obtain a first database connection; and recording the association relationship between the identification information of the target data source and the first database connection through the thread variable of the target thread.

[0011] Furthermore, after establishing a virtual database connection according to the target data source through the first object to obtain a first database connection, the method also includes: determining a target operation that the target transaction needs to perform on the data in the target data source; when the target operation belongs to a preset operation, instantiating the first database connection into a real database connection to obtain a second database connection; updating the thread variable in the target thread according to the second database connection, and storing the second database connection in a cache.

[0012] Furthermore, after the first transaction and the target transaction are committed according to the transaction status of the first transaction, the method further includes: deleting a target database connection corresponding to the target transaction in a cache; and deleting a database connection corresponding to the first transaction in a cache.

[0013] To achieve the above-mentioned purpose, according to another aspect of the present application, a database transaction submission device is provided, the device comprising: a first determination unit, for determining a target data source of the target transaction when it is detected that a target thread starts a target transaction; a first establishment unit, for establishing a target database connection of the target transaction based on the target data source and a first data source, wherein the first data source is a data source of a first transaction, and the target transaction needs to be executed during the execution of the first transaction; a second determination unit, for executing a structured query statement in the target transaction based on the target database connection, and determining the execution status of the target transaction; and a submission unit, for submitting the first transaction and the target transaction according to the transaction status of the first transaction when a submission instruction of the target transaction is detected.

[0014] Furthermore, the first establishing unit includes: a first determining subunit, used to determine that the target transaction reuses the first database connection of the first transaction and uses the first database connection as the target database connection when the target data source and the second data source are the same, wherein the second data source is any data source for which a database connection has been established; and a creating subunit, used to create a target database connection for the target transaction when the target data source and the first data source are different.

[0015] Further, the establishment unit also includes: a first judgment subunit, used to judge whether the first identification information is recorded in the target thread before determining that the target transaction reuses the first database connection of the first transaction, wherein the first identification information is identification information of the first data source; a second determination subunit, used to determine that the target data source is the same as the first data source when the target thread records the first identification information and the first identification information is the same as the target identification information, wherein the target identification information is identification information of the target data source; a query subunit, used to query in the cache according to the target identification information to obtain a query result when the target thread does not record the first identification information, wherein the target identification information is identification information of the target data source; a third determination subunit, used to determine the target database connection according to the query result when the query result is not empty.

[0016] Further, the submission unit includes: a second judgment subunit, used to judge whether the transaction status of the first transaction belongs to a target status, wherein the target status refers to a status of waiting for the transaction to be submitted after the structured query statement in the transaction is executed; a first processing subunit, used to not submit the target transaction if the transaction status of the first transaction does not belong to the target status; and a second processing subunit, used to perform submission processing on the first transaction and the target transaction if the transaction status of the first transaction belongs to the target status.

[0017] Furthermore, the device includes: a definition unit, used to define a virtual database connection object to obtain a first object before detecting that the target thread starts a target transaction; a second establishment unit, used to establish a virtual database connection according to the target data source through the first object to obtain a first database connection; and a recording unit, used to record the association relationship between the identification information of the target data source and the first database connection through the thread variable of the target thread.

[0018] Furthermore, the device also includes: a third determination unit, which is used to establish a virtual database connection according to the target data source through the first object to obtain a first database connection, and then determine the target operation that the target transaction needs to perform on the data in the target data source; a processing unit, which is used to instantiate the first database connection into a real database connection to obtain a second database connection when the target operation belongs to a preset operation; and an updating unit, which is used to update the thread variables in the target thread according to the second database connection, and store the second database connection in a cache.

[0019] Furthermore, the device also includes: a first deleting unit, used to delete the target database connection corresponding to the target transaction in the cache after the first transaction and the target transaction are committed according to the transaction status of the first transaction; and a second deleting unit, used to delete the database connection corresponding to the first transaction in the cache.

[0020] In order to achieve the above-mentioned purpose, according to one aspect of the present application, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, it implements any one of the above-mentioned methods for submitting database transactions, and when the computer program is executed by the processor, it implements the steps of the method for submitting database transactions described in each embodiment of the present application.

[0021] In order to achieve the above-mentioned purpose, according to one aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium includes stored computer instructions, wherein when the computer instructions are executed by a processor, any one of the above-mentioned database transaction submission methods is implemented.

[0022] In order to achieve the above-mentioned purpose, according to one aspect of the present application, an electronic device is provided, including one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by one or more processors, the one or more processors implement any one of the above-mentioned database transaction submission methods.

[0023] Through this application, the following steps are adopted: when it is detected that the target thread starts the target transaction, the target data source of the target transaction is determined; the target database connection of the target transaction is established according to the target data source and the first data source, wherein the first data source is the data source of the first transaction, and the target transaction needs to be executed during the execution of the first transaction; the structured query statement in the target transaction is executed according to the target database connection, and the execution status of the target transaction is determined; when the commit instruction of the target transaction is detected, the first transaction and the target transaction are committed according to the transaction status of the first transaction, which solves the problem that when multiple data sources commit transactions in the related technology, the database connections corresponding to the multiple data sources point to the same data source, resulting in inconsistent data, and the submission timing of multiple database connections cannot be uniformly managed, resulting in dirty data when the transaction is committed. Through the data source of each transaction in the nested database transaction, the database connection can be established according to the data source, thereby achieving the effect of reducing the database connection, increasing the memory occupied by the database connection, and improving the data processing efficiency. At the same time, by controlling the submission operation of other transactions based on the execution status of the root transaction in the nested transaction, the generation of dirty data can be avoided, the accuracy of database processing data is improved, and the work efficiency of the staff is further improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] The drawings constituting a part of the present application are used to provide a further understanding of the present application. The illustrative embodiments and descriptions of the present application are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0025] Figure 1 is a flowchart of a method for submitting a database transaction provided in accordance with Embodiment 1 of the present application;

[0026] Figure 2 This is a schematic diagram of an optional database transaction submission method provided in Example 1 of the present application. Figure 1 ;

[0027] Figure 3 This is a schematic diagram of an optional database transaction submission method provided in Example 1 of the present application. Figure 2 ;

[0028] Figure 4 It is a schematic diagram of a database transaction submission device provided according to Embodiment 2 of the present application;

[0029] Figure 5 This is a schematic diagram of an electronic device for submitting database transactions provided according to Embodiment 5 of the present application. DETAILED DESCRIPTION

[0030] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0031] It should be noted that the user information (including but not limited to user device information, user personal information, collected data, used data, generated data, processed data, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, collected information, used information, generated information, processed information, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, storage, use, processing, transmission, provision, disclosure and application of relevant data are in compliance with the relevant laws, regulations and standards of relevant countries and regions, necessary confidentiality measures are taken, and public order and good morals are not violated, and corresponding operation portals are provided for users to choose to authorize or refuse. For example, an interface is set between this system and relevant users or organizations. Before obtaining relevant information, it is necessary to send an acquisition request to the aforementioned user or organization through the interface, and obtain relevant information after receiving the consent information fed back by the aforementioned user or organization.

[0032] It should be noted that this application provides users with corresponding operation entrances for them to choose to agree or reject the automated decision-making results; if the user chooses to reject, the expert decision-making process will be entered.

[0033] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0034] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present application described here. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0035] Embodiment 1

[0036] The present invention is described below in conjunction with preferred implementation steps. Figure 1 is a flowchart of a method for submitting a database transaction according to the first embodiment of the present application, such as Figure 1 As shown, the method comprises the following steps:

[0037] Step S101 : when it is detected that a target thread starts a target transaction, a target data source of the target transaction is determined.

[0038] In the prior art, different threads may execute different database transactions. Concurrent execution of these transactions can improve the processing capability of the database and the response speed of the system. In the first embodiment, in order to reduce the occupation of database connections, it is necessary to determine the target data source of the target transaction after the target thread opens the target transaction, so as to process the database connection of the target transaction according to the data source.

[0039] Step S102: establishing a target database connection of a target transaction according to a target data source and a first data source, wherein the first data source is a data source of a first transaction, and the target transaction needs to be executed during the execution of the first transaction.

[0040] In the first embodiment, the target transaction is a transaction nested in the first transaction, that is, the target transaction needs to be executed during the execution of the first transaction, wherein the data source of the target transaction may or may not be the same as the data source of the first transaction. In order to reduce the problem of occupying too many database connections due to too many nested transactions, it is necessary to establish a database connection according to the data source of each database transaction.

[0041] It should be noted that the data source of each database transaction in the first embodiment may be the same database or different databases, or different types of databases, such as relational databases, non-relational databases, or other types of data storage systems.

[0042] Step S103, executing the structured query statement in the target transaction according to the target database connection, and determining the execution status of the target transaction.

[0043] In the first embodiment of the present invention, in order to avoid the generation of dirty data caused by nested database transactions, it is necessary to execute multiple nested database transactions, namely the first transaction and the target transaction mentioned above, after establishing the database connection, until the target transaction and the first transaction are committed. It should be noted that the first transaction mentioned above refers to the root transaction, and the root transaction refers to the starting transaction of other subtransactions in the nested transaction. Multiple subtransactions can be nested in the root transaction. In distributed transactions and complex transaction processing, the root transaction is responsible for coordinating and managing multiple subtransactions to ensure that they are executed under the premise of satisfying the ACID (atomicity, consistency, isolation, and durability) properties of the transaction.

[0044] Step S104: when a commit instruction of the target transaction is detected, commit the first transaction and the target transaction according to the transaction status of the first transaction.

[0045] In the first embodiment, in order to avoid the generation of dirty data caused by nested database transactions and to solve the problem in the prior art that database connections corresponding to multiple transactions lack unified management when transactions are committed, it is necessary to perform a commit operation on each other transaction according to the execution status of the root transaction involved in the nested transaction when a commit instruction of the target transaction is detected.

[0046] In summary, the method for submitting a database transaction provided in the first embodiment of the present application determines the target data source of the target transaction when detecting that the target thread has opened the target transaction; establishes the target database connection of the target transaction according to the target data source and the first data source, wherein the first data source is the data source of the first transaction, and the target transaction needs to be executed during the execution of the first transaction; executes the structured query statement in the target transaction according to the target database connection, and determines the execution status of the target transaction; submits the first transaction and the target transaction according to the transaction status of the first transaction when the commit instruction of the target transaction is detected, solves the problem that when multiple data sources submit transactions in the related technology, the database connections corresponding to the multiple data sources point to the same data source, resulting in inconsistent data, and the submission timing of multiple database connections cannot be uniformly managed, resulting in dirty data when the transaction is submitted. Through the data source of each transaction in the nested database transaction, a database connection can be established according to the data source, thereby achieving the effect of reducing the database connection, increasing the memory occupied by the database connection, and improving the data processing efficiency. At the same time, by controlling the submission operation of other transactions based on the execution status of the root transaction in the nested transaction, the generation of dirty data can be avoided, the accuracy of the database processing data is improved, and the work efficiency of the staff is further improved.

[0047] Optionally, in the database transaction submission method provided in Example 1 of the present application, a target database connection for the target transaction is established based on the target data source and the first data source, including: when the target data source and the second data source are the same, determining that the target transaction reuses the first database connection of the first transaction, and using the first database connection as the target database connection, wherein the second data source is any data source for which a database connection has been established; when the target data source and the first data source are different, creating a target database connection for the target transaction.

[0048] In the first embodiment, in order to reduce the use of database connections, database transactions of the same data source may reuse database connections, thereby reducing the occupancy of database connections.

[0049] In an optional embodiment, the schematic diagram of establishing a database connection can be as follows: Figure 2 As shown, the root transaction 1 refers to the database transaction created first, that is, the first transaction mentioned above, and the data source of the first transaction is as follows Figure 2 As shown in the order library in; Subtransaction 2 refers to the subtransaction nested in the root transaction, and the data source of subtransaction 2 is as follows Figure 2 As shown in the goods library in; Subtransaction 3 refers to the subtransaction nested in the root transaction, and the data source of subtransaction 3 is as follows Figure 2As shown in the order library in. If the target transaction is subtransaction 3, it is determined that the target data source and the data source of the first transaction are both the order library, and subtransaction 3 can be commanded to reuse the database connection of root transaction 1, wherein the data source of the first transaction belongs to the second data source mentioned above, and the target identification information is the identification information of the target data source; if the target transaction is subtransaction 2, the target data source is the goods library, and the first data source is the order library, so that it is determined that the target data source and the first data source are different, and then the target database connection of the target transaction is created, that is, the database connection of subtransaction 2.

[0050] Optionally, in the database transaction submission method provided in Example 1 of the present application, before determining that the target transaction reuses the first database connection of the first transaction, the above method also includes: judging whether the first identification information is recorded in the target thread, wherein the first identification information is identification information of the first data source; when the target thread records the first identification information and the first identification information is the same as the target identification information, determining that the target data source is the same as the first data source, wherein the target identification information is the identification information of the target data source; when the target thread does not record the first identification information, querying in the cache according to the target identification information to obtain a query result; when the query result is not empty, determining the target database connection according to the query result.

[0051] In the first embodiment, in order to reuse database connections and reduce the occupancy of database connections, it is possible to determine whether the data sources of database transactions are the same based on the data source identification information of each data source, thereby controlling the number of database connections of the database transaction.

[0052] In an optional embodiment, it is possible to determine whether the data source of the first transaction and the data source of the target transaction are the same based on whether the data source ID information of the first data source (i.e., the first identification information mentioned above) is recorded in the target thread that starts the target transaction. Furthermore, if the data source ID information of the first data source cannot be found from the data source identification information recorded by the target thread, it can also be found from the cache information.

[0053] Exemplarily, if the data source ID information of the first data source has been recorded in the target thread (i.e., the first identification information mentioned above), it is determined that the target process that starts the target transaction has created the first transaction, and when the data source ID information of the first data source and the data source ID information of the target data source (i.e., the target identification information mentioned above) are the same, it is determined that the target data source and the first data source are the same; if the target thread has not recorded the first identification information, it is determined that the target process that starts the target transaction and the process that starts the first transaction are different, so that the data source ID information of the target data source can be queried in the cache, and if the query result is empty, it is determined that the data source of the existing database transaction and the first data source of the target transaction are different, and if the query result is not empty, there is a database connection in the existing database connection that is the same as the first data source of the target transaction, so that it can be determined that the target transaction can reuse the connections of other database transactions.

[0054] Optionally, in the database transaction submission method provided in Example 1 of the present application, submitting the first transaction and the target transaction according to the transaction status of the first transaction includes: judging whether the transaction status of the first transaction belongs to the target status, wherein the target status refers to the status of waiting for the transaction to be submitted after the structured query statement in the transaction is executed; if the transaction status of the first transaction does not belong to the target status, not submitting the target transaction; if the transaction status of the first transaction belongs to the target status, submitting the first transaction and the target transaction.

[0055] In the first embodiment, in order to avoid the problem of dirty data generated by committing a nested transaction, it is possible to determine whether other transactions in the nested transaction are committed based on the commit status of the root transaction.

[0056] In an optional embodiment, the submission process of the nested transaction can be as follows: Figure 2 As shown, it includes root transaction 1, subtransaction 2 is nested in root transaction 1, subtransaction 3 is nested in subtransaction 2, and subtransaction 4 is nested in subtransaction 3. If subtransaction 2 (or subtransaction 3, subtransaction 4) completes the execution of the SQL statement (i.e., the structured query statement mentioned above) and is in the target state mentioned above, subtransaction 2 (or subtransaction 3, subtransaction 4) will not be submitted temporarily. Until root transaction 1 is in the target state, root transaction 1 (i.e., the first transaction mentioned above) is submitted, and all transactions nested in root transaction 1 (i.e., the target transaction mentioned above, for example, Figure 2 Subtransaction 2, subtransaction 3 and subtransaction 4).

[0057] Optionally, in the database transaction submission method provided in Example 1 of the present application, before detecting that the target thread starts the target transaction, it includes: defining a virtual database connection object to obtain a first object; establishing a virtual database connection according to the target data source through the first object to obtain a first database connection; and recording the association relationship between the identification information of the target data source and the first database connection through the thread variable of the target thread.

[0058] In the first embodiment, the virtual database connection is a middle-layer technology that allows an application to interact with a database through an intermediate agent. This connection can provide additional functions, such as transaction management, connection pool, query cache, etc., thereby improving the performance and scalability of the application. In order to improve the efficiency of database processing, before the target thread opens the target transaction, a virtual database connection object DC (dummy connection, i.e., the above-mentioned first object) can be defined to implement a standard database connection interface and a built-in real database connection RC (Real connection), and a virtual database connection of the target transaction is created according to the first object to obtain the above-mentioned first database connection, and the database connection is stored and accessed through the Map object TC (thread connections) of the virtual database connection, and the relationship between the data source ID and the database connection is recorded. In TC, the Map object is used to store and manage database connections. Exemplarily, the key of the Map object can be a thread ID or other identifier of the thread, and the value is the created virtual database connection.

[0059] By establishing a virtual database connection, an application can obtain connection resources without directly establishing an actual connection with the database, thereby reducing the number of database connections and improving application performance.

[0060] Optionally, in the database transaction submission method provided in Example 1 of the present application, after establishing a virtual database connection according to the target data source through the first object to obtain the first database connection, the above method also includes: determining the target operation that the target transaction needs to perform on the data in the target data source; when the target operation belongs to a preset operation, instantiating the first database connection into a real database connection to obtain a second database connection; updating the thread variables in the target thread according to the second database connection, and storing the second database connection in the cache.

[0061] In the first embodiment, a real database connection refers to a connection established directly with a database server, which allows an application to interact with a database in real time and perform operations such as query, update, and delete. Establishing a real database connection usually requires information such as database access rights, IP addresses, and port numbers. When determining that a target transaction includes data interaction with data in a target data source, it is necessary to instantiate the first database connection of the target transaction as a real database connection, and update the database connection information stored in the target thread and update it to the cache.

[0062] For example, the process of building a real database connection can be as follows Figure 2 As shown, it includes root transaction 1, subtransaction 2 is nested in root transaction 1, subtransaction 3 is nested in subtransaction 2, and subtransaction 4 is nested in subtransaction 3. Root transaction 1 needs to interact with the order library, so the virtual connection of root transaction 1 is instantiated as real connection 1; subtransaction 2 needs to interact with the product library, but the data source of subtransaction 2 is different from that of root transaction 1, so a new real connection 2 is created; subtransaction 3 needs to interact with the order library, so the virtual connection is instantiated as a real database connection, and because the data source of subtransaction 3 is the same as that of root transaction 1, the real connection 1 of root transaction 1 can be reused; subtransaction 4 needs to interact with the product library, and the data source of subtransaction 4 is the same as that of subtransaction 2, so the real connection 2 of subtransaction 2 can be reused.

[0063] Optionally, in the database transaction submission method provided in Example 1 of the present application, after submitting the first transaction and the target transaction according to the transaction status of the first transaction, the above method also includes: deleting the target database connection corresponding to the target transaction in the cache; deleting the database connection corresponding to the first transaction in the cache.

[0064] In the first embodiment, in order to reduce the occupation of database connections, the database connection of the transaction can be deleted after the transaction is committed. For example, after the first transaction is committed, the first database connection of the first transaction is deleted, and after the target transaction is committed, the target database connection of the target transaction is deleted.

[0065] Optionally, in the first embodiment, the process of processing transactions in this solution can be as follows: Figure 3As shown. First, the target transaction is opened through a thread. When establishing the connection of the target transaction, it is determined whether the data source of the target transaction is the same as that of other transactions. If the data source of the target transaction is the same as that of other transactions, the database connection of the transaction is reused. If the data source of the target transaction is different from that of other transactions, a virtual database connection of the target transaction is created. Further, if the target transaction needs to interact with data, the virtual database connection of the target transaction is instantiated as a real database connection. If the target transaction is not a transaction nested in other transactions, that is, the above-mentioned root transaction, the target transaction is set as the root transaction, and the database connection of the target transaction is stored in the cache. Then, when the target transaction completes the execution of the SQL statement and waits for submission, it is determined whether the target transaction belongs to the root transaction. If the target transaction belongs to the root transaction, the target transaction is submitted, as well as other transactions nested in the target transaction; if the target transaction does not belong to the root transaction, but is nested in the first transaction of the root transaction, the target transaction is not submitted temporarily, and the first transaction and the target transaction are submitted together after the first transaction is executed.

[0066] It should be noted that the steps shown in the flowcharts of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and that, although a logical order is shown in the flowcharts, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0067] Embodiment 2

[0068] The second embodiment of the present application also provides a database transaction submission device. It should be noted that the database transaction submission device of the second embodiment of the present application can be used to execute the database transaction submission method provided in the first embodiment of the present application. The following introduces the database transaction submission device provided in the second embodiment of the present application.

[0069] Figure 4 Schematic diagram of a database transaction submission device according to Embodiment 2 of the present application. Figure 4 As shown, the device includes: a first determining unit 401, a first establishing unit 402, a second determining unit 403 and a submitting unit 404.

[0070] Specifically, the first determining unit 401 is configured to determine a target data source of a target transaction when it is detected that a target thread starts a target transaction.

[0071] The first establishing unit 402 is used to establish a target database connection for a target transaction according to a target data source and a first data source, wherein the first data source is a data source of a first transaction and the target transaction needs to be executed during the execution of the first transaction.

[0072] The second determining unit 403 is used to execute the structured query statement in the target transaction according to the target database connection and determine the execution status of the target transaction.

[0073] The commit unit 404 is configured to commit the first transaction and the target transaction according to the transaction status of the first transaction when a commit instruction of the target transaction is detected.

[0074] The database transaction submission device provided in the second embodiment of the present application determines the target data source of the target transaction through the first determination unit 401 when detecting that the target thread starts the target transaction; the first establishment unit 402 establishes the target database connection of the target transaction according to the target data source and the first data source, wherein the first data source is the data source of the first transaction, and the target transaction needs to be executed during the execution of the first transaction; the second determination unit 403 executes the structured query statement in the target transaction according to the target database connection, and determines the execution status of the target transaction; the submission unit 404 submits the first transaction and the target transaction according to the transaction status of the first transaction when detecting the submission instruction of the target transaction, thereby solving the problem in the related art that when multiple data sources submit transactions, the database connections corresponding to the multiple data sources point to the same data source, causing data inconsistency, and the submission timing of the multiple database connections cannot be uniformly managed, resulting in dirty data when the transaction is submitted. Through the data source of each transaction in the nested database transaction, a database connection can be established according to the data source, thereby reducing the database connection, increasing the memory occupied by the database connection, and improving data processing efficiency. At the same time, by controlling the submission operations of other transactions based on the execution status of the root transaction in the nested transaction, the generation of dirty data can be avoided, thereby improving the accuracy of database data processing and further improving the work efficiency of staff.

[0075] Optionally, in the database transaction submission device provided in Example 2 of the present application, the above-mentioned first establishment unit 402 includes: a first determination subunit, used to determine that the target transaction reuses the first database connection of the first transaction and uses the first database connection as the target database connection when the target data source and the second data source are the same, wherein the second data source is any data source for which a database connection has been established; and a creation subunit, used to create the target database connection of the target transaction when the target data source and the first data source are different.

[0076] Optionally, in the database transaction submission device provided in Example 2 of the present application, the above-mentioned first establishment unit 402 also includes: a first judgment subunit, used to judge whether the first identification information is recorded in the target thread before determining that the target transaction reuses the first database connection of the first transaction, wherein the first identification information is identification information of the first data source; a second determination subunit, used to determine that the target data source is the same as the first data source when the target thread records the first identification information and the first identification information is the same as the target identification information, wherein the target identification information is identification information of the target data source; a query subunit, used to query in the cache according to the target identification information when the target thread does not record the first identification information to obtain a query result, wherein the target identification information is identification information of the target data source; a third determination subunit, used to determine the target database connection according to the query result when the query result is not empty.

[0077] Optionally, in the submission device for database transactions provided in Embodiment 2 of the present application, the submission unit 404 includes: a second judgment subunit, used to judge whether the transaction status of the first transaction belongs to the target status, wherein the target status refers to the status of waiting for the transaction to be submitted after the structured query statement in the transaction is executed; a first processing subunit, used to not submit the target transaction if the transaction status of the first transaction does not belong to the target status; and a second processing subunit, used to perform submission processing on the first transaction and the target transaction if the transaction status of the first transaction belongs to the target status.

[0078] Optionally, in the database transaction submission device provided in Example 2 of the present application, the above-mentioned device includes: a definition unit, used to define a virtual database connection object to obtain a first object before detecting that the target thread starts a target transaction; a second establishment unit, used to establish a virtual database connection according to the target data source through the first object to obtain a first database connection; and a recording unit, used to record the association relationship between the identification information of the target data source and the first database connection through the thread variable of the target thread.

[0079] Optionally, in the database transaction submission device provided in Example 2 of the present application, the above-mentioned device also includes: a third determination unit, which is used to establish a virtual database connection according to the target data source through the first object to obtain the first database connection, and then determine the target operation that the target transaction needs to perform on the data in the target data source; a processing unit, which is used to instantiate the first database connection into a real database connection to obtain a second database connection when the target operation belongs to a preset operation; and an update unit, which is used to update the thread variables in the target thread according to the second database connection, and store the second database connection in the cache.

[0080] Optionally, in the database transaction submission device provided in Example 2 of the present application, the above-mentioned device also includes: a first deletion unit, used to delete the target database connection corresponding to the target transaction in the cache after submitting the first transaction and the target transaction according to the transaction status of the first transaction; a second deletion unit, used to delete the database connection corresponding to the first transaction in the cache.

[0081] The submission device of the database transaction includes a processor and a memory. The above-mentioned first determination unit 401, first establishment unit 402, second determination unit 403 and submission unit 404 are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to realize corresponding functions.

[0082] The processor includes a kernel, which calls the corresponding program unit from the memory. The kernel can be set to one or more, and the accuracy of database transaction submission can be improved by adjusting the kernel parameters.

[0083] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.

[0084] Embodiment 3 of the present invention provides a computer-readable storage medium on which a program is stored. When the program is executed by a processor, a method for submitting a database transaction is implemented.

[0085] A fourth embodiment of the present invention provides a processor, which is used to run a program, wherein the program executes a method for submitting a database transaction when the program is running.

[0086] like Figure 5 As shown, embodiment 5 of the present invention provides an electronic device, the device includes a processor, a memory, and a program stored in the memory and executable on the processor, and the processor implements the following steps when executing the program: when it is detected that the target thread starts a target transaction, determining the target data source of the target transaction; establishing a target database connection of the target transaction based on the target data source and a first data source, wherein the first data source is the data source of the first transaction, and the target transaction needs to be executed during the execution of the first transaction; executing the structured query statement in the target transaction based on the target database connection, and determining the execution status of the target transaction; when a commit instruction of the target transaction is detected, committing the first transaction and the target transaction based on the transaction status of the first transaction.

[0087] When the processor executes the program, the following steps are also implemented: establishing a target database connection for the target transaction based on the target data source and the first data source, including: when the target data source and the second data source are the same, determining that the target transaction reuses the first database connection of the first transaction, and using the first database connection as the target database connection, wherein the second data source is any data source that has established a database connection; when the target data source and the first data source are different, creating a target database connection for the target transaction.

[0088] The processor also implements the following steps when executing the program: before determining that the target transaction reuses the first database connection of the first transaction, the above method also includes: judging whether the first identification information is recorded in the target thread, wherein the first identification information is identification information of the first data source; when the target thread records the first identification information and the first identification information is the same as the target identification information, determining that the target data source is the same as the first data source, wherein the target identification information is identification information of the target data source; when the target thread does not record the first identification information, querying in the cache according to the target identification information to obtain a query result, wherein the target identification information is identification information of the target data source; when the query result is not empty, determining the target database connection according to the query result.

[0089] When the processor executes the program, the following steps are also implemented: submitting the first transaction and the target transaction according to the transaction status of the first transaction, including: judging whether the transaction status of the first transaction belongs to the target status, wherein the target status refers to the status of waiting for the transaction to be submitted after the structured query statement in the transaction is executed; if the transaction status of the first transaction does not belong to the target status, not submitting the target transaction; if the transaction status of the first transaction belongs to the target status, submitting the first transaction and the target transaction.

[0090] The processor also implements the following steps when executing the program: before detecting that the target thread starts the target transaction, including: defining a virtual database connection object to obtain a first object; establishing a virtual database connection according to the target data source through the first object to obtain a first database connection; and recording the association relationship between the identification information of the target data source and the first database connection through the thread variable of the target thread.

[0091] When the processor executes the program, the following steps are also implemented: after establishing a virtual database connection according to the target data source through the first object to obtain the first database connection, the above method also includes: determining the target operation that the target transaction needs to perform on the data in the target data source; when the target operation belongs to a preset operation, instantiating the first database connection into a real database connection to obtain a second database connection; updating the thread variable in the target thread according to the second database connection, and storing the second database connection in the cache.

[0092] When the processor executes the program, the following steps are also implemented: after the first transaction and the target transaction are submitted according to the transaction status of the first transaction, the above method also includes: deleting the target database connection corresponding to the target transaction in the cache; deleting the database connection corresponding to the first transaction in the cache.

[0093] The devices in this article can be servers, PCs, PADs, mobile phones, etc.

[0094] The present application also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program that is initialized with the following method steps: determining a target data source of the target transaction when it is detected that a target thread starts a target transaction; establishing a target database connection of the target transaction based on the target data source and a first data source, wherein the first data source is a data source of a first transaction, and the target transaction needs to be executed during the execution of the first transaction; executing a structured query statement in the target transaction based on the target database connection, and determining the execution status of the target transaction; and committing the first transaction and the target transaction based on the transaction status of the first transaction when a commit instruction of the target transaction is detected.

[0095] When executed on a data processing device, the program is also suitable for executing initialization having the following method steps: establishing a target database connection for a target transaction based on a target data source and a first data source, including: when the target data source and the second data source are the same, determining that the target transaction reuses the first database connection of the first transaction, and using the first database connection as the target database connection, wherein the second data source is any data source for which a database connection has been established; and when the target data source and the first data source are different, creating a target database connection for the target transaction.

[0096] When executed on a data processing device, it is also suitable for executing a program that is initialized with the following method steps: before determining that the target transaction reuses the first database connection of the first transaction, the above method also includes: judging whether the first identification information is recorded in the target thread, wherein the first identification information is identification information of the first data source; when the target thread records the first identification information and the first identification information is the same as the target identification information, determining that the target data source is the same as the first data source, wherein the target identification information is identification information of the target data source; when the target thread does not record the first identification information, querying in the cache according to the target identification information to obtain a query result, wherein the target identification information is identification information of the target data source; when the query result is not empty, determining the target database connection according to the query result.

[0097] When executed on a data processing device, the program is also suitable for executing initialization having the following method steps: submitting the first transaction and the target transaction according to the transaction status of the first transaction, including: judging whether the transaction status of the first transaction belongs to the target status, wherein the target status refers to the status of waiting for the transaction to be submitted after the structured query statement in the transaction is executed; if the transaction status of the first transaction does not belong to the target status, the target transaction is not submitted; if the transaction status of the first transaction belongs to the target status, the first transaction and the target transaction are submitted.

[0098] When executed on a data processing device, the program is also suitable for executing initialization having the following method steps: before detecting that a target thread starts a target transaction, the program includes: defining a virtual database connection object to obtain a first object; establishing a virtual database connection according to a target data source through the first object to obtain a first database connection; and recording the association relationship between the identification information of the target data source and the first database connection through a thread variable of the target thread.

[0099] When executed on a data processing device, it is also suitable for executing a program that is initialized with the following method steps: after establishing a virtual database connection according to a target data source through a first object to obtain a first database connection, the above method also includes: determining a target operation that the target transaction needs to perform on data in the target data source; when the target operation belongs to a preset operation, instantiating the first database connection into a real database connection to obtain a second database connection; updating a thread variable in a target thread according to the second database connection, and storing the second database connection in a cache.

[0100] When executed on a data processing device, it is also suitable for executing a program that is initialized with the following method steps: after committing the first transaction and the target transaction according to the transaction status of the first transaction, the above method also includes: deleting the target database connection corresponding to the target transaction in the cache; deleting the database connection corresponding to the first transaction in the cache.

[0101] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0102] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0103] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0104] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0105] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0106] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0107] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0108] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0109] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0110] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.

Claims

1. A method for submitting a database transaction, characterized in that: include: In the case of detecting that the target thread starts a target transaction, determining a target data source of the target transaction; Establishing a target database connection for the target transaction according to the target data source and a first data source, wherein the first data source is a data source of a first transaction, and the target transaction needs to be executed during the execution of the first transaction; Executing the structured query statement in the target transaction according to the target database connection, and determining the execution status of the target transaction; In a case where a commit instruction of the target transaction is detected, the first transaction and the target transaction are committed according to the transaction status of the first transaction.

2. The method according to claim 1, characterized in that Establishing a target database connection for the target transaction according to the target data source and the first data source includes: In the case where the target data source and the second data source are the same, determining that the target transaction reuses the first database connection of the first transaction, and using the first database connection as the target database connection, wherein the second data source is any data source that has established a database connection; In a case where the target data source is different from the first data source, a target database connection for the target transaction is created.

3. The method according to claim 2, characterized in that Before determining that the target transaction reuses the first database connection of the first transaction, the method further includes: Determining whether first identification information is recorded in the target thread, wherein the first identification information is identification information of the first data source; When the target thread records the first identification information and the first identification information is the same as the target identification information, determining that the target data source is the same as the first data source, wherein the target identification information is identification information of the target data source; In the case that the target thread does not record the first identification information, querying in the cache according to the target identification information to obtain a query result, wherein the target identification information is identification information of the target data source; When the query result is not empty, the target database connection is determined according to the query result.

4. The method according to claim 1, characterized in that: Submitting the first transaction and the target transaction according to the transaction status of the first transaction, including: Determining whether the transaction state of the first transaction belongs to a target state, wherein the target state refers to a state in which the structured query statement in the transaction is executed and the transaction is waiting to be submitted; If the transaction state of the first transaction does not belong to the target state, the target transaction is not submitted; When the transaction state of the first transaction belongs to the target state, a commit process is performed on the first transaction and the target transaction.

5. The method according to claim 1, characterized in that Before detecting that the target thread has started a target transaction, including: Define a virtual database connection object and obtain a first object; Establishing a virtual database connection according to the target data source through the first object to obtain a first database connection; The association relationship between the identification information of the target data source and the first database connection is recorded through the thread variable of the target thread.

6. The method according to claim 5, characterized in that After establishing a virtual database connection according to the target data source through the first object to obtain a first database connection, the method further includes: Determine a target operation that the target transaction needs to perform on the data in the target data source; In the case where the target operation is a preset operation, instantiating the first database connection into a real database connection to obtain a second database connection; The thread variable in the target thread is updated according to the second database connection, and the second database connection is stored in a cache.

7. The method according to claim 1, characterized in that After committing the first transaction and the target transaction according to the transaction status of the first transaction, the method further includes: Deleting the target database connection corresponding to the target transaction in the cache; The database connection corresponding to the first transaction is deleted from the cache.

8. A database transaction submission device, characterized in that: include: A first determining unit, configured to determine a target data source of the target transaction when detecting that a target thread has started a target transaction; A first establishing unit, configured to establish a target database connection for the target transaction according to the target data source and a first data source, wherein the first data source is a data source of a first transaction, and the target transaction needs to be executed during the execution of the first transaction; A second determining unit, configured to execute the structured query statement in the target transaction according to the target database connection, and determine the execution state of the target transaction; A commit unit is configured to commit the first transaction and the target transaction according to the transaction status of the first transaction when a commit instruction of the target transaction is detected.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes stored computer instructions, wherein when the computer instructions are executed by a processor, the method for submitting a database transaction as described in any one of claims 1 to 7 is implemented.

10. An electronic device, characterized in that: The invention comprises one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the database transaction submission method described in any one of claims 1 to 7.