A database operation method, device, equipment, storage medium and product

By generating proxy data sources and dynamic proxy, the data source is dynamically determined according to the type of database operation request, which solves the problem of database read and write separation in the prior art that requires intrusion into code, and realizes efficient database operation separation and reduces modification costs.

CN118503319BActive Publication Date: 2024-11-08FLYING FOX INFORMATION TECH TIANJIN CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410964441.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-18
Publication Date
2024-11-08
Estimated Expiration
2044-07-18

AI Technical Summary

Technical Problem

The prior art requires intrusion into the original code when implementing database read and write separation, resulting in high modification costs in complex services and it is difficult to achieve efficient database operation separation.

Method used

By generating proxy data sources and dynamic proxy, dynamically determine the master or slave database data source based on the statements, transaction opening situation and data modification tags in the database operation request, dynamically determine the master or slave database data source, obtain and use the corresponding database link, execute database operation requests, and realize the database separation of read and write.

Benefits of technology

Without modifying the original code, accurately judge the database operation request type, determine the data source, and realize the read and write separation of database operations, reducing the modification cost and operation complexity.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118503319B_ABST
    Figure CN118503319B_ABST
Patent Text Reader

Abstract

The present application provides a database operation method, device, equipment, storage medium and product, the method comprising: generating a proxy data source and a dynamic proxy for obtaining a database link; in response to receiving a database operation request, executing the dynamic proxy so that the dynamic proxy performs the following steps: obtaining the database operation statement carried in the database operation request, the transaction opening status of the database operation request and the data modification mark in the current thread, determining the proxy data source as the master database data source or the slave database data source according to the database operation statement, the transaction opening status and the data modification mark, obtaining the database link corresponding to the proxy data source; using the master database or the slave database associated with the database link to execute the database operation request. The method can accurately determine whether the current database operation request is a request to modify data or a request to read data, and realize the read-write separation of database operations without modifying the original code logic.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a database operation method, device, electronic device, computer-readable storage medium, and computer program product. Background Art

[0002] Generally, software systems can use databases for data storage. In actual application scenarios, since the demand for reading data is greater than the demand for modifying data, the industry usually divides databases into master databases and slave databases. The master database is used to modify data, and the slave database is used to read data, thus realizing the read-write separation of the database.

[0003] In specific implementation, the industry usually modifies the original code so that the code used to modify data selects the master database, and the code used to read data selects the slave database. However, the above method requires intrusion into the original code, and the modification cost is high for complex businesses. Summary of the invention

[0004] The present application provides a database operation method, which can realize read-write separation of database operation. The present application also provides a database operation device, an electronic device, a computer-readable storage medium, and a computer program product.

[0005] In a first aspect, the present application provides a database operation method. The method comprises:

[0006] Generate proxy data sources and dynamic proxies for obtaining database links;

[0007] In response to receiving a database operation request, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtaining a database link corresponding to the proxy data source; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether a data modification operation has been performed in the current thread;

[0008] The database operation request is executed using the master database or slave database associated with the database link.

[0009] In some possible implementations, determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark includes:

[0010] When the transaction opening condition indicates that the database operation request does not open a non-read-only transaction, the type of the database operation statement is a query statement and does not include a key statement, and the data modification mark indicates that no data modification operation has been performed in the current thread, the proxy data source is determined as a slave data source;

[0011] Otherwise, the proxy data source is determined as the main database data source.

[0012] In some possible implementations, the method further includes:

[0013] According to the database operation statement, the data modification mark is updated.

[0014] In some possible implementations, the method further includes:

[0015] determining a synchronization delay between the master database and the slave database;

[0016] When the synchronization delay is greater than a set threshold, the proxy data source is determined as the main database data source.

[0017] In some possible implementations, the dynamic proxy further includes a database link cache, and obtaining a database link corresponding to the proxy data source includes:

[0018] Obtaining a database link corresponding to the proxy data source from the database link cache; or,

[0019] Execute a database link acquisition function to acquire a database link corresponding to the proxy data source.

[0020] In some possible implementations, the dynamic proxy further includes at least one public function, and the method further includes:

[0021] In response to receiving an operation request related to the public function, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining an existing database link in the database link cache, and executing the operation request related to the public function using the existing database link.

[0022] In a second aspect, the present application provides a database operation device. The device comprises:

[0023] A generation module for generating a proxy data source and a dynamic proxy for obtaining a database link;

