Database processing method, device, server and storage medium
By monitoring and handling specified exceptions, and using interceptors and detection methods to obtain valid connections for database access retries, the problems of database access exceptions and data loss caused by invalid connections in the connection pool are solved, thus achieving the robustness and continuity of the business system.
Patent Information
- Application Number
- CN202310239799.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-13
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2043-03-13
AI Technical Summary
In the prior art, the connection pool is unable to promptly identify and delete invalid connections, resulting in abnormal database access when the master-slave switch or the database server is shut down, and in severe cases, data loss may occur.
By listening to the specified exception, throwing the exception and retrying the database access based on the predefined exception handling logic, using the interceptor to intercept the target database access method, calling the detection method to check the validity of the database connection in the connection pool, obtaining the valid connection and retrying the access.
It avoids data loss, improves data security and integrity, reduces the impact on business, and enhances the robustness and continuity of business systems.
Smart Images

Figure CN116305265B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a database processing method, device, server, and storage medium. Background Art
[0002] In software system development, data is often a core element of the system. As a data carrier, a database is essential, and the fault tolerance of the connection pool connecting to the database is particularly important. For example, when using MySQL as a data storage medium, a master-slave deployment model is often adopted for data security and high availability. When an exception occurs in the master database, master-slave switching becomes a common maintenance measure to ensure normal operations. If the connection pool cannot effectively handle invalid connections, system anomalies can occur, and in severe cases, data loss can occur.
[0003] Typically, systems use Druid as a connection pool for accessing the database. After a master-slave switch or a connection being closed by the database server, connections in the system connection pool become invalid. However, the connection pool cannot promptly detect and delete these invalid connections, resulting in a period of database access anomalies. This means that all invalid connections must be consumed and new connections must be acquired before normal database access can be achieved. This period not only causes underlying database anomalies but, in severe cases, can also lead to data loss, impacting business operations.
[0004] Therefore, there is an urgent need for a solution that can avoid problems such as database access anomalies and data loss caused by invalid connections. Summary of the Invention
[0005] The purpose of the embodiments of the present application is to provide a database processing method, device, server and storage medium for enhancing the robustness and business continuity of the business system and avoiding problems such as database access anomalies and data loss due to invalid connections.
[0006] In order to achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[0007] In a first aspect, an embodiment of the present application provides a database processing method, comprising:
[0008] In the process of accessing the database through the database access method, listen for the specified exception and throw it;
[0009] Database access is retried based on a predefined exception handling logic corresponding to the specified exception, wherein the exception handling logic includes: calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection; intercepting a target database access method through an interceptor, where the target database access method refers to the database access method that throws the specified exception; and retrying access to the database based on the valid database connection and the target database access method.
[0010] In a second aspect, an embodiment of the present application provides a database processing device, including:
[0011] The monitoring unit is used to monitor and throw specified exceptions during the process of accessing the database through the database access method;
[0012] A processing unit is configured to retry database access based on a predefined exception handling logic corresponding to the specified exception, wherein the exception handling logic includes: calling a predefined detection method to perform validity detection on a database connection in a connection pool to obtain a valid database connection; intercepting a target database access method through an interceptor, wherein the target database access method refers to the database access method that throws the specified exception; and retrying access to the database based on the valid database connection and the target database access method.
[0013] In a third aspect, an embodiment of the present application provides a server, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the method described in the first aspect.
[0014] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, which, when the instructions in the storage medium are executed by a processor of the server, enables the server to execute the method described in the first aspect.
[0015] At least one of the above-mentioned technical solutions adopted in the embodiments of the present application can achieve the following beneficial effects: in the process of accessing the database through the database access method, a specified exception is listened for and thrown, and then the database access is retried based on the predefined exception handling logic for the specified exception. Specifically, by calling the predefined detection method to detect the validity of the database connection in the connection pool, a valid database connection can be automatically and timely obtained; by intercepting the target database access method that throws the exception through the interceptor, the database access method that has the database access exception can be timely known, and the database access is retried based on the valid database connection and the target database access method in the connection pool, which can avoid data loss, improve data security and integrity, and reduce the impact on the business; and, since the validity of the database connection in the connection pool has been detected, the database is automatically retried using the valid database connection and the target database access method. Therefore, under normal circumstances, executing the target database access method again will no longer cause database access exceptions, ensuring the normal execution of the database access method, and thus enhancing the robustness of the business system and business continuity. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0017] Figure 1 A schematic diagram of an application scenario applicable to a database processing method provided in one embodiment of the present application;
[0018] Figure 2 A flowchart of a database processing method provided in one embodiment of the present application;
[0019] Figure 3 A flowchart of a database processing method provided in another embodiment of the present application;
[0020] Figure 4 A schematic diagram of the structure of a database processing device provided in one embodiment of the present application;
[0021] Figure 5 A schematic diagram of the structure of a server provided for one embodiment of the present application. DETAILED DESCRIPTION
[0022] To make the purpose, technical solutions, and advantages of this application more clear, the technical solutions of this application will be clearly and completely described below in conjunction with the specific embodiments of this application and the corresponding drawings. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0023] The terms "first," "second," and the like in this specification and claims are used to distinguish similar objects and are not intended to describe a particular order or precedence. It should be understood that such terms are interchangeable where appropriate so that the embodiments of the present application can be implemented in sequences other than those illustrated or described herein. In addition, the term "and / or" in this specification and claims refers to at least one of the connected objects, and the character " / " generally indicates that the connected objects are in an "or" relationship.
[0024] Some concept descriptions:
[0025] Java: A computer development language.
[0026] Druid: is a database connection pool framework and a technology of Java language that can be used for classes and methods.
[0027] Connection Pool: When the program starts, a certain number of database connections will be created and placed in the connection pool. The number of these database connections is set by the minimum number of database connections. When a user needs to access the database, instead of establishing a new connection, an established idle database connection is taken from the connection pool. After use, the user does not close the database connection, but puts the database connection back into the connection pool for the next request to access. This omits the process of creating and closing database connections, which can improve business system performance and avoid performance degradation and waste of business system resources caused by repeated creation and closing of database connections. The establishment and disconnection of connections are managed by the connection pool itself.
[0028] Master-slave deployment mode: refers to the deployment of at least one master node and at least one slave node in the database. Usually, data is written to the master node and queries are performed using the slave node. The master node synchronizes data with the slave node to achieve data consistency between the master and slave nodes.
[0029] The reason for the master-slave switch being unavailable is: The server corresponding to the database connection in the connection pool has been switched, meaning that the original server can no longer be accessed. However, the client is unaware of the server switch, causing the database connection in the client connection pool to still be considered a valid connection. Consequently, the client still uses this database connection to access the original database, but cannot connect, resulting in a Communications link failure error.
[0030] Reasons for closing the connection to the database server: One case is that the server's firewall limits the idle time of the connection. Another case is that the database connection has exceeded a certain time and the server considers the database connection to be useless.
[0031] Pre-use check (testOnBorrow): This function checks whether the database connection is valid when applying for it. When testOnBorrow is set to true, this check is enabled. Because each database connection needs to be checked for validity, and in most cases it is, this extra check adds overhead and reduces system performance. Therefore, testOnBorrow is typically set to false, disabling this check.
[0032] Test on Return (testOnReturn): When a database connection is returned, a check is performed to verify that the connection is valid. When testOnReturn is set to true, this check is enabled. Because a check is required every time a database connection is returned, and most cases are valid, an additional check adds overhead and reduces system performance. Therefore, testOnReturn is typically set to false, disabling this check.
[0033] Idle detection function (testWhileIdle): When applying for a database connection, if the idle time is greater than timeBetweenEvictionRunsMills, a check is performed to see if the database connection is valid, where timeBetweenEvictionRunsMills indicates the number of milliseconds to sleep between two idle connection scans. When the value of testWhileIdle is true, it means that the detection is turned on, which does not affect the performance of the business system and ensures security. However, this function has a limitation, that is, it will only be detected after the idle time reaches a certain time. At the same time, because frequent connection detection will affect the performance of the business system, the actual business system usually sets the idle time to be greater than 600,000 (i.e. 10 minutes) before detection. This period of time is uncertain, and it is very likely that the database connection is invalid during this period but cannot be detected in time.
[0034] It should be understood that the database processing method provided in the embodiment of the present application can be executed by the server or software installed in the server.
[0035] The technical solutions provided by the embodiments of the present application are described in detail below with reference to the accompanying drawings.
[0036] In order to enable those skilled in the art to understand the technical solutions provided by the embodiments of the present application, Figure 1 The technical solution provided by the embodiment of the present application is described in detail in a practical application scenario shown in FIG. Figure 1 The scenario shown is merely an exemplary description and should not be understood as limiting the application scenario of the embodiments of the present application.
[0037] like Figure 1 As shown, the business system includes client 1, business server 2, and database 3. During initialization, business server 2 creates a connection pool 4, which contains a certain number of database connections, such as database connection 1 to database connection n. When business server 2 receives a database access request from client 1, it obtains an idle database connection from connection pool 4 and uses this database connection to communicate with database 3 in response to client 1's access request to database 3. After using the database connection, business server 2 does not close it, but instead returns it to connection pool 4 for use by the next request.
[0038] However, as described in the background, after a master-slave switch or a connection being closed by the database server, the connections in the system connection pool may actually become invalid. However, the connection pool cannot promptly detect and delete these invalid connections, resulting in a period of database access anomalies. This means that all invalid connections must be consumed and new connections must be acquired before normal database access can be achieved. This period not only causes underlying database anomalies but, in severe cases, can also lead to data loss, impacting business operations.
[0039] In response to the above problem, the inventors found through extensive research that if a database access request uses an invalid database connection, it will cause a database access exception, but this exception is usually not captured and processed at the business level, which in turn leads to data loss and affects the business. Based on this, an embodiment of the present application aims to propose a database processing method, which listens for and throws a specified exception in the process of accessing the database through a database access method, and then performs a database access retry based on a predefined exception handling logic for the specified exception. Specifically, by calling a predefined detection method to perform validity detection on the database connection in the connection pool, a valid database connection can be automatically and timely obtained; by intercepting the target database access method that throws the exception through an interceptor, the database access method in which the database access exception occurs can be promptly known, and access to the database can be retried based on the valid database connection and the target database access method in the connection pool, which can avoid data loss, improve data security and integrity, and reduce the impact on the business; and, since the validity of the database connection in the connection pool has been detected, the database is automatically retried using a valid database connection and the target database access method. Therefore, under normal circumstances, executing the target database access method again will no longer cause database access exceptions, ensuring the normal execution of the database access method, and thereby enhancing the robustness of the business system and business continuity.
[0040] See Figure 2 , is a flowchart of a database processing method provided by an embodiment of the present application, which can be applied to the server, such as Figure 1 The business service end 2 in the business system shown. Figure 2 As shown, the method may include the following steps:
[0041] S202: In the process of accessing the database through the database access method, listening for a specified exception and throwing it.
[0042] Specifically, the server can monitor exceptions that occur during access to the database through the database access method. If the exception is a specified exception, the specified exception is packaged into a RetrySqlException and thrown, so that the target database access method that encounters the specified exception can be captured and automatically retried during subsequent operation, thereby enhancing the robustness and business continuity of the business system.
[0043] In practical applications, the throwing of a specified exception can be implemented using various methods commonly used in the art, which can be selected according to actual needs and are not limited in this embodiment of the present application. For example, using the Java language as an example, the throw() statement or the throws() statement can be used to package the specified exception into a RetrySqlException and throw it.
[0044] In addition, the specified exception can be set according to actual needs, and the embodiments of the present application do not limit this. Optionally, considering that the connection between the client and the server has been disconnected, some database connections in the connection pool may have become invalid. In this case, if the database connection in the connection pool is continued to be used to access the database, it may cause abnormalities in the underlying database and even cause data loss, affecting the business. Based on this, the specified exception can include the disconnection between the client and the server.
[0045] S204: Retry database access based on predefined exception handling logic corresponding to the specified exception.
[0046] The predefined exception handling logic can be a pre-written exception handling mechanism for a specific exception. In practical applications, the exception handling mechanism can be written in various appropriate computer languages, such as Java, and deployed on the server. The server handles the specific exception by running the exception handling mechanism.
[0047] Specifically, the predefined exception handling logic includes: calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection; intercepting the target database access method through an interceptor, where the target database access method refers to the database access method that throws the above-mentioned specified exception; and retrying access to the database based on the valid database connection and the target database access method.
[0048] In an embodiment of the present application, the detection method is a pre-written method for performing validity detection on a database connection, such as a handleConnectionException method written in Java. Optionally, the detection method may include a connection detection method (such as checkConnection) and a detection enable method (such as openTestOnBorrow). Accordingly, in the above S204, calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection may include the following steps: turning on a pre-use detection function of the connection pool through the detection enable method, and the pre-use detection function is used to trigger the connection pool to call a connection detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection. For example, the connection pool usually has a pre-use detection function (testOnBorrow), that is, when applying for a database connection, it performs a check on whether the database connection is valid, and the default value of this function is false (i.e., turning off detection). When the server detects a specified exception during access to the database, it can enable the function by setting the value of the pre-use detection function testOnBorrow to true through the detection opening method openTestOnBorrow. After the pre-use detection function is enabled, the connection pool calls the connection detection method checkConnection to check whether each database connection in the connection pool is valid, and obtains valid database connections and invalid database connections respectively, and closes and deletes invalid database connections. As a result, all database connections in the connection pool are valid and available.
[0049] It can be understood that by pre-defining the connection detection method and the detection enable method in the exception handling logic, and using the detection enable method to enable the original pre-use detection function of the connection pool, the connection pool is triggered to perform connection validity detection through the connection detection method. This is not only simple and efficient to implement, but also can automatically and promptly obtain a valid database connection when a specified exception occurs during access to the database, thereby avoiding the subsequent use of invalid database connections to access the database and affecting the business.
[0050] Optionally, the detection method may further include a detection closing method (such as closeTestOnBorrow). Accordingly, in the above S204, calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection may further include the following steps: after the connection pool calls the connection detection method to perform validity detection on the database connection in the connection pool, closing the pre-use detection function through the detection closing method.
[0051] For example, after the connection pool calls the connection detection method checkConnection to check the validity of each database connection in the connection pool, the server can also set the value of the pre-use detection function testOnBorrow to false through the detection closing method closeTestOnBorrow to disable the function.
[0052] It is understandable that if the pre-use check function is continuously enabled, the connection pool will check whether the database connection is valid each time before using it on the server side. In most cases, the database connection is valid, and this extra check step will increase overhead and reduce business system performance. By pre-defining a check-close method in the exception handling logic, after the connection pool calls the connection check method to check the validity of the database connection in the connection pool, the pre-use check function is turned off through the check-close method. This can avoid the connection pool from frequently performing connection validity checks, thereby reducing overhead and improving business system performance.
[0053] In the embodiment of the present application, the interceptor is a pre-written interception mechanism that is used to intercept the target database access method that throws a specified exception. In practical applications, the interceptor can be written in various appropriate computer languages, such as the catch() statement in the Java language, etc., and the embodiment of the present application does not limit this.
[0054] Considering that in actual applications, database access methods used to access a database can be either transactional or non-transactional, interceptors can be provided to accurately intercept different types of database access methods. Transactional interceptors are used to intercept transactional methods that throw specified exceptions, while non-transactional interceptors are used to intercept non-transactional methods that throw specified exceptions. The following describes these two types of interceptors.
[0055] For the transaction interceptor, in the above S204, intercepting the target database access method that throws the specified exception through the interceptor can include the following steps: step A1, intercepting the transaction method with the transaction annotation through the transaction interceptor; step A2, if the transaction method throws the specified exception, determining that the transaction method is the target database access method.
[0056] Declarative transaction management is a common transaction implementation method within the Spring framework. It uses the @Transactional annotation to perform transaction operations, making it quick and easy to implement. Built on Aspect-Oriented Programming (AOP), declarative transaction management essentially intercepts method calls before and after, creating or joining a transaction before the method begins and committing or rolling back the transaction after the method completes.
[0057] For example, the transaction interceptor can be an aspect interceptor defined using the capabilities of Spring AOP. Through this transaction interceptor, the transaction method with the transaction annotation @Transactional under the Spring framework can be intercepted. If the transaction method throws a specified exception wrapped as RetrySqlException, the transaction method is determined to be the target database access method.
[0058] For the non-transaction interceptor, in the above S204, intercepting the target database access method that throws the specified exception through the interceptor may include the following steps: step B1, intercepting the public method in the mapping annotation class through the non-transaction interceptor; step B2, removing the public methods without transaction annotations from the intercepted public methods to obtain non-transaction methods; step B3, if the non-transaction method throws the specified exception, determining the non-transaction method as the target database access method.
[0059] For example, the non-transactional interceptor can be an aspect interceptor defined using the capabilities of Spring AOP. Through this non-transactional interceptor, all public methods in the @Mapper annotated class (i.e., the mapping annotation class) under the Spring framework can be intercepted, and the public methods with the transaction annotation @Transactional are excluded. The remaining public methods are non-transactional methods. Furthermore, for each non-transactional method, it is identified whether the non-transactional method throws a specified exception packaged as RetrySqlException. If so, the non-transactional method is determined as the target database access method.
[0060] It is worth noting that in actual applications, non-transactional methods can also have other forms, such as those with custom annotations, or those located in a certain package path. In this case, non-transactional interceptors can be used to intercept non-transactional methods that throw specified exceptions in the specified package path, or to intercept non-transactional methods that have custom annotations and throw specified exceptions, etc. This can avoid missing the target database access method and further improve the robustness and business continuity of the business system.
[0061] In the embodiment of the present application, different access retry mechanisms can be used to retry access to the database for different types of target database access methods to avoid data errors during the retry process. The access retry mechanisms corresponding to different types of target database methods are described below.
[0062] Case 1: The target database access method is a transaction method that throws a specified exception.
[0063] In this case, in the above S204, retrying access to the database based on the valid database connection and the target database access method may include the following steps:
[0064] Step A3: Acquire transaction propagation characteristic information of the target database access method.
[0065] Specifically, during the process of intercepting a transaction method, the transaction interceptor can also cache the transaction propagation characteristic information of the intercepted transaction method. The transaction propagation characteristic information of the transaction method is used to describe the transaction propagation characteristics of the transaction method. The propagation characteristics of the transaction method refer to the following options for specifying the execution behavior of a transactional method if a transaction method context already exists before starting the current transaction method. The definition of TransactionalDefinition usually includes the following constants representing propagation characteristics:
[0066] TransactionDefinition.PROPAGATION_REQUIRED: Indicates that the current transaction method is supported. If there is no transaction method currently, a new transaction method is created;
[0067] TransactionDefinition.PROPAGATION_SUPPORTS: Indicates support for the current transaction method. If there is no transaction method, the operation is performed in a non-transactional method.
[0068] TransactionDefinition.PROPAGATION_MANDATORY: Indicates support for the current transaction method. If there is no transaction method, an exception is thrown.
[0069] TransactionDefinition.PROPAGATION_REQUIRED_NEW: Indicates that a new transaction method is created. If a transaction method currently exists, the current transaction method will be suspended.
[0070] TransactionDefinition.PROPAGATION_NOT_SUPPORTED: Indicates that the operation is performed in a non-transactional method. If a transactional method currently exists, the current transactional method will be suspended. The transactional method is not currently supported.
[0071] TransactionDefinition.PROPAGATION_NEVE: Indicates that the operation is performed in a non-transactional method. If a transaction method currently exists, an exception is thrown and the operation cannot be run in the transaction method.
[0072] TransactionDefinition.PROPAGATION_NESTED: Indicates that if a transaction method currently exists, a transaction method is created as a nested transaction method of the current transaction method to run; if there is no transaction method currently, the value is equivalent to TransactionDefinition.PROPAGATION_REQUIRED.
[0073] Step A4: retry access to the database based on the transaction propagation characteristic information and valid database connections in the connection pool.
[0074] When a server responds to an access request from a client, it may perform multiple database operations based on the request, with each operation performed using a database access method (transactional or non-transactional). For example, a database operation may be performed based on database connection 1 and database access method 1, and then based on database connection 2 and database access method 2. Database access method 1 and database access method 2 may both be transactional methods, or both be non-transactional methods, or one may be transactional and the other non-transactional. If both database access methods are in the same transactional method and database connection 2 is invalid, retrying only database connection 2 may result in errors in the operation process or results, thereby affecting the normal execution of the business.
[0075] On this basis, because the transaction propagation characteristic information of a transaction method can reflect the execution behavior of the transaction method, the transaction propagation characteristic information of the transaction method can be used to determine the dependencies between the database access methods on the method chain in which the transaction method is located. Based on this, the database access method to be retried is determined, which helps improve the reliability of the business system and the accuracy of business processing, and ensures the normal execution of the business. Among them, the method chain in which the transaction method is located is used to represent the dependencies between the database access methods executed by the server in the process of responding to the access request from the client.
[0076] Specifically, in the above step A4, based on the transaction propagation characteristic information, it can be determined whether the target database access method and the top-level transaction method in the method chain where the target database access method is located belong to the same transaction. If so, use a valid database connection to access the database again through the top-level transaction method; if not, use a valid database connection to access the database again through the target database access method.
[0077] For example, the target database access method is referred to as the current transaction method. If the transaction propagation characteristics of the current transaction method meet the first preset condition, it can be determined that the current transaction method and the top-level transaction method in its method chain are not the same transaction method, and then a valid database connection is used to access the database again through the current transaction method. The first preset condition includes:
[0078] The propagation characteristics of the current transaction method are
[0079] TransactionDefinition.PROPAGATION_SUPPORTS, and no other transaction methods exist on the method chain where the current transaction method is located; or
[0080] The propagation characteristics of the current transaction method are
[0081] TransactionDefinition.PROPAGATION_REQUIRES_NEW; or,
[0082] The propagation characteristics of the current transaction method are
[0083] TransactionDefinition.PROPAGATION_NOT_SUPPORTED; or,
[0084] The propagation characteristics of the current transaction method are
[0085] TransactionDefinition.PROPAGATION_NEVER; or,
[0086] The propagation characteristics of the current transaction method are
[0087] TransactionDefinition.PROPAGATION_NESTED.
[0088] If the transaction propagation characteristics of the current transaction method meet the second preset condition, it can be determined that the current transaction method and the top-level transaction method in its method chain belong to the same transaction method, and then use a valid database connection to access the database again through the top-level transaction method. The second preset condition includes:
[0089] The propagation characteristics of the current transaction method are
[0090] TransactionDefinition.PROPAGATION_REQUIRED; or,
[0091] The propagation characteristics of the current transaction method are
[0092] TransactionDefinition.PROPAGATION_SUPPORTS, and there are other transaction methods on the method chain where the current transaction method is located; or
[0093] The propagation characteristics of the current transaction method are
[0094] TransactionDefinition.PROPAGATION_MANDATORY.
[0095] Case 2: The target database access method is a non-transactional method that throws the specified exception.
[0096] In this case, in the above S204, retrying access to the database based on the valid database connection and the target database access method may include the following steps:
[0097] Step B4: Use the valid database connection to access the database again through the target database access method.
[0098] Since the execution of non-transactional methods usually does not violate the integrity constraints of the database, when the target database access method is a non-transactional method that throws a specified exception, the target database access method can be retried using a valid database connection without having to retry access from the database access method before the target database access method, thereby improving the efficiency of database access.
[0099] Optionally, the database processing method provided in the embodiment of the present application can not only perform connection validity detection when a specified exception is detected, but also actively initiate connection validity detection in response to a detection request input by the detection interface provided by the server, thereby achieving the purpose of dynamically detecting connection validity while the business continues to run.
[0100] Specifically, before the above S202, the database processing method provided by the embodiment of the present application may also include: receiving a connection detection request input through a specified path, wherein the connection detection request is used to request a validity check of the database connection in the connection pool; further, in response to the connection detection request, calling a predefined detection method to perform a validity check on the database connection in the connection pool, and deleting invalid database connections in the connection pool.
[0101] In practical applications, the designated path can be configured based on actual needs and is not limited in this embodiment of the present application. For example, the designated path may include, but is not limited to, at least one of the following: a server-side HTTP (Hypertext Transfer Protocol) interface, a public configuration center, Zookeeper, or a Redis subscription and publishing channel. This can trigger all instances of the business system, and then call the detection method of the corresponding connection pool in all instances to achieve synchronous trigger detection for all instances.
[0102] It's worth noting that in some other embodiments, the server can place the connection pool in a global Map, where the key is the connection alias, such as master or slave, and the value is the connection pool. Based on this, the connection pool can be obtained by specifying the corresponding alias, and then the detection method in the connection pool can be called, achieving the purpose of dynamic detection while the service continues to run.
[0103] In some other embodiments, the server may also receive a connection check request input through a specified path before listening for a specified exception that occurs during database access and before throwing it. Furthermore, in response to the connection check request, the server invokes a predefined check method to check the validity of database connections in the connection pool and deletes invalid database connections from the connection pool. This approach is typically used when it is known that a master-slave switch is required, that is, when it is known that a database connection in the connection pool is about to become unavailable.
[0104] It is also worth noting that the technical solutions of the embodiments of the present application are applicable not only to the Java language, but also to other computer languages. Secondly, the technical solutions of the embodiments of the present application are applicable not only to the Druid connection pool, but also to other connection pool frameworks. In addition, the technical solutions of the embodiments of the present application are applicable not only to the Spring AOP framework, but also to other AOP frameworks.
[0105] See Figure 3 , is a flowchart of a database processing method provided by another embodiment of the present application, which can be applied to the server, such as Figure 1 Business service end 2 in the business system shown.
[0106] like Figure 3 As shown, the method may include the following steps:
[0107] S302: Monitoring exceptions that occur during access to a database using a database access method.
[0108] S304: Determine whether the exception is a specified exception.
[0109] S306: If not, process the exception according to the original business processing logic.
[0110] S308: If yes, then throw the specified exception and call a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection.
[0111] It is worth noting that the connection pool in the embodiments of the present application can be a pre-written connection pool, which is different from the original connection pool (such as DruidDataSource) but inherits from the original connection pool. In this way, if the detected exception is not a specified exception, the exception is handled based on the original connection pool; if the detected exception is a specified exception, the exception is handled based on the pre-written connection pool.
[0112] S310, intercepting the target database access method through an interceptor.
[0113] The target database access method refers to the database access method that throws the specified exception.
[0114] S312: Retry access to the database based on the valid database connection and the target database access method.
[0115] S314: Receive a connection detection request input through a designated path.
[0116] S316: In response to the connection detection request, a predefined detection method is called to perform validity detection on the database connection in the connection pool.
[0117] S318, delete the invalid database connection in the connection pool.
[0118] One or more embodiments of the present application provide a database processing method that, in the process of accessing a database through a database access method, listens for and throws a specified exception, and then performs a database access retry based on a predefined exception handling logic process for the specified exception. Specifically, by calling a predefined detection method to perform validity detection on the database connection in the connection pool, a valid database connection can be automatically and promptly obtained; by intercepting the target database access method that throws the exception through an interceptor, the database access method that has a database access exception can be promptly informed, and access to the database can be retried based on the valid database connection and the target database access method in the connection pool, which can avoid data loss, improve data security and integrity, and reduce the impact on the business; and, since the validity of the database connection in the connection pool has been detected, the database is automatically retried using the valid database connection and the target database access method. Therefore, under normal circumstances, executing the target database access method again will no longer cause a database access exception, thereby ensuring the normal execution of the database access method, and thereby enhancing the robustness of the business system and business continuity.
[0119] The foregoing description of this specification describes specific embodiments. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0120] In addition, with the above Figure 2 Corresponding to the database processing method shown, the embodiment of the present application also provides a database processing device, which can be applied to the server, such as Figure 1 Business server 2 in the business system shown. Figure 4 , is a schematic structural diagram of a database processing device 400 provided in one embodiment of the present application. The device 400 may include:
[0121] A monitoring unit 410 is used to monitor and throw a specified exception during the process of accessing the database through the database access method;
[0122] The processing unit 420 is used to retry database access based on a predefined exception handling logic corresponding to the specified exception, wherein the exception handling logic includes: calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection; intercepting a target database access method through an interceptor, where the target database access method refers to the database access method that throws the specified exception; and retrying access to the database based on the valid database connection and the target database access method.
[0123] Optionally, the detection method includes a connection detection method and a detection start method;
[0124] The calling of a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection includes:
[0125] The pre-use detection function of the connection pool is enabled through the detection enabling method. The pre-use detection function is used to trigger the connection pool to call the connection detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection.
[0126] Optionally, the detection method further includes a detection closing method;
[0127] The calling of a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection also includes:
[0128] After the connection pool calls the connection detection method to perform validity detection on the database connection in the connection pool, the pre-use detection function is closed by the detection closing method.
[0129] Optionally, the interceptor includes a transaction interceptor;
[0130] The method of intercepting the target database access method by the interceptor includes:
[0131] Intercepting the transaction method with transaction annotation through the transaction interceptor;
[0132] If the transaction method throws the specified exception, the transaction method is determined to be the target database access method.
[0133] Optionally, retrying access to the database based on the valid database connection and the target database access method includes:
[0134] Obtaining transaction propagation characteristic information of the target database access method;
[0135] Access to the database is retried based on the transaction propagation characteristic information and valid database connections in the connection pool.
[0136] Optionally, retrying access to the database based on the transaction propagation characteristic information and valid database connections in the connection pool includes:
[0137] Based on the transaction propagation characteristic information, determining whether the target database access method and the top-level transaction method in the method chain where the target database access method is located belong to the same transaction;
[0138] If so, use the valid database connection to access the database again through the top-level transaction method;
[0139] If not, the valid database connection is used to access the database again through the target database access method.
[0140] Optionally, the interceptor includes a non-transaction interceptor;
[0141] The target database access method intercepted by the interceptor includes:
[0142] Intercept the public method in the mapping annotation class through the non-transaction interceptor;
[0143] Remove the public methods without transaction annotations from the intercepted public methods to obtain non-transactional methods;
[0144] If the non-transactional method throws the specified exception, the non-transactional method is determined as the target database access method.
[0145] Optionally, retrying access to the database based on the valid database connection and the target database access method includes:
[0146] Using the valid database connection, the database is accessed again through the target database access method.
[0147] Optionally, the database processing device 400 further includes:
[0148] a receiving unit, configured to receive a connection detection request input through a specified path before the processing unit 420 processes the specified exception based on a predefined exception handling logic, wherein the connection detection request is used to request a validity detection of a database connection in the connection pool;
[0149] The processing unit 420 is further configured to, in response to the connection detection request, call a predefined detection method to perform validity detection on the database connections in the connection pool, and delete invalid database connections in the connection pool.
[0150] Obviously, the database processing device provided in the embodiment of the present application can be used as Figure 2 The execution body of the database processing method shown is, for example Figure 2 In the database processing method shown in FIG. 1 , step S202 can be performed by Figure 4 The monitoring unit 410 in the database processing device 400 shown in FIG. 4 is executed, and step S204 can be performed by Figure 4 The processing unit 420 in the database processing device 400 shown is executed.
[0151] According to another embodiment of the present application, Figure 4 The various units in the database processing device shown can be individually or all combined into one or several other units to form, or one (or some) of the units can be further divided into multiple smaller functional units to form, which can achieve the same operation without affecting the realization of the technical effects of the embodiments of the present application. The above-mentioned units are divided based on logical functions. In actual applications, the functions of one unit can also be implemented by multiple units, or the functions of multiple units can be implemented by one unit. In other embodiments of the present application, the database processing device may also include other units. In actual applications, these functions can also be implemented with the assistance of other units, and can be implemented by the collaboration of multiple units.
[0152] According to another embodiment of the present application, the program can be executed on a general computing device such as a computer including a central processing unit (CPU), a random access memory (RAM), a read-only memory (ROM), and other processing elements and storage elements. Figure 2 A computer program (including program code) for each step of the corresponding method shown in FIG. Figure 4 The database processing device shown in and the database processing method of the embodiment of the present application are implemented. The computer program can be recorded on a computer-readable storage medium, for example, and transferred to the server through the computer-readable storage medium and run therein.
[0153] Figure 5 This is a schematic diagram of the server structure of an embodiment of this application. Figure 5At the hardware level, the server includes a processor and, optionally, an internal bus, a network interface, and memory. The memory may include internal memory, such as high-speed random-access memory (RAM), or non-volatile memory, such as at least one disk drive. Of course, the server may also include other hardware required for the service.
[0154] The processor, network interface, and memory can be interconnected via an internal bus, which can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus, or an EISA (Extended Industry Standard Architecture) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or one type of bus.
[0155] The memory is used to store programs. Specifically, the program may include program code, which includes computer operating instructions. The memory may include internal memory and non-volatile memory, and provides instructions and data to the processor.
[0156] The processor reads the corresponding computer program from the non-volatile memory into the internal memory and then runs it, forming a database processing device at the logical level. The processor executes the program stored in the memory and is specifically used to perform the following operations:
[0157] In the process of accessing the database through the database access method, listen for the specified exception and throw it;
[0158] Database access is retried based on a predefined exception handling logic corresponding to the specified exception, wherein the exception handling logic includes: calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection; intercepting a target database access method through an interceptor, where the target database access method refers to the database access method that throws the specified exception; and retrying access to the database based on the valid database connection and the target database access method.
[0159] The above application Figure 2The methods performed by the database processing device disclosed in the illustrated embodiments can be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by hardware integrated logic circuits in the processor or by software instructions. The above processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. The various methods, steps, and logic block diagrams disclosed in the embodiments of this application can be implemented or executed. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in conjunction with the embodiments of this application can be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium well-known in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, etc. The storage medium is located in the memory, and the processor reads the information in the memory and, in conjunction with its hardware, completes the steps of the above method.
[0160] The server can also execute Figure 2 Method, and realize the database processing device in Figure 2 、 Figure 3 The functions of the illustrated embodiment will not be described in detail in the embodiments of the present application.
[0161] Of course, in addition to software implementation, the server of this application does not exclude other implementation methods, such as logical devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logical unit, but can also be hardware or logical devices.
[0162] The embodiment of the present application also provides a computer-readable storage medium, which stores one or more programs, wherein the one or more programs include instructions, which, when executed by a portable server including multiple application programs, enable the portable server to execute Figure 2 The method of the embodiment shown is specifically used to perform the following operations:
[0163] In the process of accessing the database through the database access method, listen for the specified exception and throw it;
[0164] Database access is retried based on a predefined exception handling logic corresponding to the specified exception, wherein the exception handling logic includes: calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection; intercepting a target database access method through an interceptor, where the target database access method refers to the database access method that throws the specified exception; and retrying access to the database based on the valid database connection and the target database access method.
[0165] In short, the above description is only a preferred embodiment of the present application and is not intended to limit the scope of protection of the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application shall be included in the scope of protection of the present application.
[0166] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer.
[0167] Computer-readable media includes permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. The 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 disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic 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 transitory computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0168] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.
[0169] The various embodiments in this specification are described in a progressive manner. Similar parts between the various embodiments can be referred to in conjunction with each other. Each embodiment focuses on the differences between the other embodiments. In particular, the system embodiments are generally similar to the method embodiments, so the description is relatively simple. For relevant parts, refer to the description of the method embodiments.
Claims
1. A database processing method, characterized in that: include: In the process of accessing the database through the database access method, listen for the specified exception and throw it; Database access is retried based on a predefined exception handling logic corresponding to the specified exception, wherein the exception handling logic includes: calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection; intercepting a target database access method through an interceptor, wherein the target database access method refers to the database access method that throws the specified exception; and retrying access to the database based on the valid database connection and the target database access method. wherein, if the target database access method is a transaction method, access is retried based on the database access method to be retried on the method chain where the target database access method is located, and the database access method to be retried is determined based on a dependency relationship between the database access methods on the method chain, and the dependency relationship is determined based on transaction propagation characteristic information of the target database access method.
2. The method according to claim 1, characterized in that The detection method includes a connection detection method and a detection start method; The calling of a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection includes: The pre-use detection function of the connection pool is enabled through the detection enabling method. The pre-use detection function is used to trigger the connection pool to call the connection detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection.
3. The method according to claim 1, characterized in that The interceptor includes a transaction interceptor; The method of intercepting the target database access method by the interceptor includes: Intercepting the transaction method with transaction annotation through the transaction interceptor; If the transaction method throws the specified exception, the transaction method is determined to be the target database access method.
4. The method according to claim 3, characterized in that The retrying access to the database based on the valid database connection and the target database access method includes: Obtaining transaction propagation characteristic information of the target database access method; Access to the database is retried based on the transaction propagation characteristic information and valid database connections in the connection pool.
5. The method according to claim 4, characterized in that The retrying access to the database based on the transaction propagation characteristic information and the valid database connection in the connection pool includes: Based on the transaction propagation characteristic information, determining whether the target database access method and the top-level transaction method in the method chain where the target database access method is located belong to the same transaction; If so, use the valid database connection to access the database again through the top-level transaction method; If not, the valid database connection is used to access the database again through the target database access method.
6. The method according to claim 1, characterized in that The interceptor includes a non-transaction interceptor; The target database access method intercepted by the interceptor includes: Intercept the public method in the mapping annotation class through the non-transaction interceptor; Remove the public methods without transaction annotations from the intercepted public methods to obtain non-transactional methods; If the non-transactional method throws the specified exception, the non-transactional method is determined as the target database access method.
7. The method according to claim 6, characterized in that The retrying access to the database based on the valid database connection and the target database access method includes: Using the valid database connection, the database is accessed again through the target database access method.
8. A database processing device, characterized in that: include: The monitoring unit is used to monitor and throw specified exceptions during the process of accessing the database through the database access method; A processing unit is configured to retry database access based on a predefined exception handling logic corresponding to the specified exception, wherein the exception handling logic includes: calling a predefined detection method to perform validity detection on the database connection in the connection pool to obtain a valid database connection; intercepting a target database access method through an interceptor, wherein the target database access method refers to the database access method that throws the specified exception; and retrying access to the database based on the valid database connection and the target database access method, wherein, if the target database access method is a transaction method, retrying access based on the database access method to be retried on the method chain where the target database access method is located, wherein the database access method to be retried is determined based on a dependency relationship between the database access methods on the method chain, wherein the dependency relationship is determined based on transaction propagation characteristic information of the target database access method.
9. A server, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that When the instructions in the storage medium are executed by a processor of the server, the server is enabled to execute the method according to any one of claims 1 to 7.