Database connection control method and device, electronic equipment and program product
By introducing proxy objects into the application, intercepting and determining the type of database operation requests, and dynamically controlling database connections, the problem of not being able to fully utilize connection resources in the existing technology is solved, efficient database connection management is achieved, and system performance and response capabilities are improved.
Patent Information
- Application Number
- CN202510498951.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-18
- Publication Date
- 2025-07-29
AI Technical Summary
In the prior art, database connections cannot be dynamically controlled, resulting in the inability to fully utilize the database connection resources, especially in high concurrency environments, the connection resources are insufficient and the time is occupied for too long.
By introducing proxy objects into the application, intercepting database operation requests, judging operation types, and dynamically controlling the acquisition and release of database connections based on the type, combining Spring framework and ORM framework extensions, the refined management of database connections is achieved.
It improves the efficiency of database connection resources, reduces connection time, improves the operation efficiency of applications and database response speed, and alleviates the problem of insufficient connection resources in high concurrency environments.
Smart Images

Figure CN120386813A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of databases, and in particular, to a method and apparatus for controlling database connections, an electronic device, and a program product. Background Art
[0002] In software development, the Spring framework (i.e., a platform framework) is widely used in the development of application systems, especially in database transaction management. The Spring framework simplifies transaction management through the AOP (Aspect Oriented Programming) mechanism, enabling developers to easily manage transactions through annotations.
[0003] However, in actual applications, developers use annotations at the class level, even in scenarios where only simple data queries are performed. This approach results in unnecessary underlying database operations. The overuse of transaction annotations brings the following problems: (1) Database connections are occupied for a long time during method execution, especially when there are time-consuming non-database operations inside the method, and the connection occupation time is even longer; (2) In a high-concurrency environment, the database connection resources may be insufficient, leading to a decline in system performance.
[0004] In related technologies, although there are some database connection pool technologies (such as HikariCP (i.e., a high-performance, lightweight implementation of a database connection pool), C3P0 (Combo Pooled DataSource), etc.) that can alleviate the problem of insufficient connection resources to a certain extent, they mainly focus on the creation and recycling efficiency of connections and do not solve the problem of long-term connection occupation caused by the overuse of transaction annotations.
[0005] In addition, when an ORM framework (Object-Relational Mapping framework) manages database connections, it usually does not automatically close indirectly held connections, which further exacerbates the waste of connection resources.
[0006] Therefore, there is a lack of an effective method in related technologies to dynamically manage database connections, especially in the scenario of read-write separation, where idle database connection resources cannot be fully utilized.
[0007] To address the above problems, no effective solution has been proposed yet. Summary of the Invention
[0008] Embodiments of the present invention provide a method and apparatus for controlling database connections, an electronic device, and a program product, to at least solve the technical problem in related technologies that database connections cannot be dynamically controlled, resulting in the inability to fully utilize database connection resources.
[0009] According to one aspect of an embodiment of the present invention, a method for controlling a database connection is provided, including: during the running of an application program, when an operation request for the database connection is received, intercepting the operation request by using a proxy object; judging the operation type of the operation request through the proxy object; and controlling the database connection based on the operation type.
[0010] Further, before intercepting the operation request by using the proxy object, it further includes: creating a proxy object in a first type of framework, where the first type of framework is used to receive database operation statements and obtain a database connection from a connection pool when the database operation statements are used to request a database connection; and extending a second type of framework, where the extended second type of framework is used to trigger the proxy object when a connection wrapper class is received.
[0011] Further, the step of judging the operation type of the operation request through the proxy object includes: when the code file indicated by the application program includes annotation information, using the proxy object to determine the target annotation information of the database operation statement corresponding to the operation request, where the annotation information marks the operation type of the current operation; and determining the operation type of the operation request based on the target annotation information.
[0012] Further, the proxy object shares the thread for executing the code lines in the code file indicated by the application program. The step of judging the operation type of the operation request through the proxy object further includes: using the proxy object to determine the target code line of the database operation statement corresponding to the operation request, and obtaining the thread variable of the target thread corresponding to the target code line; and determining the operation type of the operation request based on the thread variable.
[0013] Further, during the running of the application program, it further includes: when a connection wrapper class is received, using the second type of framework to receive the connection wrapper class and trigger the proxy object; using the proxy object to intercept the connection wrapper class and parse the connection wrapper class to obtain multiple operation statements for database operations; judging the operation type of each operation statement in sequence based on the execution order in the connection wrapper class; and controlling the database connection based on the operation type.
[0014] Further, the step of controlling the database connection based on the operation type includes: when the operation type is a write type, keeping the database connection; and when the operation type is a read type, keeping the database connection until the read operation indicated by the read type is completed, and then releasing the database connection.
[0015] Further, when running the application program, it further includes: initializing the proxy object; and replacing the database connection object in the first type of framework with the proxy object, where the database connection object is used to control the database connection.
[0016] According to another aspect of the embodiments of the present invention, there is also provided a control device for database connection, including: an interception unit, configured to intercept an operation request by using a proxy object when an operation request for database connection is received during the running of an application program; a judgment unit, configured to judge the operation type of the operation request by using the proxy object; and a control unit, configured to control the database connection based on the operation type.
[0017] Further, the control device further includes: a first creation module, configured to create a proxy object in a first type of framework before intercepting the operation request by using the proxy object, where the first type of framework is used to receive database operation statements and obtain a database connection from a connection pool when the database operation statements are used to request a database connection; and a first extension module, configured to extend a second type of framework, where the extended second type of framework is used to trigger the proxy object when a connection wrapper class is received.
[0018] Further, the judgment unit includes: a first determination module, configured to determine, by using the proxy object, target annotation information of a database operation statement corresponding to the operation request when an application program-indicated code file includes annotation information, where the annotation information annotates the operation type of the current operation; and a second determination module, configured to determine the operation type of the operation request based on the target annotation information.
[0019] Further, the proxy object shares a thread for executing code lines in an application program-indicated code file, and the judgment unit further includes: a third determination module, configured to determine, by using the proxy object, a target code line of a database operation statement corresponding to the operation request and obtain a thread variable of a target thread corresponding to the target code line; and a fourth determination module, configured to determine the operation type of the operation request based on the thread variable.
[0020] Further, the control device further includes: a first reception module, configured to receive a connection wrapper class by using the second type of framework and trigger the proxy object when the connection wrapper class is received during the running of the application program; a first interception module, configured to intercept the connection wrapper class by using the proxy object and parse the connection wrapper class to obtain a plurality of operation statements for database operations; a first judgment module, configured to sequentially judge the operation type of each operation statement based on the execution order in the connection wrapper class; and a first control module, configured to control the database connection based on the operation type.
[0021] Further, the control unit includes: a first holding module, configured to hold the database connection when the operation type is a write type; and a first release module, configured to hold the database connection until a read operation indicated by the read type is completed and release the database connection when the operation type is a read type.
[0022] Further, the control device further includes: a first initialization module, configured to initialize a proxy object when running an application; a first replacement module, configured to replace a database connection object in a first type of framework with the proxy object, where the database connection object is used to control database connection.
[0023] According to another aspect of the embodiments of the present invention, there is also provided a computer program product, including a non-volatile computer-readable storage medium storing a computer program, where the computer program, when executed by a processor, implements the control method for database connection in any one of the above.
[0024] According to another aspect of the embodiments of the present invention, there is also provided an electronic device, including one or more processors and a memory, where the memory is used to store one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the control method for database connection in any one of the above.
[0025] In the present invention, during the process of running an application, when an operation request for database connection is received, the proxy object is used to intercept the operation request, the operation type of the operation request is judged through the proxy object, and based on the operation type, the database connection is controlled, thus solving the technical problem in the related art that the database connection cannot be dynamically controlled, resulting in the inability to fully utilize the database connection resources.
[0026] In the present invention, the fine-grained control of the database connection is realized through the proxy object, the database connection can be dynamically managed according to different operation types, the waste of connection resources is effectively avoided, the running efficiency of the application and the response speed of the database are improved, and thus the technical effect of reducing the occupation duration of the database connection resources and fully utilizing the database connection resources is achieved. Description of the Drawings
[0027] The drawings described herein are used to provide a further understanding of the present invention, and constitute a part of the present invention. The illustrative embodiments of the present invention and their descriptions are used to explain the present invention, and do not constitute an improper limitation to the present invention. In the drawings:
[0028] Figure 1 is a flowchart of an optional control method for database connection according to an embodiment of the present invention;
[0029] Figure 2 is a schematic diagram of an optional control device for database connection according to an embodiment of the present invention;
[0030] Figure 3 is a hardware structure block diagram of an electronic device (or mobile device) for a control method for database connection according to an embodiment of the present invention. Detailed implementation manners
[0031] To enable those skilled in the art to better understand the solution of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0032] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above drawings are used to distinguish similar objects, and do not necessarily need to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances so that the embodiments of the present invention described here can be implemented in an order other than those illustrated or described here. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0033] It should be noted that the relevant information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) collected and involved in the present invention are all information and data authorized by the user or fully authorized by all parties. And the collection, storage, use, processing, transmission, provision, disclosure and application of relevant data and other processing all comply with the relevant laws, regulations and standards in the relevant regions, take necessary confidentiality measures, do not violate public order and good customs, and provide corresponding operation entrances for users to choose to authorize or refuse. For example, an interface is set between the present system and relevant users or institutions. Before obtaining relevant information, it is necessary to send an acquisition request to the aforementioned user or institution through the interface, and obtain the relevant information after receiving the consent information feedback from the aforementioned user or institution.
[0034] The present invention provides a database connection management method, which realizes a mechanism for dynamically managing database connections by extending the Spring framework and the ORM framework. It is particularly applicable to high-concurrency environments with read-write separation and can solve the problem of long-term occupation of database connections caused by excessive use of annotations in the database transaction management of the Spring framework.
[0035] In the present invention, through the extension of the proxy pattern and the ORM framework, the automatic release of database connections during read-only operations is achieved, thereby reducing the connection occupation time and improving the utilization efficiency of database connection resources. Especially in a high-concurrency environment, it can effectively relieve the pressure of insufficient database connection resources. In addition, this database connection management method can optimize system performance and resource management without changing the upper-layer application code.
[0036] The present invention will be described in detail below in conjunction with each embodiment.
[0037] Embodiment 1
[0038] According to an embodiment of the present invention, an embodiment of a control method for database connections is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. And although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.
[0039] Figure 1 is a flowchart of an optional control method for database connections according to an embodiment of the present invention. As Figure 1 shown, the method includes the following steps:
[0040] Step S101, during the running of the application program, when an operation request for a database connection is received, use a proxy object to intercept the operation request.
[0041] In an embodiment of the present invention, when the application program is running, it will perform various database operations, such as reading, writing, updating, etc. These operations are realized through database connections. That is, during the running of the application program, when a database operation is required, an operation request for a database connection needs to be initiated to operate the database through the obtained database connection. Usually, the application program directly uses the database connection to perform various operations on the database (for example, performs operations such as reading, writing, and updating of the database through SQL (Structured Query Language) statements). However, when the application program uses annotations, since the database connection needs to be obtained before the application program runs and is kept until the application program ends running, it will cause waste of database connection resources. For this reason, this embodiment introduces a proxy object, which is located between the application program and the database connection and plays the role of intercepting operation requests. For example, when the application program needs to execute an SQL query, it actually calls the method of the proxy object instead of directly calling the database connection.
[0042] Step S102, judge the operation type of the operation request through the proxy object.
[0043] In an embodiment of the present invention, after the proxy object intercepts an operation request, it analyzes the content of the request to determine the type of operation requested, that is, to determine whether the request is a read operation, a write operation, or other types of database operations. For example, the proxy object can analyze specific parameters in the request to determine the operation type, such as api / getData?opType=read.
[0044] Step S103, based on the operation type, control the database connection.
[0045] In an embodiment of the present invention, if the proxy object determines the operation type, it can control the database connection according to the type. For example, for write operations, the database connection can be kept open to enable consecutive execution of multiple write operations; while for read operations, if the connection can be safely closed after reading is completed, the proxy object will release the database connection for other operations to use.
[0046] Through the above steps, intelligent management of the database connection can be achieved, improving the efficiency of database operations while reducing resource consumption. For example, when using the Spring framework, an AOP (Aspect-Oriented Programming) can be combined to implement the proxy object, and connection pools such as HikariCP or C3P0 can be used to manage the creation and release of database connections, thereby achieving more efficient and intelligent database resource management.
[0047] In summary, through the proxy object, fine-grained control of the database connection is achieved, and the database connection can be dynamically managed according to different operation types, effectively avoiding waste of connection resources, improving the running efficiency of the application program and the response speed of the database, thereby achieving the technical effect of reducing the occupation duration of database connection resources and making full use of database connection resources, and further solving the technical problem in the related art that the database connection cannot be dynamically controlled, resulting in the inability to make full use of database connection resources.
[0048] To achieve dynamic management of the database connection, in the control method of the database connection provided in the first embodiment of the present application, before using the proxy object to intercept the operation request, a proxy object is created in the first type of framework, where the first type of framework is used to receive database operation statements and, when the database operation statement is used to request a database connection, obtain a database connection from the connection pool; the second type of framework is extended, where the extended second type of framework is used to trigger the proxy object when receiving a connection wrapper class.
[0049] In an embodiment of the present invention, in the Spring framework (i.e., the first type of framework, which is used to receive database operation statements (such as SQL statements) and obtain a database connection from a connection pool when the database operation statement is used to request a database connection), AOP can be used to create a proxy object. This proxy object will be designed as an enhancer around the database connection object to intercept database operation requests in the application. For example, a aspect can be defined through Spring annotations, and this aspect will contain one or more annotated methods to intercept method calls of the target class (i.e., the database connection object). When a certain component in the application needs to perform a database operation, it will call the database connection object, and in fact, these calls will be captured by the proxy object.
[0050] For example, suppose there is a database connection object DBConnection. In the Spring configuration file, a DBConnectionInterceptor aspect can be defined, and this aspect contains annotated methods to execute additional logic before and after calling the methods of DBConnection. In this way, every request to the database will pass through this proxy object, and it can achieve intelligent control of the database connection without modifying the core code of the application.
[0051] In an embodiment of the present invention, for an application that uses an ORM framework such as Hibernate (i.e., an object-relational mapping framework, and the extended second type of framework is used to trigger a proxy connection object when receiving a connection wrapper class), its connection wrapper class can be extended to enable it to recognize and trigger the closing operation of the proxy connection object. In this way, even when the ORM framework indirectly holds the connection, dynamic management of the connection can be achieved.
[0052] Here, the connection wrapper class is used to proxy the real database connection, rewrite the original method of executing SQL, and close or return the connection when it detects a read operation. When the application calls the method of the connection wrapper class, in fact, it calls the interception of the proxy object, thereby intelligently controlling the acquisition and release of the database connection.
[0053] In this embodiment, more advanced control of the database connection is achieved. The proxy object can not only receive and process database operation requests, but also decide how to manage the database connection according to the type of operation. At the same time, by extending the ORM framework, the database operations in the application can be enhanced in a non-invasive manner without modifying the core code of the application. The use of the connection wrapper class further encapsulates the database operations, making the code cleaner and more maintainable. Finally, it can improve the efficiency of database operations and the response speed of the system, while reducing resource consumption and improving the overall performance of the application.
[0054] For example, in a high-concurrency scenario, by intelligently controlling the acquisition and release of database connections, the connection resources in the connection pool can be prevented from being occupied unnecessarily, ensuring that more concurrent requests can obtain database connections in a timely manner and improving the system throughput. In addition, it can also enhance the transactionality and consistency of operations. By maintaining the connection during write operations, a series of write operations can be ensured to be executed sequentially within the transaction boundary, enhancing data integrity and system reliability.
[0055] In order to accurately determine the operation type of an operation request, in the method for controlling database connections provided in Embodiment 1 of this application, when the code file indicated by the application includes annotation information, a proxy object is used to determine the target annotation information of the database operation statement corresponding to the operation request, where the annotation information is marked with the operation type of the current operation; based on the target annotation information, the operation type of the operation request is determined.
[0056] In an embodiment of the present invention, when developing an application, specific annotations can be added to the method for performing database operations to indicate whether the operation is a read operation or a write operation. For example, two annotations can be defined: @ReadOperation and @WriteOperation. When a component in the application needs to perform a database operation, the method will be marked with the corresponding annotation. At runtime, the proxy object is dynamically applied to these annotated methods (i.e., database operation statements) through the Spring framework. When the method is called, the proxy object will first check the annotation information on the method to determine its operation type.
[0057] In an embodiment of the present invention, if the proxy object determines the annotation information on the operation request, it will judge the type of the operation based on this information. For example, if the method is marked with the @ReadOperation annotation, the proxy object will recognize the request as a read operation; if it is marked with the @WriteOperation annotation, it will be recognized as a write operation. Based on this judgment, the proxy object can adopt different strategies to manage database connections, such as maintaining the connection for read operations until the operation is completed, or maintaining the connection during write operations to support consecutive write operations.
[0058] In this embodiment, by using annotations and proxy objects in the application code to determine the type of database operations, a dynamic, policy-based database connection control method can be implemented. This method not only reduces the number of times of creating and destroying database connections, thereby reducing the resource consumption of the system, but also improves the efficiency of database operations. Especially in a high-concurrency environment, it can ensure the effective utilization of database connections, avoiding unnecessary waiting and delays. In addition, this method also improves the readability and maintainability of the code, enabling developers to more easily understand and modify the logic of database operations.
[0059] For example, when a complex service call involves multiple database operations, the proxy object can intelligently control the timing of obtaining and releasing database connections by checking the annotation information on each operation, thereby optimizing the utilization of database resources while ensuring the smooth progress of the service call. In this way, even in the face of a large number of concurrent requests, the system can maintain good response capabilities and stability.
[0060] Optionally, the proxy object shares the thread that executes the code lines in the code file indicated by the application. To accurately determine the operation type of the operation request, in the method for controlling database connection provided in Embodiment 1 of the present application, a proxy object is used to determine the target code line of the database operation statement corresponding to the operation request, and obtain the thread variable of the target thread corresponding to the target code line; based on the thread variable, determine the operation type of the operation request.
[0061] In an embodiment of the present invention, in a multi-threaded environment, different parts of an application may simultaneously attempt to access a database, which is usually achieved through threads. The proxy object needs to be able to track these threads to ensure that it can accurately process database operation requests on each thread. To achieve this, the proxy object is designed to be able to "share" the threads that execute the application code. This means that when the proxy object intercepts an operation request, it can identify which thread initiated the request and can obtain the context information of that thread.
[0062] In an embodiment of the present invention, when the proxy object intercepts an operation request, it needs to be able to identify the specific location of the database operation statement (such as an SQL statement) corresponding to this request in the source code, that is, the target code line. Then, the proxy object will obtain the thread variables related to the target code line, and these variables may include the thread ID (identifier), thread status, etc. The proxy object uses this information to analyze the current activities of the thread to determine the nature of the operation request. If the proxy object obtains the thread variables of the target thread, it can determine the specific type of the operation request (such as a read operation, a write operation) based on these variables. For example, if the thread variable indicates that the current operation involves updating or inserting data, the proxy object will determine that this is a write operation. If the operation involves reading data, it will be determined as a read operation. This determination is based on the analysis of the current execution environment of the thread, and can ensure that the operation type is accurately identified, regardless of which specific method call initiates the operation request.
[0063] In this embodiment, by sharing threads and analyzing thread variables through a proxy object, precise identification and control of database operation requests are achieved. In a scenario where multiple threads execute concurrently, it can help manage database connections more meticulously, avoid resource waste, and ensure data operation consistency and transaction integrity. For example, for write operations, the database connection can be kept open to handle possible subsequent write operations; while for read operations, the database connection can be closed in a timely manner to release resources for other threads to use. This precise control not only improves the efficiency of database operations but also enhances the system's concurrent processing ability.
[0064] In order to dynamically manage database connections when receiving a connection wrapper class, in the control method of database connections provided in Embodiment 1 of this application, during the running of the application program, when receiving a connection wrapper class, a second type of framework is used to receive the connection wrapper class and trigger a proxy object; the proxy object is used to intercept the connection wrapper class and parse the connection wrapper class to obtain multiple operation statements for database operations; based on the execution order in the connection wrapper class, the operation type of each operation statement is judged in sequence; based on the operation type, the database connection is controlled.
[0065] In the embodiment of the present invention, when the application program executes database operations, a connection wrapper class (Connection Wrapper Class) can be used, which encapsulates the acquisition and release of database connections. By extending the connection wrapper class of the ORM framework, when the application program uses the connection wrapper class for database operations, a second type of framework is used to receive the connection wrapper class and trigger a proxy object to intercept the connection wrapper class using the proxy object. Then, the proxy object can parse the connection wrapper class and rewrite the original multiple database operation statements. For example, by analyzing the parameters of the connection wrapper class method, the original method for executing SQL is rewritten. Through parsing, the proxy object can obtain a series of database operation statements to be executed, preparing for the next step of judging the operation type and controlling the database connection.
[0066] In the embodiments of the present invention, the obtained multiple operation statements may be executed in the order included in the connection wrapper class (i.e., determined by the internal logic of the connection wrapper class). For example, the read operation is executed first, and then the write operation. The proxy object will judge the operation type of each operation statement one by one according to this execution order, that is, judge whether it is a read operation or a write operation. Judging the operation type may be achieved by analyzing the structure of the operation statement, using annotation information, or based on specific keywords or patterns. This process ensures that the proxy object can understand the nature of each operation, providing a basis for subsequent database connection control. Then, according to the judged operation type, the proxy object can intelligently control the life cycle of the database connection. For example, for a read operation, the proxy object may keep the connection until the read operation is completed, and then release the connection for other operations to use; for a write operation, the proxy object may keep the connection for a period of time to handle possible subsequent write operations, thereby reducing the overhead of frequently establishing and disconnecting connections. Through this dynamic management based on the operation type, the proxy object can improve the efficiency of database operations, reduce resource waste, and ensure the security of operations and the consistency of transactions.
[0067] In this embodiment, a flexible and intelligent database connection control mechanism is implemented. The proxy object can not only parse the database operation statements of the connection wrapper class, but also dynamically adjust the use of the database connection according to the type of operation, thereby reducing the number of connection establishments and releases while ensuring the efficient execution of database operations, and improving the system's concurrent processing ability and resource utilization rate. This control mechanism is particularly applicable to scenarios that need to process a large number of data requests and transactions, such as financial transactions, e-commerce systems, etc., which can effectively reduce the latency of database operations and enhance the reliability and response speed of the system.
[0068] In order to improve the accuracy of controlling the database connection, in the database connection control method provided in Embodiment 1 of the present application, when the operation type is a write type, the database connection is maintained; when the operation type is a read type, the database connection is maintained until the read operation indicated by the read type is completed, and then the database connection is released.
[0069] In the embodiments of the present invention, when the proxy object judges that the operation type of the operation request is a write type (for example, including SQL statements such as INSERT, UPDATE, and DELETE), it will keep the database connection in an open state until the write operation is completely completed. This is because the write operation may involve a series of transaction processes. For example, in a business logic, multiple write operations may need to be executed to complete the entire transaction. Keeping the connection open can avoid the overhead of re-establishing the connection between each write operation, especially in a high-concurrency scenario, which helps to improve the efficiency of the write operation and the consistency of the transaction.
[0070] When the proxy object determines that the operation type of the operation request is a read type (e.g., SELECT statement), it will maintain the database connection until all read operations are completed. If the read operation ends, the proxy object will immediately release the database connection for other read and write operations to use. This is because read operations generally have less stringent requirements for the duration of the connection compared to write operations. Releasing the connection in a timely manner can avoid resource waste. Especially for scenarios with a large number of read operations, this helps to improve the turnover rate of the connection pool, reduce waiting time, and increase the system's concurrent processing ability.
[0071] In this embodiment, through the intelligent judgment of the proxy object, fine control of the database connection is achieved. In write-type operations, the connection is maintained to support the continuity of transactions and the efficiency of database writes; while in read-type operations, the connection is dynamically managed to ensure that once the read operation is completed, the connection is immediately released back to the connection pool for the next request to use. This connection control strategy based on the operation type can not only improve the performance of database operations, especially in high-concurrency scenarios, but also ensure the consistency of transactions and the integrity of data, while effectively reducing resource consumption, improving the utilization rate of the connection pool, and ultimately enhancing the stability and response ability of the entire system.
[0072] In order to dynamically manage the database connection, in the control method of the database connection provided in the first embodiment of this application, when the application program is running, it further includes: initializing the proxy object; replacing the database connection object in the first type of framework with the proxy object, where the database connection object is used to control the database connection.
[0073] In the embodiment of the present invention, when the application program starts, the proxy object needs to be initialized. This is usually completed in the initialization configuration stage of the application. For example, in the Spring framework, the proxy object can be defined and initialized through a configuration class or a configuration file. For example, create a class for the proxy object, and this class needs to implement the same method interface as the database connection object to ensure that it can be replaced without modifying the existing code of the application. During initialization, the proxy object needs to load necessary configuration information, such as database connection pool information, operation type judgment rules, etc., so as to correctly execute its functions in subsequent database operations.
[0074] In the embodiment of the present invention, in the application program, the database connection is usually managed by a certain framework (such as the first type of framework). In order to enable the proxy object to intervene and control the database connection, it is necessary to modify the configuration of the first type of framework to replace the original database connection object with the proxy object. For example, in the Spring framework, by configuring an AOP proxy, all database connection calls can be intercepted by the proxy object. When the proxy object receives an original database connection request, it will control the creation, use, and release of the actual database connection according to its judgment logic.
[0075] In this embodiment, the proxy object can be intelligently integrated into the database connection management of the application to achieve dynamic control of the database connection. Initializing the proxy object ensures that it has all the necessary configuration information and resources, and replacing the database connection object in the framework enables the proxy object to take over the control of all database connections, including creation, use, and release.
[0076] In the embodiment of the present invention, through the extension of the proxy mode and the ORM framework, the automatic release of database connections during read-only operations is achieved, thereby reducing the connection occupation time and improving the utilization efficiency of database connection resources, thus achieving the following beneficial effects:
[0077] (1) Through the extension of the proxy mode and the ORM framework, the dynamic management of database connections is achieved, especially the automatic release of connections during read-only operations. This mechanism reduces the occupation time of connections, enabling more efficient utilization of database connection resources. In a high-concurrency environment, this optimization can effectively alleviate the problem of insufficient connection resources.
[0078] (2) Due to the reduction of unnecessary database connection occupation, the response speed of the system when processing a large number of concurrent requests is improved.
[0079] (3) By optimizing the use of database connections, the pressure on the database connection pool is reduced, thereby reducing the demand for hardware resources and the operation and maintenance costs of the database server.
[0080] (4) Through a non-invasive design, developers can enjoy the optimized connection management mechanism without modifying the existing code, reducing the implementation cost and risk, and facilitating rapid deployment in the existing system.
[0081] (5) By improving the resource utilization efficiency and performance of the system, it can help institutions support more users and business growth without increasing hardware investment, which not only reduces the institutional cost but also improves user satisfaction and business competitiveness.
[0082] The following is a detailed description in combination with another embodiment.
[0083] Embodiment 2
[0084] A control device for database connections provided in this embodiment includes multiple implementation units, and each implementation unit corresponds to each implementation step in Embodiment 1 above.
[0085] Figure 2 is a schematic diagram of an optional control device for database connections according to an embodiment of the present invention, as Figure 2As shown in the figure, the control device may include: an interception unit 20, a judgment unit 21, and a control unit 22.
[0086] Among them, the interception unit 20 is used to intercept the operation request by using a proxy object when receiving an operation request for a database connection during the process of running an application.
[0087] The judgment unit 21 is used to judge the operation type of the operation request through the proxy object.
[0088] The control unit 22 is used to control the database connection based on the operation type.
[0089] The above control device realizes fine-grained control of the database connection through the proxy object, can dynamically manage the database connection according to different operation types, effectively avoids waste of connection resources, improves the running efficiency of the application and the response speed of the database, thus achieving the technical effect of reducing the occupation duration of the database connection resources and making full use of the database connection resources, and further solves the technical problem in the related art that the database connection cannot be dynamically controlled, resulting in the inability to make full use of the database connection resources.
[0090] Optionally, the control device further includes: a first creation module, which is used to create a proxy object in the first type of framework before intercepting the operation request by using the proxy object, where the first type of framework is used to receive database operation statements and obtain a database connection from the connection pool when the database operation statement is used to request a database connection; a first extension module, which is used to extend the second type of framework, and the extended second type of framework is used to trigger the proxy object when receiving a connection wrapper class.
[0091] Optionally, the judgment unit includes: a first determination module, which is used to determine the target annotation information of the database operation statement corresponding to the operation request by using the proxy object when the code file indicated by the application includes annotation information, where the annotation information marks the operation type of the current operation; a second determination module, which is used to determine the operation type of the operation request based on the target annotation information.
[0092] Optionally, the proxy object shares the thread for executing the code lines in the code file indicated by the application, and the judgment unit further includes: a third determination module, which is used to determine the target code line of the database operation statement corresponding to the operation request by using the proxy object and obtain the thread variable of the target thread corresponding to the target code line; a fourth determination module, which is used to determine the operation type of the operation request based on the thread variable.
[0093] Optionally, the control device further includes: a first receiving module, configured to, during the running of an application, when receiving a connection wrapper class, receive the connection wrapper class by using a second type of framework and trigger a proxy object; a first interception module, configured to intercept the connection wrapper class by using the proxy object and parse the connection wrapper class to obtain multiple operation statements for database operations; a first judgment module, configured to sequentially judge the operation types of each operation statement based on the execution order in the connection wrapper class; and a first control module, configured to control the database connection based on the operation type.
[0094] Optionally, the control unit includes: a first holding module, configured to hold the database connection when the operation type is a write type; and a first releasing module, configured to hold the database connection until the read operation indicated by the read type is completed and release the database connection when the operation type is a read type.
[0095] Optionally, the control device further includes: a first initialization module, configured to initialize the proxy object when the application is running; and a first replacement module, configured to replace the database connection object in the first type of framework with the proxy object, where the database connection object is used to control the database connection.
[0096] The above control device may further include a processor and a memory. The above interception unit 20, judgment unit 21, control unit 22, etc. are all stored in the memory as program units, and the processor executes the above program units stored in the memory to implement corresponding functions.
[0097] The above processor includes a kernel, and the kernel retrieves the corresponding program unit from the memory. One or more kernels may be set, and the database connection is controlled based on the operation type by adjusting the kernel parameters.
[0098] The above memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in forms such as read-only memory (ROM) or flash memory (flash RAM), and the memory includes at least one storage chip.
[0099] The present invention also provides a computer program product, which is suitable for executing a program initialized with the following method steps when executed on a data processing device: during the running of an application, when receiving an operation request for a database connection, intercept the operation request by using a proxy object, judge the operation type of the operation request through the proxy object, and control the database connection based on the operation type.
[0100] According to another aspect of the embodiments of the present invention, there is also provided a computer program product, including a non-volatile computer-readable storage medium storing a computer program, where when the computer program is executed by a processor, it implements the control method for database connection in any one of the above.
[0101] According to another aspect of the embodiments of the present invention, there is also provided an electronic device, including one or more processors and a memory, where the memory is used to store one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the control method for database connection described above.
[0102] Figure 3 is a hardware structural block diagram of an electronic device (or mobile device) for the control method of database connection according to an embodiment of the present invention. As Figure 3 shown, the electronic device may include one or more processors (for example, Figure 3 processor 302a, processor 302b,..., processor 302n in Figure 3 etc., and these processors may include, but are not limited to, processing devices such as a microprocessor MCU or a field programmable gate array FPGA), and a memory 304 for storing data. In addition, it may further include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the I / O interface), a network interface, a keyboard, a power supply, and / or a camera. Those of ordinary skill in the art can understand that Figure 3 the structure shown Figure 3 is only schematic and does not limit the structure of the above electronic device. For example, the electronic device may further include more or fewer components than
[0103] shown in
[0104] The embodiments or examples of the present disclosure are not exhaustive. They are only illustrations of some embodiments or examples and do not constitute specific limitations on the protection scope of the present disclosure. Without contradiction, each step in a certain embodiment or example can be implemented as an independent example, and the steps can be combined arbitrarily. For example, a solution obtained by removing some steps in a certain embodiment or example can also be implemented as an independent example, and the order of the steps in a certain embodiment or example can be exchanged arbitrarily. In addition, the optional ways or optional examples in a certain embodiment or example can be combined arbitrarily; furthermore, the embodiments or examples can be combined arbitrarily. For example, some or all steps of different embodiments or examples can be combined arbitrarily, and a certain embodiment or example can be combined arbitrarily with the optional ways or optional examples of other embodiments or examples.
[0105] In the above embodiments of the present invention, the descriptions of the respective embodiments have their own focuses. For parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0106] In the several embodiments provided by the present invention, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are merely illustrative. For example, the division of the units can be a logical function division, and there can be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces, and the indirect couplings or communication connections of the units or modules can be in electrical or other forms.
[0107] The units described as separate components may or may not be physically separated. The components shown as units may or may not be physical units, that is, they can be located in one place or distributed to multiple units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0108] In addition, the functional units in the various embodiments of the present invention can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above integrated units can be implemented in the form of hardware or in the form of software functional units.
[0109] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The foregoing storage medium includes: various media that can store program codes, such as USB flash drives, read-only memories (ROMs), random access memories (RAMs), mobile hard disks, magnetic disks, or optical discs.
[0110] The foregoing are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present invention, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present invention.
Claims
1. A control method for database connection, characterized in that, including: During the running of the application, when an operation request for a database connection is received, intercept the operation request using a proxy object; Determine the operation type of the operation request through the proxy object; Control the database connection based on the operation type.
2. The control method according to claim 1, wherein Before intercepting the operation request using a proxy object, it further includes: Create the proxy object in a first type of framework, where the first type of framework is used to receive database operation statements and, when the database operation statements are used to request a database connection, obtain a database connection from a connection pool; Extend a second type of framework, where the extended second type of framework is used to trigger the proxy object when a connection wrapper class is received.
3. The control method according to claim 1, characterized in that The step of determining the operation type of the operation request through the proxy object includes: When the code file indicated by the application includes annotation information, use the proxy object to determine the target annotation information of the database operation statement corresponding to the operation request, where the annotation information is marked with the operation type of the current operation; Determine the operation type of the operation request based on the target annotation information.
4. The control method according to claim 1, characterized in that The proxy object shares the thread executing the code lines in the code file indicated by the application. The step of determining the operation type of the operation request through the proxy object further includes: Use the proxy object to determine the target code line of the database operation statement corresponding to the operation request and obtain the thread variable of the target thread corresponding to the target code line; Determine the operation type of the operation request based on the thread variable.
5. The control method according to claim 1, wherein During the running of the application, it further includes: When a connection wrapper class is received, use the second type of framework to receive the connection wrapper class and trigger the proxy object; Use the proxy object to intercept the connection wrapper class and parse the connection wrapper class to obtain multiple operation statements for database operations; Based on the execution order in the connection wrapper class, sequentially determine the operation type of each operation statement; Control the database connection based on the operation type.
6. The control method according to any one of claims 1 and 5, characterized in that, The step of controlling the database connection based on the operation type includes: When the operation type is a write type, keep the database connection; When the operation type is a read type, keep the database connection until the read operation indicated by the read type is completed, and then release the database connection.
7. The control method according to claim 1, characterized in that, When running the application, it further includes: Initialize the proxy object; Replace the database connection object in the first type of framework with the proxy object, where the database connection object is used to control the database connection.
8. A control device for database connection, characterized in that, including: An interception unit for intercepting the operation request using a proxy object during the running of the application when an operation request for a database connection is received; A judgment unit for determining the operation type of the operation request through the proxy object; A control unit for controlling the database connection based on the operation type.
9. A computer program product, characterized in that, It includes a non-volatile computer-readable storage medium that stores a computer program, and when the computer program is executed by a processor, it implements the control method for database connection described in any one of claims 1 to 7.
10. An electronic device, characterized in that, It includes one or more processors and a memory, and the memory is used to store one or more programs. Among them, when the one or more programs are executed by the one or more processors, the one or more processors implement the control method for database connection described in any one of claims 1 to 7.