[0024] A first execution module is used to execute the dynamic proxy in response to receiving a database operation request, so that the dynamic proxy executes the following steps: obtaining a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtaining a database link corresponding to the proxy data source; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether a data modification operation has been performed in the current thread;

[0025] The second execution module is used to execute the database operation request by using the master database or the slave database associated with the database link.

[0026] In some possible implementations, the first execution module is specifically configured to:

[0027] When the transaction opening condition indicates that the database operation request does not open a non-read-only transaction, the type of the database operation statement is a query statement and does not include a key statement, and the data modification mark indicates that no data modification operation has been performed in the current thread, the proxy data source is determined as a slave data source;

[0028] Otherwise, the proxy data source is determined as the main database data source.

[0029] In some possible implementations, the apparatus further includes an updating module, wherein the updating module is configured to:

[0030] According to the database operation statement, the data modification mark is updated.

[0031] In some possible implementations, the first execution module is further configured to:

[0032] determining a synchronization delay between the master database and the slave database;

[0033] When the synchronization delay is greater than a set threshold, the proxy data source is determined as the main database data source.

[0034] In some possible implementations, the dynamic proxy further includes a database link cache, and the first execution module is specifically configured to:

[0035] Obtaining a database link corresponding to the proxy data source from the database link cache; or,

[0036] Execute a database link acquisition function to acquire a database link corresponding to the proxy data source.

[0037] In some possible implementations, the dynamic proxy further includes at least one public function, and the first execution module is further configured to:

[0038] In response to receiving an operation request related to the public function, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining an existing database link in the database link cache, and executing the operation request related to the public function using the existing database link.

[0039] In a third aspect, the present application provides an electronic device, wherein the electronic device comprises a processor and a memory, wherein the memory stores instructions, and the processor executes the instructions so that the electronic device platform executes the method as described in the first aspect of the present application or any implementation of the first aspect.

[0040] In a fourth aspect, the present application provides a computer-readable storage medium. The computer-readable storage medium stores instructions, which, when executed on an electronic device, enable the electronic device to execute the method described in the first aspect or any one of the implementations of the first aspect.

[0041] In a fifth aspect, the present application provides a computer program product. The computer program product includes computer-readable instructions, and when the computer program product is executed on an electronic device, the electronic device executes the method described in the first aspect or any one of the implementations of the first aspect.

[0042] Based on the implementations provided in the above aspects, this application can also be further combined to provide more implementations.

[0043] Based on the above description, it can be seen that the technical solution of this application has the following beneficial effects:

[0044] Specifically, a proxy data source and a dynamic proxy for obtaining a database link are first generated, and in response to receiving a database operation request, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, and determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtaining a database link corresponding to the proxy data source; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether a data modification operation has been performed in the current thread. Then, the database operation request is executed using the master database or the slave database associated with the database link.

[0045] In this method, a dynamic proxy is generated for obtaining a database link. Since the database operation statement carried in the database operation request can be obtained when the dynamic proxy is executed, it is possible to accurately judge whether the current database operation request is a request to modify data or a request to read data, in combination with the specific database operation statement, transaction opening status, and data modification mark, and then determine whether the proxy data source is the main database data source or the slave database data source. In this way, by executing the dynamic proxy, without modifying the original code logic, the database link can be obtained by using the main database data source or the slave database data source, and the database operation can be executed in the main database or the slave database based on the difference in the database operation request, so as to realize the read-write separation of the database operation. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] The above and other features, advantages and aspects of the embodiments of the present application will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.

[0047] Figure 1 A flowchart of a database operation method provided in an embodiment of the present application;

[0048] Figure 2 A flowchart of another database operation method provided in an embodiment of the present application;

[0049] FIG. 3A to FIG. 3F A code schematic diagram of a method for implementing a database operation provided in an embodiment of the present application;

[0050] Figure 4 A schematic diagram of the structure of a database operation device provided in an embodiment of the present application;

[0051] Figure 5 A schematic diagram of the structure of an electronic device for implementing database operations provided in an embodiment of the present application. DETAILED DESCRIPTION

[0052] The embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be construed as being limited to the embodiments described herein. Instead, these embodiments are provided to provide a more thorough and complete understanding of the present application. It should be understood that the drawings and embodiments of the present application are only for exemplary purposes and are not intended to limit the scope of protection of the present application.

[0053] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.

[0054] It should be noted that the concepts such as "first" and "second" mentioned in this application are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.

[0055] It should be noted that the modifications of "one" and "plurality" mentioned in the present application are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".

[0056] In order to facilitate understanding of the technical solution of the present application, the specific technical terms and application scenarios in the present application are explained below.

[0057] Database: In this application, database can be understood as a relational database. A relational database refers to a database that uses a relational model to organize data and stores data in the form of rows and columns. Among them, the relational model can be understood as a two-dimensional table model. A relational database consists of two-dimensional tables and the relationships between them.

[0058] Master-slave replication: A database replication technology used to synchronize data between two or more database servers. In this architecture, one server acts as the "master" server, which is responsible for handling all modification operations (such as inserts, updates, and deletes). Other servers act as "slave" servers, which usually only handle read operations (such as queries) and receive copies of the data from the master server.

[0059] Data source: usually refers to a configured connection pool used to obtain and manage database links.

[0060] Database link: It can be understood as a network link initiated from the data source to the target database. After the database link is established, it can be used to perform read operations or write operations on the target database.

[0061] Dynamic proxy: used to enhance the methods of an object without modifying the original object. By creating a proxy object, additional functions can be added when performing operations on the original object without modifying the code of the original object. New functions can be added without changing the original code logic. In this application, the implementation of dynamic proxy can be JDK dynamic proxy.

[0062] For example, suppose there is a code A, and there is a function sumA in A. sumA requires two parameters a and b. The execution method of sumA in the original code is return a+b. Generate a dynamic proxy ProxyA. When the upstream program calls the sumA function of A, ProxyA will take over the execution of sumA function from A. ProxyA can have a different execution method from A. For example: if the caller executes the sumA function, execute sumA+1 and return to the caller. Otherwise, execute sumA and return to the caller. It does not affect the normal logic of the entire code.

[0063] Transaction: usually refers to an indivisible sequence of database operations, and is also the basic unit of database concurrency control. It can be understood as a logical set of operations that are either all executed or not executed.

[0064] Thread: It is the smallest unit that the operating system can schedule operations on. It is contained in a process and is the actual operation unit of the process. A thread usually refers to a single sequential control flow in a process. Multiple threads can run concurrently in a process, and each thread executes different tasks in parallel.

[0065] In actual business applications, there are situations where the demand for reading data is greater than the demand for modifying data. For example, when a user uses a system and wants to modify data, he needs to read the data first and then modify the data. In other words, even a simple data modification request will perform a data read operation. The above situation causes most of the pressure on the database to come from data read requests.

[0066] In order to ease the reading pressure of the database, the industry uses the master-slave replication method, which divides the database into a master database and a slave database, with the master database used as the database for modifying data and the slave database used as the database for reading data. In this way, the reading pressure is distributed to the slave database, improving the overall data reading and writing speed of the system.

[0067] In order to achieve read-write separation of the database, it is usually necessary to modify the original code so that the read-only business code uses the slave database and the code that modifies data uses the master database. The above method requires intrusion into the original code, which is very time-consuming and laborious to modify for complex business codes that have been running for a long time.

[0068] In view of this, the present application provides a database operation method. Specifically, firstly, a proxy data source and a dynamic proxy for obtaining a database link are generated, and in response to receiving a database operation request, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining the database operation statement carried in the database operation request, the transaction opening status of the database operation request, and the data modification mark in the current thread, and according to the database operation statement, the transaction opening status, and the data modification mark, the proxy data source is determined as the master database data source or the slave database data source, and the database link corresponding to the proxy data source is obtained; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether the data modification operation has been performed in the current thread. Then, the database operation request is executed using the master database or the slave database associated with the database link.

[0069] In this method, a dynamic proxy is generated for obtaining a database link. Since the database operation statement carried in the database operation request can be obtained when the dynamic proxy is executed, it is possible to accurately judge whether the current database operation request is a request to modify data or a request to read data, in combination with the specific database operation statement, transaction opening status, and data modification mark, and then determine whether the proxy data source is the main database data source or the slave database data source. In this way, by executing the dynamic proxy, without modifying the original code logic, the database link can be obtained by using the main database data source or the slave database data source, and the database operation can be executed in the main database or the slave database based on the difference in the database operation request, so as to realize the read-write separation of the database operation.

[0070] Next, the database operation method provided by the present application is described in detail with reference to the accompanying drawings.

[0071] See also Figure 1 The flowchart of a database operation method shown in FIG. 1 is a flowchart of a database operation method, and the method specifically includes the following steps:

[0072] S101: Generate a proxy data source and a dynamic proxy for obtaining a database link.

[0073] The proxy data source can be understood as a wrapper of the actual master data source or slave data source. In specific implementation, the proxy data source class can be generated, and the master data source and slave data source can be saved in the proxy data source class. At the same time, the proxy data source class also includes a database link acquisition function. In this way, the database link is created through the proxy data source without intrusion on the original code, master data source, and slave data source.

[0074] The dynamic proxy for obtaining a database link can be understood as a proxy for an interface for obtaining a database link in the original code. By generating a dynamic proxy, the dynamic proxy can be executed when a database link needs to be obtained.

[0075] S102: In response to receiving a database operation request, executing a dynamic proxy, so that the dynamic proxy performs the following steps: obtaining a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, determining a proxy data source as a primary database data source or a secondary database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtaining a database link corresponding to the proxy data source.

[0076] The database operation request can be understood as a request for operating a database, such as a data writing request, a data query request, etc. Since the database operation request needs to be executed in the database, it is necessary to first obtain a database link.

[0077] In the embodiment of the present application, the database link is obtained by executing the dynamic proxy. Specifically, the dynamic proxy first obtains the database operation statement carried in the database operation request, the transaction opening status of the database operation request and the data modification mark in the current thread.

[0078] The database operation statement can be understood as a statement for performing specific operations on the database, such as a structured query language (SQL) statement. The transaction opening status is used to indicate whether the database operation request opens a transaction. If a transaction is opened, database operations before and after the transaction is opened must be performed in the transaction. The data modification mark is used to indicate whether a data modification operation has been performed in the current thread.

[0079] In some possible implementations, the database operation statement can be obtained by parsing the database operation request (eg, the executed database operation function). The transaction opening status and the data modification mark can be obtained by querying the attribute value in the current thread.

[0080] Next, the dynamic proxy can determine whether the database operation request is related to writing data or reading data, and determine whether the proxy data source is the master database data source or the slave database data source. Specifically, when the transaction opening status indicates that the database operation request does not open a non-read-only transaction, the type of the database operation statement is a query statement and does not include a key statement, and the data modification mark indicates that no data modification operation has been performed in the current thread, the proxy data source can be determined as the slave database data source. Otherwise, the proxy data source is determined as the master database data source.

[0081] Among them, the key statement can be a database operation statement ending with for update. When the database operation request does not open a non-read-only transaction, the type of the database operation statement is a query statement and does not include a key statement, and the data modification mark indicates that no data modification operation has been performed in the current thread, it can be determined that the database operation request is a read-only request, and then the proxy data source is determined as the slave database data source, so that the slave database can be used to perform read-only operations later.

[0082] In some embodiments, the dynamic proxy can also update the data modification mark according to the database operation statement. For example, when the database operation statement is to add, modify or delete data, the data modification mark can be updated to the data modification operation performed in the current thread, so as to facilitate the determination of the proxy data source in subsequent database operations.

[0083] After determining that the proxy data source is the master data source or the slave data source, the dynamic proxy can obtain the database link corresponding to the proxy data source. For example, when the proxy data source is the master data source, the dynamic proxy can obtain the database link corresponding to the master data source; when the proxy data source is the slave data source, the dynamic proxy can obtain the database link corresponding to the slave data source. In this way, by executing the dynamic proxy, the database links corresponding to different data sources can be obtained.

[0084] In a specific implementation, the dynamic proxy may also include a database link cache, which can be used to cache the created database links. The dynamic proxy can obtain the database link corresponding to the proxy data source from the database link cache, or execute a database link acquisition function to obtain the database link corresponding to the proxy data source. In this way, when the database link corresponding to the proxy data source already exists in the database link cache, it is directly used to avoid wasting computing resources.

[0085] Furthermore, the dynamic proxy may also include at least one common function, such as a common function for closing a database link, a common function for committing a transaction, or a common function for rolling back a transaction. At this time, in response to receiving an operation request related to a common function, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining an existing database link in a database link cache, and using the existing database link to execute the operation request related to the common function.

[0086] Since public functions are functions that are independent of database operations but related to database links, when receiving an operation request related to a public function, the dynamic proxy does not need to determine whether the proxy data source is the primary database data source or the secondary database data source, but can directly use the existing database link to perform the operation.

[0087] In the embodiment of the present application, the synchronization delay between the master database and the slave database may also be determined, and when the synchronization delay is greater than a set threshold, the proxy data source may be determined as the master database data source.

[0088] In other words, a scheduled task can be started in an embodiment of the present application, and the scheduled task is used to detect the synchronization delay between the master database and the slave database. For example, a temporary table can be created in the scheduled task, and the time field in the temporary table is updated by writing data to the master database at a scheduled time. Since there is data synchronization between the master database and the slave database, the synchronization delay between the master database and the slave database can be determined by the difference between the time field of the master database and the time field of the slave database in the temporary table. If the synchronization delay is greater than the set threshold, for example, greater than 3 seconds, the proxy data source can be determined as the master database data source, that is, the master database is used to perform database operations to avoid failures caused by excessive differences between the master and slave databases.

[0089] S103: Execute the database operation request using the master database or slave database associated with the database link.

[0090] After obtaining the database link by executing the dynamic proxy, the database operation request can be executed using the master database or slave database associated with the database link. Since the database link is associated with the master database when the operation intention of the database operation request is to modify data, and the database link is associated with the slave database when the operation intention of the database operation request is to read data, the read-write separation of the database is realized.

[0091] Based on the above description, it can be known that in the database operation method provided by the present application, a proxy data source and a dynamic proxy for obtaining a database link are first generated, and in response to receiving a database operation request, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining the database operation statement carried in the database operation request, the transaction opening status of the database operation request, and the data modification mark in the current thread, and according to the database operation statement, the transaction opening status, and the data modification mark, determining the proxy data source as the master database data source or the slave database data source, and obtaining the database link corresponding to the proxy data source; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether a data modification operation has been performed in the current thread. Then, the database operation request is executed using the master database or the slave database associated with the database link.

[0092] In this method, a dynamic proxy is generated for obtaining a database link. Since the database operation statement carried in the database operation request can be obtained when the dynamic proxy is executed, it is possible to accurately judge whether the current database operation request is a request to modify data or a request to read data, in combination with the specific database operation statement, transaction opening status, and data modification mark, and then determine whether the proxy data source is the main database data source or the slave database data source. In this way, by executing the dynamic proxy, without modifying the original code logic, the database link can be obtained by using the main database data source or the slave database data source, and the database operation can be executed in the main database or the slave database based on the difference in the database operation request, so as to realize the read-write separation of the database operation.

[0093] See also Figure 2 Another flow chart of database operation shown in FIG. 1 shows that in response to receiving a database operation request, a dynamic proxy can be executed. First, it is determined whether the current function (i.e., the database operation request) is a public function. If so, the public function is executed for all database links in the database link cache. In particular, when the public function is a public function for closing a database link (i.e., a close function), the database link cache can be cleared, and the data modification mark in the current thread can be cleared at the same time.

[0094] If the current function is not a public function, it is possible to determine whether active synchronization is abnormal by determining the synchronization delay between the master database and the slave database, and if so, determine the proxy data source as the master database data source. If not, it is further determined whether an SQL statement is executed, and if not, the proxy data source is also determined as the master database data source.

[0095] Next, if the database operation request starts a non-read-only transaction, the proxy data source is determined as the main database data source, and the database modification mark is updated to indicate that a data modification operation has been performed in the current thread. If a non-read-only transaction is not started, further determine whether the SQL statement is a query statement (starting with select) and does not include a key statement (ending with for update), and whether a data modification operation has not been performed in the current thread. If so, it indicates that the current database operation request is a read-only request, and the proxy data source is determined as the slave database data source. Otherwise, the proxy data source is determined as the main database data source, and the database modification mark is updated to indicate that a data modification operation has been performed in the current thread.

[0096] After determining the proxy data source, obtain the database link corresponding to the proxy data source. When there is a database link corresponding to the proxy data source in the database link cache, obtain the database link from the database link cache. When there is no database link corresponding to the proxy data source in the database link cache, execute the database link acquisition function to create a database link, and then use the master database or slave database associated with the database link to execute the database operation request.

[0097] Next, combine FIG. 3A to FIG. 3F , the implementation code of the database operation method provided in this application is explained.

[0098] exist Figure 3A In the example, MasterSlaveDataSource is injected as the proxy data source, which includes the master data source db1DataSource and the slave data source db2DataSource.

[0099] exist Figure 3B In the example, MasterSlaveDataSource is the implementation class of AbstractRoutingDataSource, determineCurrentLookupKey function is used to determine whether the proxy data source is the master data source or the slave data source, and getConnection function is the database link acquisition function.

[0100] exist Figure 3C In the example, private MasterSlaveDataSource dataSource is the proxy data source, connectionMap is the database connection cache, and METHOD_CLOSE, METHOD_SET_AUTOCOMMIT, METHOD_COMMIT, and METHOD_ROLLBACK are public functions.

[0101] exist Figure 3D The first step of the invoke() function is to determine whether it is a public function.

[0102] exist Figure 3E In the example, prepareStatement is the operation of executing SQL statements in the connection, args[0] indicates that the first parameter is the SQL statement, isSlaveDelayMuch indicates the synchronization delay between the master database and the slave database, actualTransactionActive is used to obtain the transaction opening status, and setCUD(create,update,delete) is used to obtain the data modification identifier.

[0103] exist Figure 3F In the method, connectionMap is used to obtain the existing database link in the database link cache, and method.invoke(realConnection,args) is used to use the database link corresponding to the proxy data source to execute the database operation request. For example, if the database operation request is connection.prepareStatement("selec*fromtb1"), and the database link corresponding to the proxy data source is a slave database link, prepareStatement("selec*from tb1") is used to return it to the caller.

[0104] Based on the above method provided in the embodiment of the present application, the embodiment of the present application also provides a database operation device corresponding to the above method. The units / modules involved in the embodiments of the present application can be implemented by software or by hardware. The name of the unit / module does not constitute a limitation on the unit / module itself in some cases.

[0105] See also Figure 4 The schematic diagram of the structure of the database operation device shown in FIG. 400 includes:

[0106] A generation module 401, used to generate a proxy data source and a dynamic proxy for obtaining a database link;

[0107] The first execution module 402 is used to execute the dynamic proxy in response to receiving a database operation request, so that the dynamic proxy executes the following steps: obtaining a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtaining a database link corresponding to the proxy data source; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether a data modification operation has been performed in the current thread;

[0108] The second execution module 403 is used to execute the database operation request by using the master database or slave database associated with the database link.

[0109] In some possible implementations, the first execution module 402 is specifically configured to:

[0110] When the transaction opening condition indicates that the database operation request does not open a non-read-only transaction, the type of the database operation statement is a query statement and does not include a key statement, and the data modification mark indicates that no data modification operation has been performed in the current thread, the proxy data source is determined as a slave data source;

[0111] Otherwise, the proxy data source is determined as the main database data source.

[0112] In some possible implementations, the apparatus further includes an updating module, wherein the updating module is configured to:

[0113] According to the database operation statement, the data modification mark is updated.

[0114] In some possible implementations, the first execution module 402 is further configured to:

[0115] determining a synchronization delay between the master database and the slave database;

[0116] When the synchronization delay is greater than a set threshold, the proxy data source is determined as the main database data source.

[0117] In some possible implementations, the dynamic proxy further includes a database link cache, and the first execution module 402 is specifically configured to:

[0118] Obtaining a database link corresponding to the proxy data source from the database link cache; or,

[0119] Execute a database link acquisition function to acquire a database link corresponding to the proxy data source.

[0120] In some possible implementations, the dynamic proxy further includes at least one public function, and the first execution module 402 is further configured to:

[0121] In response to receiving an operation request related to the public function, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining an existing database link in the database link cache, and executing the operation request related to the public function using the existing database link.

[0122] The database operation device 400 according to the embodiment of the present application may correspond to executing the method described in the embodiment of the present application, and the above and other operations and / or functions of each module / unit of the database operation device 400 are respectively to implement Figure 1 or Figure 2 For the sake of brevity, the corresponding processes of each method in the illustrated embodiment are not described in detail here.

[0123] The functions described above in this document may be performed at least in part by one or more hardware logic components. Figure 5 The schematic diagram of the structure of the electronic device 500 for implementing database operation shown in FIG. Figure 5 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0124] like Figure 5 As shown, the electronic device 500 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage device 508 to a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the electronic device 500 are also stored. The processing device 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.

[0125] Typically, the following devices may be connected to the I / O interface 505: an input device 506 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 507 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 508 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 509. The communication device 509 may allow the electronic device 500 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 5 The electronic device 500 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.

[0126] The present application also provides a computer-readable storage medium, also referred to as a machine-readable medium. In the context of the present application, a machine-readable medium may be a tangible medium that may contain or store a program for use by an instruction execution system, device or equipment or used in combination with an instruction execution system, device or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0127] It should be noted that the computer-readable medium mentioned above in the present application may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer readable signal media may also be any computer readable medium other than computer readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.

[0128] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: generates a proxy data source and a dynamic proxy for obtaining a database link; executes the dynamic proxy in response to receiving a database operation request, so that the dynamic proxy performs the following steps: obtains a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, determines the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtains a database link corresponding to the proxy data source; and executes the database operation request using the master database or the slave database associated with the database link.

[0129] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network via a communication device, or installed from a storage device. When the computer program is executed by a processing device, the above-mentioned functions defined in the method of the embodiment of the present application are executed.

[0130] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.

[0131] Although several specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present application. Certain features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination.

[0132] The above description is only a preferred embodiment of the present application and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present application is not limited to the technical solution formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in this application (but not limited to) by each other to form a technical solution.

Claims

1. A database operation method, It is characterized in that The method comprises: Generate a proxy data source and a dynamic proxy for obtaining a database link; wherein the proxy data source is represented in the form of a class, and the class of the proxy data source stores a master database data source, a slave database data source and a database link acquisition function; the dynamic proxy is used to execute when it is necessary to obtain a database link; In response to receiving a database operation request, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtaining a database link corresponding to the proxy data source; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether a data modification operation has been performed in the current thread; Utilize the master database or slave database associated with the database link to execute the database operation request; The step of determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status and the data modification mark includes: When the transaction opening condition indicates that the database operation request does not open a non-read-only transaction, the type of the database operation statement is a query statement and does not include a key statement, and the data modification mark indicates that no data modification operation has been performed in the current thread, the proxy data source is determined as a slave data source; Otherwise, the proxy data source is determined as the main database data source.

2. The method according to claim 1, It is characterized in that The method further comprises: According to the database operation statement, the data modification mark is updated.

3. The method according to claim 1, It is characterized in that The method further comprises: determining a synchronization delay between the master database and the slave database; When the synchronization delay is greater than a set threshold, the proxy data source is determined as the main database data source.

4. The method according to claim 1, It is characterized in that The dynamic proxy also includes a database link cache, and the acquiring of the database link corresponding to the proxy data source includes: Obtaining a database link corresponding to the proxy data source from the database link cache; or, Execute a database link acquisition function to acquire a database link corresponding to the proxy data source.

5. The method according to claim 4, It is characterized in that The dynamic proxy further includes at least one public function, and the method further includes: In response to receiving an operation request related to the public function, the dynamic proxy is executed, so that the dynamic proxy performs the following steps: obtaining an existing database link in the database link cache, and executing the operation request related to the public function using the existing database link.

6. A database operation device, It is characterized in that The device comprises: A generation module, used to generate a proxy data source and a dynamic proxy for obtaining a database link; wherein the proxy data source is represented in the form of a class, and the class of the proxy data source stores a master library data source, a slave library data source and a database link acquisition function; the dynamic proxy is used to execute when it is necessary to obtain a database link; A first execution module is used to execute the dynamic proxy in response to receiving a database operation request, so that the dynamic proxy executes the following steps: obtaining a database operation statement carried in the database operation request, a transaction opening status of the database operation request, and a data modification mark in the current thread, determining the proxy data source as a master database data source or a slave database data source according to the database operation statement, the transaction opening status, and the data modification mark, and obtaining a database link corresponding to the proxy data source; wherein the transaction opening status is used to indicate whether the database operation request opens a transaction, and the data modification mark is used to indicate whether a data modification operation has been performed in the current thread; A second execution module, used to execute the database operation request using the master database or the slave database associated with the database link; The first execution module is specifically configured to: determine the proxy data source as a slave data source when the transaction opening condition indicates that the database operation request does not open a non-read-only transaction, the type of the database operation statement is a query statement and does not include a key statement, and the data modification mark indicates that no data modification operation has been performed in the current thread; Otherwise, the proxy data source is determined as the main database data source.

7. An electronic device, It is characterized in that The electronic device comprises a processor and a memory, wherein instructions are stored in the memory, and the processor executes the instructions so that the electronic device executes the method according to any one of claims 1 to 5.

8. A computer-readable storage medium, It is characterized in that The method comprises computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device executes the method according to any one of claims 1 to 5.

9. A computer program product, It is characterized in that The method comprises computer-readable instructions; the computer-readable instructions are used to implement the method according to any one of claims 1 to 5.