Deadlock detection method and device, equipment and storage medium
By judging whether the business thread initiates a second connection acquisition request to the connection pool, the problem of the failure of the existing technology to detect deadlocks is solved, and deadlock detection and exception handling are realized when the connection pool resources are exhausted, ensuring system stability.
Patent Information
- Application Number
- CN202410973540.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-19
- Publication Date
- 2024-11-08
AI Technical Summary
The prior art cannot detect whether the service thread has deadlocks, especially when the connection resources in the connection pool are exhausted, resulting in the system deadlocks that cannot be detected and processed.
The current business thread obtains the connection acquisition request initiated by the transaction manager, and determines whether there is a usable connection in the target connection pool. If it does not exist, it determines whether all occupied business threads have initiated a second connection acquisition request to the connection pool. All threads wait, and output deadlock exception information to identify deadlock.
Effectively detect and identify whether there are deadlocks in occupied business threads, avoid system stagnation caused by resource exhaustion, and improve the business processing stability of Spring Transaction Manager.
Smart Images

Figure CN118916187A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of big data technology, and in particular to a deadlock detection method, device, equipment and storage medium. Background Art
[0002] The Spring transaction manager is a component in the Spring framework for managing transactions. It provides a convenient and transparent way to handle transactions and manage databases, ensuring the consistency and integrity of data in database operations.
[0003] In the Spring framework, the @Transactional(propagation=Propagation.REQUIRES_NEW) annotation is used to specify the propagation behavior of a transaction. When a method is marked as @Transactional(propagation=Propagation.REQUIRES_NEW), it indicates that a new transaction will be started regardless of whether there is a current transaction. If a transaction currently exists, the existing transaction will be suspended until the new transaction is completed. For the connection pool, each business thread will obtain a connection from the connection pool to open and process transactions, and return the connection to the connection pool after the transaction ends. When encountering a method marked as @Transactional(propagation=Propagation.REQUIRES_NEW) during the transaction processing, the Spring transaction manager needs to suspend the current transaction and obtain a new connection from the connection pool to process the new transaction. After the new transaction is processed, the corresponding connection is returned to the connection pool and the suspended transaction continues to be processed.
[0004] However, when the connection resources in the connection pool are exhausted, it is impossible to detect whether the business thread is deadlocked. Summary of the invention
[0005] The present application provides a deadlock detection method, apparatus, device and storage medium to solve the problem that the prior art cannot detect whether a business thread is deadlocked.
[0006] In a first aspect, the present application provides a deadlock detection method, comprising:
[0007] Acquire, through the current service thread, a first connection acquisition request initiated by the transaction manager, where the first connection acquisition request is used to acquire a connection to a target database;
[0008] Initiating a second connection acquisition request to a target connection pool through the current service thread according to the first connection acquisition request, the target connection pool being used to store connections of the target database;
[0009] According to the second connection acquisition request, determining whether there is an available connection in the target connection pool;
[0010] If there is no available connection in the target connection pool, determining whether all occupied service threads have initiated a second connection acquisition request to the target connection pool, the occupied service threads being service threads that are using connections in the target connection pool to perform database operations;
[0011] If all occupied service threads have initiated a second connection acquisition request to the target connection pool, deadlock exception information is output, where the deadlock exception information is used to indicate that all occupied service threads are deadlocked.
[0012] In one possible design, the method further includes:
[0013] If there are available connections in the target connection pool, any available connection is determined as the target connection;
[0014] Encapsulating the target connection to generate a proxy connection;
[0015] The proxy connection is returned to the transaction manager.
[0016] In one possible design, the method further includes:
[0017] If any occupied service thread does not initiate a second connection acquisition request to the target connection pool, the current service thread is suspended.
[0018] In a possible design, after initiating a second connection acquisition request to a target connection pool through the current service thread according to the first connection acquisition request, the method further includes:
[0019] Storing the current service thread identifier in an application resource pool, where the application resource pool is used to store the service thread identifier that has initiated a second connection acquisition request to the target connection pool;
[0020] Correspondingly, the determining whether all occupied service threads have initiated a second connection acquisition request to the target connection pool includes:
[0021] According to the service thread identifier stored in the application resource pool, it is determined whether all occupied service threads have initiated a second connection acquisition request to the target connection pool.
[0022] In a possible design, after outputting the deadlock exception information, the method further includes:
[0023] The current service thread identifier is removed from the application resource pool.
[0024] In one possible design, the method further includes:
[0025] Monitor in real time whether there are available connections in the target connection pool;
[0026] If it is detected that there is a usable connection in the target connection pool, waking up the suspended current service thread;
[0027] Determine any connection among the available connections in the target connection pool as a target connection;
[0028] Encapsulating the target connection to generate a proxy connection;
[0029] The proxy connection is returned to the transaction manager.
[0030] In one possible design, after returning the proxy connection to the transaction manager, the method further includes:
[0031] Acquire a connection release request initiated by a transaction manager through the current service thread, wherein the connection release request is used to release the target connection encapsulated by the proxy connection;
[0032] Call the close method in the target connection to release the target connection.
[0033] In a possible design, after encapsulating the target connection and generating the proxy connection, the method further includes:
[0034] The corresponding relationship between the target connection and the current service thread is stored in a shared resource pool.
[0035] In a possible design, after calling the close method in the target connection and releasing the target connection, the method further includes:
[0036] The correspondence between the target connection and the current service thread is removed from the shared resource pool.
[0037] In a second aspect, the present application provides a deadlock detection device, comprising:
[0038] An acquisition module, configured to acquire a first connection acquisition request initiated by a transaction manager through a current business thread, wherein the first connection acquisition request is used to acquire a connection to a target database;
[0039] an initiating module, configured to initiate a second connection acquisition request to a target connection pool through the current service thread according to the first connection acquisition request, wherein the target connection pool is used to store connections of the target database;
[0040] A first judgment module, used for judging whether there is an available connection in the target connection pool according to the second connection acquisition request;
[0041] A second judgment module is used to judge whether all occupied service threads have initiated a second connection acquisition request to the target connection pool if there is no available connection in the target connection pool, and the occupied service threads are service threads that are using the connection in the target connection pool to perform database operations;
[0042] The output module is used to output deadlock exception information if all occupied service threads have initiated a second connection acquisition request to the target connection pool, wherein the deadlock exception information is used to indicate that all occupied service threads are deadlocked.
[0043] In a possible design, the deadlock detection device further includes:
[0044] A determination module, configured to determine any available connection as a target connection if there are available connections in the target connection pool;
[0045] An encapsulation module, used to encapsulate the target connection and generate a proxy connection;
[0046] A return module is used to return the proxy connection to the transaction manager.
[0047] In a possible design, the deadlock detection device further includes:
[0048] If any occupied service thread does not initiate a second connection acquisition request to the target connection pool, the current service thread is suspended.
[0049] In a possible design, after initiating a second connection acquisition request to a target connection pool through the current service thread according to the first connection acquisition request, the deadlock detection device further includes:
[0050] A storage module, used to store the current service thread identifier in an application resource pool, wherein the application resource pool is used to store the service thread identifier that has initiated a second connection acquisition request to the target connection pool;
[0051] Correspondingly, the second judgment module is specifically used for:
[0052] According to the service thread identifier stored in the application resource pool, it is determined whether all occupied service threads have initiated a second connection acquisition request to the target connection pool.
[0053] In a possible design, after the deadlock exception information is output, the deadlock detection device further includes:
[0054] The removal module is used to remove the current service thread identifier from the application resource pool.
[0055] In a possible design, the deadlock detection device further includes:
[0056] A monitoring module, used for monitoring in real time whether there are available connections in the target connection pool;
[0057] A wake-up module, configured to wake up the suspended current service thread if it is detected that there is an available connection in the target connection pool;
[0058] The determination module is used to determine any connection among the available connections in the target connection pool as a target connection;
[0059] The encapsulation module is used to encapsulate the target connection and generate a proxy connection;
[0060] The returning module is used to return the proxy connection to the transaction manager.
[0061] In a possible design, after returning the proxy connection to the transaction manager, the deadlock detection device further includes:
[0062] The acquisition module is further used to acquire a connection release request initiated by a transaction manager through the current service thread, wherein the connection release request is used to release the target connection encapsulated by the proxy connection;
[0063] The release module is used to call the closing method in the target connection to release the target connection.
[0064] In a possible design, after encapsulating the target connection and generating the proxy connection, the storage module is further used to:
[0065] The corresponding relationship between the target connection and the current service thread is stored in a shared resource pool.
[0066] In a possible design, after calling the closing method in the target connection and releasing the target connection, the removal module is further used to remove the corresponding relationship between the target connection and the current service thread from the shared resource pool.
[0067] In a third aspect, an embodiment of the present application provides an electronic device, comprising: at least one processor and a memory; the memory stores computer execution instructions; the at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor executes the deadlock detection method described in the first aspect and various possible designs of the first aspect.
[0068] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which computer execution instructions are stored. When a processor executes the computer execution instructions, the deadlock detection method described in the first aspect and various possible designs of the first aspect is implemented.
[0069] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program. When the computer program is executed by a processor, the deadlock detection method described in the first aspect and various possible designs of the first aspect is implemented.
[0070] The present application provides a deadlock detection method, device, equipment and storage medium. In the method, a first connection acquisition request initiated by a transaction manager is obtained through the current business thread, and then a second connection acquisition request is initiated to a target connection pool through the current business thread according to the first connection acquisition request, and then according to the second connection acquisition request, it is determined whether there is a usable connection in the target connection pool. If there is no usable connection in the target connection pool, it is determined whether all occupied business threads have initiated a second connection acquisition request to the target connection pool. If all occupied business threads have initiated a second connection acquisition request to the target connection pool, deadlock exception information is output. Among them, the first connection acquisition request is used to obtain a connection to a target database, the target connection pool is used to store connections to the target database, the occupied business thread is a business thread that is using a connection in the target connection pool to perform database operations, and the deadlock exception information is used to indicate that all occupied business threads are deadlocked. In the technical solution, when the connection in the target connection pool is exhausted, it is determined whether the occupied business thread has a system-level deadlock by determining whether the occupied business thread has initiated a second connection acquisition request to the target connection pool. BRIEF DESCRIPTION OF THE DRAWINGS
[0071] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0072] Figure 1 A schematic diagram of a deadlock detection method according to an embodiment of the present application;
[0073] Figure 2 A flowchart of a first embodiment of a deadlock detection method provided in an embodiment of the present application;
[0074] Figure 3 A flowchart of a second embodiment of a deadlock detection method provided in an embodiment of the present application;
[0075] Figure 4 A flowchart of a third embodiment of a deadlock detection method provided in an embodiment of the present application;
[0076] Figure 5 A flowchart of a fourth embodiment of a deadlock detection method provided in an embodiment of the present application;
[0077] Figure 6 A flowchart of a fifth embodiment of a deadlock detection method provided in an embodiment of the present application;
[0078] Figure 7 A schematic diagram of the structure of a deadlock detection device provided in an embodiment of the present application;
[0079] Figure 8 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0080] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0081] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.
[0082] In the technical solution of this application, the collection, storage, use, processing, transmission, provision and disclosure of information such as financial data or user data involved shall comply with the provisions of relevant laws and regulations and shall not violate public order and good morals.
[0083] It should be noted that in the embodiments of the present application, certain software, components, models and other existing solutions in the industry may be mentioned, and they should be regarded as exemplary. Their purpose is only to illustrate the feasibility of implementing the technical solution of the present application, but it does not mean that the applicant has or will necessarily use the solution.
[0084] First, the application scenarios involved in this application are explained.
[0085] The Spring transaction manager provides developers with a transparent transaction and database management mechanism that uses aspect-oriented programming (AOP) to manage transactions. In the default scenario, users can ignore it and simply use the @Transactional annotation to control the scope of the transaction. Specifically, when a method is marked with the @Transactional annotation, the Spring transaction manager will open a transaction before the method is executed, and after the method is executed, it will decide whether to commit or roll back the transaction based on whether the method throws an exception.
[0086] Spring transaction manager is generally used in business operation scenarios that need to ensure data consistency and integrity, such as financial transaction systems, e-commerce platforms, reservation systems, etc. These application scenarios usually involve read and write operations on the database, and require that these operations are either all successful or all rolled back when an error occurs to maintain data consistency.
[0087] When the Spring transaction manager needs to read and write to the database, it first needs to obtain a connection for data interaction with the database. There are generally two ways to obtain it. One way is to establish the connection when the database needs to be read and written, and destroy the connection after the read and write operations are completed. Another way is to pre-configure a connection pool, which stores multiple connections for data interaction with the database. The Spring transaction manager obtains a connection from the connection pool each time it needs to read and write to the database, and returns the connection after the operation is completed. Compared with the first way, the second way reduces the overhead of creating and destroying connections and improves performance.
[0088] For the @Transactional(propagation=Propagation.REQUIRES_NEW) scenario, when a method is marked as @Transactional(propagation=Propagation.REQUIRES_NEW), it means that a new transaction will be started regardless of whether there is a current transaction. If there is a current transaction, the existing transaction will be suspended until the new transaction is completed. In other words, when a method is marked with the @Transactional annotation, and another method marked with the @Transactional annotation is called internally by this method, and the propagation attribute of the called method is set to Propagation.REQUIRES_NEW, the Spring transaction manager needs to suspend the current transaction and obtain a new connection from the connection pool to process the new transaction. After the new transaction is processed, the corresponding connection is returned to the connection pool and the suspended transaction continues to be processed.
[0089] The recycling of connections in the connection pool depends on the return of connections by the Spring transaction manager, but in the @Transactional(propagation = Propagation.REQUIRES_NEW) scenario, a typical deadlock will occur when the connections in the connection pool are exhausted. Deadlock refers to a phenomenon in which two or more threads wait for each other due to competition for resources (that is, connections in the connection pool) during execution. Without external force, they will not be able to continue execution.
[0090] Especially for some connection pools that do not set a timeout for connection acquisition requests, if all connections in the connection pool are occupied and there are no available connections, the business thread trying to obtain a connection will wait indefinitely and will not give up waiting due to timeout, thus causing a deadlock or system stagnation. The deadlock cannot be resolved by itself from within the existing Spring transaction manager and connection pool, causing the entire system to suddenly die and there is no memory overflow.
[0091] In summary, in the @Transactional(propagation=Propagation.REQUIRES_NEW) scenario, if the connection resources in the connection pool are exhausted, the business thread will deadlock.
[0092] As can be seen from the above, when a business thread deadlocks, it will seriously affect the business processing of the Spring transaction manager, but the prior art does not have a method for detecting whether a business thread is deadlocked, that is, it is impossible to detect whether a business thread is deadlocked. Therefore, how to detect whether a business thread is deadlocked to ensure the normal operation of the Spring transaction manager is an urgent problem to be solved.
[0093] In order to solve the above technical problems, the technical concept of the present application is as follows: when the current business thread initiates a second connection acquisition request to obtain a connection from a connection pool, it can be determined whether there is an available connection in the connection pool. If not, it is further determined whether each occupied business thread has initiated a second connection acquisition request to the connection pool to perform deadlock detection on the business thread. If each occupied business thread has initiated a second connection acquisition request to the connection pool, it means that each business thread is in a waiting state at this time, that is, there is a deadlock in the business thread, and then deadlock exception information is output to remind relevant staff to handle the deadlock. In this way, when the connection pool resources are exhausted, it is possible to detect whether the occupied business thread is deadlocked by whether each occupied business thread initiates a second connection acquisition request to the connection pool, which effectively solves the problem that the prior art cannot detect whether the thread is deadlocked.
[0094] Figure 1 A schematic diagram of a deadlock detection method provided in an embodiment of the present application. Figure 1 As shown, the scenario includes a Spring transaction manager 11, multiple connection pools (e.g. Figure 1 Connection pool 121, connection pool 122, connection pool 123), multiple databases (for example Figure 1 Database 131, database 132, database 133) and custom data source 14.
[0095] There is a one-to-one correspondence between the connection pool and the database, and each connection pool is used to store the connection of the corresponding database. Figure 1 The connection pool 121 is used to store the connection of the database 131 , the connection pool 122 is used to store the connection of the database 132 , and the connection pool 123 is used to store the connection of the database 133 .
[0096] It should be understood that the embodiment of the present application does not limit the number of connection pools and the number of databases, and the number of connection pools and the number of databases can be determined according to actual conditions.
[0097] In actual applications, the Spring transaction manager 11, connection pool, and custom data source 14 can be deployed on the same device or on different devices. For example, the Spring transaction manager 11, connection pool, and custom data source 14 can be deployed in the same application server or in different application servers. The database server where the database is deployed can be its internal data storage device or its external data storage device relative to the above application server. This application does not Figure 1 The positional relationship between the components shown and the composition of the application scenario are not restricted in any way. The specific form can be determined according to actual conditions and will not be elaborated here.
[0098] In actual applications, the custom data source 14 inherits from the DelegatingDataSource, and provides a getConnection interface for providing a proxy connection. ProxyConnection is a proxy connection that implements the Connection interface. When creating a ProxyConnection, it is necessary to obtain a Connection object (i.e., a connection in the connection pool) that can access the database and is provided by the connection pool.
[0099] Furthermore, the custom data source 14 may also provide a shared resource pool and an application resource pool, wherein the shared resource pool is used to store the correspondence between the connection and the service thread, and the application resource pool is used to store the service thread identifier that has initiated the second connection acquisition request to the connection pool.
[0100] Furthermore, the custom data source 14 may also interact with the connection pool to obtain a connection in the connection pool.
[0101] In actual applications, the Spring transaction manager can call the getConnection interface of the custom data source 14 to initiate a first connection acquisition request to obtain a connection to the target database. The current business thread of the custom data source 14 determines the target connection pool corresponding to the target database based on the first connection acquisition request, and initiates a second connection acquisition request to the target connection pool to obtain the target connection in the target connection pool. After obtaining the target connection, a ProxyConnection is created, and the obtained target connection is assigned to the ProxyConnection. Finally, the ProxyConnection assigned with the target connection is returned to the Spring transaction manager.
[0102] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0103] Figure 2 This is a flow chart of the first embodiment of the deadlock detection method provided in the present application. Figure 2 As shown, the deadlock detection method can be implemented by the following steps:
[0104] S21. Obtain a first connection acquisition request initiated by a transaction manager through the current service thread.
[0105] The execution subject of the embodiment of the present application is an electronic device, which can be a terminal device, such as a laptop, a desktop computer, a tablet computer, etc., or a server. In practical applications, whether the electronic device is a terminal device or a server can be determined according to actual conditions, and no specific limitation is imposed on this.
[0106] Optionally, the transaction manager may be a Spring transaction manager.
[0107] It should be understood that the electronic device is deployed with Figure 1 The custom data source and connection pool shown. In addition, the electronic device can also be deployed with a Spring transaction manager or a database.
[0108] Optionally, the electronic device may be deployed with one connection pool or multiple connection pools, and different connection pools are used to store connections of different databases. In practical applications, the number of connection pools may be determined according to actual conditions, and the present application embodiment does not impose specific restrictions on this.
[0109] In this step, the Spring transaction manager may call the getConnection interface of the custom data source to initiate a first connection acquisition request, and the current business thread acquires the first connection acquisition request.
[0110] The first connection acquisition request is used to acquire a connection to a target database.
[0111] Optionally, the first connection acquisition request may carry a target database identifier.
[0112] Among them, if the Spring transaction manager sets the transaction propagation attribute to Propagation.REQUIRES_NEW when calling the getConnection interface of the custom data source, and the current business thread already holds a connection, the Spring transaction manager will suspend the current transaction so that the current business thread responds to the Spring transaction manager's call to the getConnection interface and obtains the first connection acquisition request initiated by the Spring transaction manager.
[0113] That is, the current service thread may be a service thread that has already held a connection, or may be a service thread that has not held a connection.
[0114] S22. Initiate a second connection acquisition request to the target connection pool through the current service thread according to the first connection acquisition request.
[0115] In this step, the current service thread initiates a second connection acquisition request to a target connection pool corresponding to the target database according to the target database indicated in the first connection acquisition request, so as to acquire a connection to the target database.
[0116] The target connection pool is used to store the connection of the target database.
[0117] In a possible implementation, the target database corresponding to the target database identifier and the target connection pool corresponding to the target database can be determined according to the target database identifier carried in the first connection acquisition request. Then, a second connection acquisition request is initiated to the target connection pool through the current service thread.
[0118] Optionally, the application resource pool is used to store the business thread identifier that has initiated a second connection acquisition request to the target connection pool. After the current business thread initiates the second connection acquisition request to the target connection pool, the current business thread identifier can be stored in the application resource pool so that subsequent detection of whether the occupied business thread is deadlocked can be performed based on the application resource pool.
[0119] Among them, the occupied business thread is the business thread that is using the connection in the target connection pool to perform database operations. For example, the current business thread initiates a second connection acquisition request to connection pool 1, and business thread 1 obtains a connection from connection pool 1 and uses the connection to start a transaction through the Spring transaction manager. In this case, business thread 1 is the occupied business thread.
[0120] When the occupied business threads are subsequently detected to be deadlocked, it is necessary to determine whether all the occupied business threads have initiated a second connection acquisition request to the target connection pool. By using the business thread identifier stored in the application resource pool to determine whether all the occupied business threads have initiated a second connection acquisition request to the target connection pool, the time for traversing the occupied business threads to determine whether they have initiated a second connection acquisition request to the target connection pool is reduced, thereby improving the efficiency of subsequent deadlock detection of the occupied business threads.
[0121] S23. According to the second connection acquisition request, determine whether there is an available connection in the target connection pool.
[0122] In this step, since the occupied service thread may have a deadlock problem only when there is no available connection in the target connection pool, after initiating the second connection acquisition request to the target connection pool, it is necessary to first determine whether there is an available connection in the target connection pool.
[0123] In a possible implementation, the connection pool stores status information of each connection, and the status information is used to indicate whether the status of the connection is available, in use, expired, or invalid. According to the second connection acquisition request, the status information of each connection in the target connection pool corresponding to the second connection acquisition request can be determined, and then the number of connections in the available state can be determined. If the number of connections is greater than 0, it is determined that there are available connections in the target connection pool. If the number of connections is equal to 0, it is determined that there are no available connections in the target connection pool.
[0124] In another possible implementation, a first list of connections in an available state and a second list of connections in a used state are maintained in the connection pool. According to the second connection acquisition request, the target connection pool corresponding to the second connection acquisition request can be determined, and the number of connections in the first list of the target connection pool can be checked. If the number of connections is greater than 0, it is determined that there are available connections in the target connection pool. If the number of connections is equal to 0, it is determined that there are no available connections in the target connection pool.
[0125] In another possible implementation, the number of service thread identifiers stored in the shared resource pool is determined, and the number is compared with the total number of connections in the target connection pool. If the number is equal to the total number of connections, it is determined that there are no available connections in the target connection pool. If the number is less than the total number of connections, it is determined that there are available connections in the target connection pool.
[0126] The shared resource pool is used to store the corresponding relationship between the connection and the service thread. That is, when the service thread uses the connection, the shared resource pool will record the service thread and the connection being used by the service thread.
[0127] S24: If there is no available connection in the target connection pool, determine whether all occupied service threads have initiated a second connection acquisition request to the target connection pool.
[0128] In this step, if there is no available connection in the target connection pool, it is possible that the occupied service thread is using the connection in the target connection pool and needs to wait, or it is possible that the occupied service thread has been deadlocked. Therefore, when there is no available connection in the target connection pool, it is necessary to perform deadlock detection on the occupied service thread according to the second connection acquisition request initiated by each occupied service thread to the target connection pool.
[0129] In a possible implementation, since the application resource pool stores the business thread identifier that has initiated the second connection acquisition request to the target connection pool, it is possible to determine whether all occupied business threads have initiated the second connection acquisition request to the target connection pool based on the business thread identifier stored in the application resource pool. If all occupied business threads have corresponding business thread identifiers in the application resource pool, it means that all occupied business threads have initiated the second connection acquisition request to the target connection pool. If there is an occupied business thread that does not have a corresponding business thread identifier in the application resource pool, it means that there is an occupied business thread that has not initiated the second connection acquisition request to the target connection pool.
[0130] By applying for a resource pool to determine whether all occupied business threads have initiated a second connection acquisition request to the target connection pool, the time for traversing the occupied business threads to determine whether they have initiated a second connection acquisition request to the target connection pool is reduced, thereby improving the determination efficiency.
[0131] Optionally, if there are available connections in the target connection pool, it means that there is no deadlock in the target connection pool. Any connection in the available connections can be determined as the target connection. After that, the target connection is encapsulated to generate a proxy connection. Finally, the proxy connection is returned to the transaction manager.
[0132] Furthermore, when there are available connections in the target connection pool, a deadlock detection result indicating that the occupied service threads are not deadlocked may also be generated.
[0133] When there are available connections in the target connection pool, any of the available connections can be encapsulated, and the encapsulated proxy connection can be returned to the Spring transaction manager, so that the Spring transaction manager starts the corresponding transaction and performs the corresponding database operation.
[0134] Optionally, after the target connection is determined, the corresponding relationship between the target connection and the current service thread is stored in the shared resource pool.
[0135] By storing the corresponding relationship between connections and business threads in a shared resource pool, it is possible to record which connections are being used by which business threads, control the number of threads accessing the database concurrently, and prevent the database server from being overloaded by too many concurrent connections. Furthermore, when a problem occurs with a connection (such as a network interruption or a database server failure), the shared resource pool can handle errors based on the relationship between the connection and the business thread, such as recycling the connection and notifying the thread to retry. At the same time, the shared resource pool can also recycle connections that are no longer needed in a timely manner to release connection resources for use by other business threads.
[0136] Optionally, after the target connection is determined, the current service thread identifier may be removed from the application resource pool.
[0137] After the target connection is determined, it means that the second connection acquisition request has been successfully responded to, and the current business thread identifier needs to be moved out of the application resource pool to achieve the purpose of updating the application resource pool and improve the accuracy of the business thread identifiers stored in the application resource pool.
[0138] S25. If all occupied service threads have initiated a second connection acquisition request to the target connection pool, deadlock exception information is output.
[0139] In this step, if all occupied business threads have initiated a second connection acquisition request, it means that each business thread is in a waiting state at this time, that is, the occupied business threads are deadlocked, and deadlock exception information is output to remind relevant staff to handle the deadlock. Among them, the deadlock exception information is used to indicate that all occupied business threads are deadlocked.
[0140] Optionally, when it is determined that the connection pool is deadlocked, the second connection request initiated by the current service thread needs to be rejected to prevent the current service thread from also falling into a deadlock state. At the same time, the current service thread identifier can also be removed from the application resource pool to achieve the purpose of updating the application resource pool and improve the accuracy of the service thread identifier stored in the application resource pool.
[0141] If any occupied service thread does not initiate a second connection acquisition request, the current service thread is suspended, and the target connection pool is monitored in real time to see whether there is an available connection. If it is detected that there is an available connection in the target connection pool, the suspended current service thread is awakened. Any connection in the available connection in the target connection pool is determined as the target connection. After that, the target connection is encapsulated to generate a proxy connection. Finally, the proxy connection is returned to the transaction manager.
[0142] If any occupied business thread does not initiate a second connection acquisition request, it means that the occupied business thread that did not initiate a second connection acquisition request is using a thread to process a transaction. After the transaction is processed, the used connection will be released. The connection will be subsequently allocated to other occupied business threads that initiate a second connection acquisition request or the current business thread, which means that there is no deadlock in the occupied business thread. The current business thread can be suspended so that when a usable connection is detected, the current business thread can be awakened. After the current business thread is awakened, the normal connection acquisition process is carried out, that is, the usable connection is acquired, thereby ensuring that the Spring transaction manager can execute the corresponding transaction in a timely manner.
[0143] After the target connection is determined, the corresponding relationship between the target connection and the current service thread is stored in the shared resource pool.
[0144] By storing the correspondence between connections and business threads in a shared resource pool, it is possible to record which connections are being used by which business threads, control the number of threads that concurrently access the database, and prevent the database server from being overloaded by too many concurrent connections. Furthermore, when a problem occurs with a connection (such as a network interruption or a database server failure), the shared resource pool can handle the error based on the relationship between the connection and the business thread, such as recycling the connection or notifying the thread to retry. At the same time, the shared resource pool can also ensure that connections that are no longer needed can be recycled in a timely manner to release connection resources for use by other business threads.
[0145] The deadlock detection method provided in the embodiment of the present application obtains the first connection acquisition request initiated by the Spring transaction manager through the current business thread, and then initiates a second connection acquisition request to the target connection pool through the current business thread according to the first connection acquisition request, and then determines whether there is a usable connection in the target connection pool according to the second connection acquisition request. If there is no usable connection in the target connection pool, it is determined whether all occupied business threads have initiated a second connection acquisition request to the target connection pool. If all occupied business threads have initiated a second connection acquisition request to the target connection pool, deadlock exception information is output. Among them, the first connection acquisition request is used to obtain a connection to the target database, the target connection pool is used to store connections to the target database, the occupied business thread is a business thread that is using a connection in the target connection pool to perform database operations, and the deadlock exception information is used to indicate that all occupied business threads are deadlocked. In this technical solution, the occupancy of the connection in the target connection pool is dynamically determined by the number of connections pre-configured in the target connection pool and the number of connections being used. When the connection in the target connection pool is exhausted, it is determined whether the occupied service thread initiates a second connection acquisition request to the target connection pool to determine whether the occupied service thread has a system-level deadlock, so that when the occupied service thread is detected to be deadlocked, the second connection acquisition request initiated by the current service thread is rejected to prevent the current service thread from falling into a deadlock. In addition, when the occupied service thread is detected to be deadlocked, the relevant staff is notified to handle the deadlock by recording and throwing deadlock exception information.
[0146] Optionally, in some embodiments, after obtaining the proxy connection, the current business thread starts a transaction through the proxy connection. Afterwards, during the execution of the transaction, the current business thread uses the proxy connection to perform database operations, such as reading, writing, or other types of operations. After the transaction is completed, the current business thread also needs to release the target thread so that other business threads can use it. Specifically, after the transaction corresponding to the proxy connection ends, the current business thread obtains a connection release request initiated by the transaction manager, and the connection release request is used to release the target connection encapsulated by the proxy connection. Afterwards, the close method in the target connection is called to release the target connection.
[0147] In the above embodiment, after the current service thread completes processing a transaction, it releases the target connection corresponding to the transaction back to the connection pool so that other service threads can continue to use it, thereby effectively improving the utilization rate of the connection.
[0148] Furthermore, since this transaction has ended and the target connection has been released, the correspondence between the target connection and the current business thread can also be removed from the shared resource pool to achieve the purpose of updating the shared resource pool and ensure the accuracy of the correspondence stored in the shared resource pool.
[0149] Based on the deadlock detection method shown in any of the above embodiments, the deadlock detection method is described in detail below through three business scenarios: when resources in the connection pool are sufficient, when resources in the connection pool are exhausted, and when resources are released.
[0150] Business scenario 1: Sufficient resources in the connection pool
[0151] In this business scenario, the custom data source returns a Connection to the Spring transaction manager and establishes a binding relationship between the Connection and the current business thread.
[0152] DelegatingDataSource receives the call of Spring transaction manager to its getConnection interface, obtains the first connection acquisition request initiated by Spring transaction manager, and obtains the connection of the target database. DelegatingDataSource initiates a second connection acquisition request to the target connection pool corresponding to the target database, stores the current business thread identifier in the application resource pool, and determines whether the connection resources in the target connection pool are exhausted. If not, DelegatingDataSource obtains the underlying connection (i.e. connection) through the target connection pool. DelegatingDataSource creates ProxyConnection, assigns the acquired underlying connection to it, binds the relationship between the current business thread and ProxyConnection, and puts the bound relationship into the shared resource pool. Finally, DelegatingDataSource removes the current business thread identifier from the application resource pool and returns ProxyConnection to the outside.
[0153] Figure 3 This is a flow chart of the second embodiment of the deadlock detection method provided in the present application. Figure 3 As shown, in the scenario where there are sufficient resources in the connection pool, the deadlock detection method can be implemented by the following steps:
[0154] S301. A user controller (English: User Controller) initiates a service request.
[0155] S302: Spring transaction manager starts the transaction.
[0156] S303: The Spring transaction manager initiates a first connection acquisition request to the custom data source.
[0157] S304: The custom data source stores the current business thread identifier in the application resource pool.
[0158] S305: The custom data source initiates a second connection acquisition request to the connection pool to determine whether connection resources in the connection pool are exhausted.
[0159] S306: If not exhausted, the connection pool obtains and returns the underlying connection.
[0160] S307, creating a proxy connection for the custom data source.
[0161] S308. The proxy connection binds the proxy connection to the underlying connection.
[0162] S309: The proxy connection writes the correspondence between the current service thread and the proxy connection into the shared resource pool.
[0163] S310: The custom data source deletes the current service thread identifier from the application resource pool.
[0164] S311. The custom data source returns a proxy connection to the Spring transaction manager.
[0165] S312. The Spring transaction manager executes subsequent requests based on the proxy connection.
[0166] Business scenario 2: Connection pool resources are exhausted
[0167] In this business scenario, the custom data source will determine whether the occupied business thread has a deadlock. If there is no deadlock, the current business thread will be suspended, and after a connection is released, the current business thread will continue to be processed to return the ProxyConnection. If a deadlock occurs, the deadlock occurrence log will be recorded and the deadlock exception information will be returned.
[0168] DelegatingDataSource receives the call of Spring transaction manager to its getConnection interface, obtains the first connection acquisition request initiated by Spring transaction manager, and obtains the connection of target database. DelegatingDataSource initiates the second connection acquisition request to the target connection pool corresponding to the target database, stores the current business thread identifier in the application resource pool, and calculates whether the connection resources in the target connection pool are exhausted. If exhausted, it is determined whether all occupied business threads have initiated the second connection acquisition request to the target connection pool. If there is an occupied business thread that has not initiated the second connection acquisition request to the target connection pool, it means that it has not entered the deadlock state, then suspend the current business thread, and wait for a connection to be released before waking up the current business thread. If each occupied business thread has initiated the second connection acquisition request to the target connection pool, it means that the occupied business thread has been deadlocked, and DelegatingDataSource records or alarms the error through the predetermined deadlock error handling method, and generates deadlock exception information according to the recorded error, and throws it to the Spring transaction manager method. Finally, DelegatingDataSource removes the current business thread identifier from the application resource pool.
[0169] Figure 4 This is a flow chart of the third embodiment of the deadlock detection method provided in the present application. Figure 4 As shown, in the scenario where resources are exhausted in the connection pool, the deadlock detection method can be implemented by the following steps:
[0170] S401. A user controller (English: User Controller) initiates a service request.
[0171] S402: Spring transaction manager starts the transaction.
[0172] S403: The Spring transaction manager initiates a first connection acquisition request to the custom data source.
[0173] S404: The custom data source stores the current business thread identifier in the application resource pool.
[0174] S405: The custom data source initiates a second connection acquisition request to the connection pool to determine whether connection resources in the connection pool are exhausted.
[0175] S406: If exhausted, the custom data source determines whether all occupied service threads have initiated a second connection acquisition request to the connection pool.
[0176] If all occupied service threads have initiated a second connection acquisition request to the connection pool, S407 is executed; if any occupied service thread has not initiated a second connection acquisition request to the connection pool, S412 is executed.
[0177] S407: The custom data source determines that a deadlock occurs in the occupied business thread.
[0178] S408. The custom data source records the deadlock status.
[0179] S409: The custom data source sends a second connection acquisition request.
[0180] S410: The custom data source throws a deadlock exception message.
[0181] S411, Spring transaction manager subsequent business processing.
[0182] S412: The custom data source suspends the current business thread.
[0183] Business scenario 3: Resource release
[0184] ProxyConnection receives the connection release request (close request) from the transaction manager, removes the relationship between the current business thread and ProxyConnection from the shared resource pool, calls the close method of the underlying connection of ProxyConnection, and releases the underlying connection. When the relationship between the current business thread and ProxyConnection is released, a resource release event is triggered, and the business thread that is suspended due to waiting for connection resources will be awakened.
[0185] Figure 5 This is a flow chart of the fourth embodiment of the deadlock detection method provided in the present application. Figure 5 As shown, in the resource release scenario, the deadlock detection method can be implemented by the following steps:
[0186] S501, Spring transaction manager transaction ends.
[0187] S502: The Spring transaction manager initiates a connection release request to the proxy connection.
[0188] S503: The proxy connection closes the connection with the database and recycles resources.
[0189] S504: The custom data source releases the correspondence between the current service thread and the proxy connection in the shared resource pool.
[0190] S505: The custom data source determines whether there is a service thread waiting to be awakened.
[0191] S506: The custom data source wakes up the business thread.
[0192] S507, the proxy connection returns to the upper layer call.
[0193] S508. The Spring transaction manager executes subsequent processes.
[0194] Next, through Figure 6 right Figure 4 and Figure 5 Explain the steps executed after the business thread is awakened.
[0195] Figure 6 This is a flow chart of the fifth embodiment of the deadlock detection method provided in the present application. Figure 6 As shown, after the business thread is awakened, the following steps can also be performed:
[0196] S61. The business thread of the custom data source is awakened.
[0197] S62: The custom data source initiates a second connection acquisition request to the connection pool to determine whether connection resources in the connection pool are exhausted.
[0198] S63. If not exhausted, the connection pool obtains and returns the underlying connection.
[0199] S64. Create a proxy connection for a custom data source.
[0200] S65. The proxy connection binds the proxy connection to the underlying connection.
[0201] S66. The proxy connection writes the correspondence between the current service thread and the proxy connection into the shared resource pool.
[0202] S67. The custom data source deletes the current business thread identifier from the application resource pool.
[0203] S68. The custom data source returns a proxy connection to the Spring transaction manager.
[0204] S69. The Spring transaction manager executes subsequent requests based on the proxy connection.
[0205] In summary, the deadlock detection method shown in any of the above embodiments, this application intercepts the first acquisition request of the Spring transaction manager to the underlying connection by constructing DelegatingDataSource and ProxyConnection. The binding relationship between the business thread and the connection is recorded through the shared resource pool, and the relationship between the business thread and the second connection acquisition request is recorded through the application resource pool, that is, the business thread identifier that initiates the second connection acquisition request is recorded, and the business thread identifier that does not initiate the second connection acquisition request is not recorded.
[0206] It should be understood that the present application is a deadlock detection method in a specific scenario based on the cooperation of connection pool and proxy connection. The connection pool can be an independent connection pool, and the proxy connection can also be an independent proxy connection. Similarly, the connection pool can also be an enhancement on the existing connection pool, and the proxy connection can also be an enhancement on the existing proxy connection, such as the DruidDataSource can be modified and enhanced to generate a proxy connection.
[0207] Compared with the prior art, this application predicts, detects, records and eliminates the global deadlock that may be caused by the Spring transaction manager under the @Transactional(propagation=Propagation.REQUIRES_NEW) condition, thereby reducing the problem of seemingly fault-free global deadlock in Springboot applications under high concurrency and unreasonable configuration.
[0208] Figure 7 This is a schematic diagram of the structure of the deadlock detection device provided in the embodiment of the present application. Figure 7 As shown, the deadlock detection device 70 comprises:
[0209] The acquisition module 71 is used to acquire a first connection acquisition request initiated by the transaction manager through the current service thread, where the first connection acquisition request is used to acquire a connection to a target database.
[0210] The initiating module 72 is used to initiate a second connection acquisition request to a target connection pool through a current service thread according to the first connection acquisition request. The target connection pool is used to store connections of a target database.
[0211] The first judgment module 73 is used to judge whether there is an available connection in the target connection pool according to the second connection acquisition request.
[0212] The second judgment module 74 is used to judge whether all occupied business threads have initiated a second connection acquisition request to the target connection pool if there is no available connection in the target connection pool, and the occupied business threads are business threads that are using the connection in the target connection pool to perform database operations.
[0213] The output module 75 is used to output deadlock exception information if all occupied service threads have initiated a second connection acquisition request to the target connection pool. The deadlock exception information is used to indicate that all occupied service threads are deadlocked.
[0214] In a possible design, the deadlock detection device 70 further includes:
[0215] The determination module is used to determine any connection in the available connections as the target connection if there are available connections in the target connection pool.
[0216] The encapsulation module is used to encapsulate the target connection and generate a proxy connection.
[0217] Return module, used to return the proxy connection to the transaction manager.
[0218] In a possible design, the deadlock detection device 70 further includes:
[0219] If any occupied service thread does not initiate a second connection acquisition request to the target connection pool, the current service thread is suspended.
[0220] In a possible design, after initiating a second connection acquisition request to a target connection pool through a current service thread according to the first connection acquisition request, the deadlock detection device 70 further includes:
[0221] The storage module is used to store the current service thread identifier in the application resource pool, and the application resource pool is used to store the service thread identifier that has initiated the second connection acquisition request to the target connection pool.
[0222] Accordingly, the second determination module 74 is specifically configured to:
[0223] According to the service thread identifier stored in the application resource pool, it is determined whether all occupied service threads have initiated a second connection acquisition request to the target connection pool.
[0224] In a possible design, after outputting the deadlock exception information, the deadlock detection device 70 further includes:
[0225] The removal module is used to remove the current business thread identifier from the application resource pool.
[0226] In a possible design, the deadlock detection device 70 further includes:
[0227] The monitoring module is used to monitor in real time whether there are available connections in the target connection pool.
[0228] The wake-up module is used to wake up the suspended current business thread if it is detected that there is an available connection in the target connection pool.
[0229] The determination module is used to determine any connection among the available connections in the target connection pool as a target connection.
[0230] The encapsulation module is used to encapsulate the target connection and generate a proxy connection.
[0231] Return module, used to return the proxy connection to the transaction manager.
[0232] In a possible design, after returning the proxy connection to the transaction manager, the deadlock detection device 70 further includes:
[0233] The acquisition module 71 is further used to acquire a connection release request initiated by the transaction manager through the current service thread, where the connection release request is used to release the target connection encapsulated by the proxy connection.
[0234] The release module is used to call the closing method in the target connection to release the target connection.
[0235] In a possible design, after encapsulating the target connection and generating the proxy connection, the storage module is further used to:
[0236] The correspondence between the target connection and the current service thread is stored in the shared resource pool.
[0237] In a possible design, after calling the close method in the target connection and releasing the target connection, the removal module is further used to remove the corresponding relationship between the target connection and the current service thread from the shared resource pool.
[0238] The deadlock detection device provided in the embodiment of the present application can be used to execute the technical solution of the deadlock detection method in the above embodiment. Its implementation principle and technical effect are similar and will not be repeated here.
[0239] It should be noted that it should be understood that the division of the various modules of the above device is only a division of logical functions. In actual implementation, they can be fully or partially integrated into one physical entity, or they can be physically separated. And these modules can all be implemented in the form of software called by processing elements; they can also be all implemented in the form of hardware; some modules can also be implemented in the form of software called by processing elements, and some modules can be implemented in the form of hardware. For example, the first judgment module 63 can be a separately established processing element, or it can be integrated in a chip of the above device. In addition, it can also be stored in the memory of the above device in the form of program code, and called and executed by a processing element of the above device. The function of the first judgment module 63. The implementation of other modules is similar. In addition, all or part of these modules can be integrated together, or they can be implemented independently. The processing element here can be an integrated circuit with signal processing capabilities. In the implementation process, each step of the above method or each module above can be completed by an integrated logic circuit of hardware in the processor element or instructions in the form of software.
[0240] Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 8 As shown, the electronic device 80 may include: a processor 81 and a memory 82 .
[0241] The processor 81 executes the computer execution instructions stored in the memory, so that the processor 81 executes the scheme in the above embodiment. The processor 81 can be a general-purpose processor, including a central processing unit CPU, a network processor (NP), etc.; it can also be a digital signal processor DSP, an application-specific integrated circuit ASIC, a field programmable gate array FPGA or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components.
[0242] The memory 82 is connected to the processor 81 via a system bus and completes communication between them. The memory 82 is used to store computer program instructions.
[0243] Optionally, the electronic device 80 may further include a transceiver, and the transceiver is used to communicate and interact with other devices.
[0244] The system bus can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The transceiver is used to realize the communication between the database access device and other computers (such as clients, read-write libraries, and read-only libraries). The memory may include random access memory (RAM) and may also include non-volatile memory.
[0245] An embodiment of the present application also provides a chip for running instructions, which is used to execute the technical solution of the deadlock detection method in the above embodiment.
[0246] An embodiment of the present application further provides a computer-readable storage medium, in which computer instructions are stored. When the computer instructions are executed on a computer, the computer executes the technical solution of the deadlock detection method of the above embodiment.
[0247] An embodiment of the present application also provides a computer program product, which includes a computer program stored in a computer-readable storage medium. At least one processor can read the computer program from the computer-readable storage medium. When at least one processor executes the computer program, the technical solution of the deadlock detection method in the above embodiment can be implemented.
[0248] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.
[0249] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to implement the solution of this embodiment.
[0250] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.
[0251] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the methods of various embodiments of the present application.
[0252] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the invention may be directly implemented as being executed by a hardware processor, or may be implemented by a combination of hardware and software modules in the processor.
[0253] The memory may include a high-speed RAM memory, and may also include a non-volatile storage NVM, such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.
[0254] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.
[0255] The above storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general or special purpose computer.
[0256] An exemplary storage medium is coupled to a processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an application specific integrated circuit (ASIC). Of course, the processor and the storage medium can also exist as discrete components in an electronic control unit or a main control device.
[0257] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.
[0258] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A deadlock detection method, It is characterized in that include: Acquire, through the current service thread, a first connection acquisition request initiated by the transaction manager, where the first connection acquisition request is used to acquire a connection to a target database; Initiating a second connection acquisition request to a target connection pool through the current service thread according to the first connection acquisition request, the target connection pool being used to store connections of the target database; According to the second connection acquisition request, determining whether there is an available connection in the target connection pool; If there is no available connection in the target connection pool, determining whether all occupied service threads have initiated a second connection acquisition request to the target connection pool, the occupied service threads being service threads that are using connections in the target connection pool to perform database operations; If all occupied service threads have initiated a second connection acquisition request to the target connection pool, deadlock exception information is output, where the deadlock exception information is used to indicate that all occupied service threads are deadlocked.
2. The method according to claim 1, It is characterized in that The method further comprises: If there are available connections in the target connection pool, any available connection is determined as the target connection; Encapsulating the target connection to generate a proxy connection; The proxy connection is returned to the transaction manager.
3. The method according to claim 1, It is characterized in that The method further comprises: If any occupied service thread does not initiate a second connection acquisition request to the target connection pool, the current service thread is suspended.
4. The method according to any one of claims 1 to 3, It is characterized in that After initiating a second connection acquisition request to a target connection pool through the current service thread according to the first connection acquisition request, the method further includes: Storing the current service thread identifier in an application resource pool, where the application resource pool is used to store the service thread identifier that has initiated a second connection acquisition request to the target connection pool; Correspondingly, the determining whether all occupied service threads have initiated a second connection acquisition request to the target connection pool includes: According to the service thread identifier stored in the application resource pool, it is determined whether all occupied service threads have initiated a second connection acquisition request to the target connection pool.
5. The method according to claim 4, It is characterized in that After outputting the deadlock exception information, the method further includes: The current service thread identifier is removed from the application resource pool.
6. The method according to claim 3, It is characterized in that The method further comprises: Monitor in real time whether there are available connections in the target connection pool; If it is detected that there is a usable connection in the target connection pool, waking up the suspended current service thread; Determine any connection among the available connections in the target connection pool as a target connection; Encapsulating the target connection to generate a proxy connection; The proxy connection is returned to the transaction manager.
7. The method according to claim 2 or 6, It is characterized in that After returning the proxy connection to the transaction manager, the method further comprises: Acquire a connection release request initiated by a transaction manager through the current service thread, wherein the connection release request is used to release the target connection encapsulated by the proxy connection; Call the close method in the target connection to release the target connection.
8. The method according to claim 7, It is characterized in that After encapsulating the target connection and generating a proxy connection, the method further includes: The corresponding relationship between the target connection and the current service thread is stored in a shared resource pool.
9. The method according to claim 8, It is characterized in that After calling the closing method in the target connection and releasing the target connection, the method further includes: The correspondence between the target connection and the current service thread is removed from the shared resource pool.
10. A deadlock detection device, It is characterized in that include: An acquisition module, configured to acquire a first connection acquisition request initiated by a transaction manager through a current business thread, wherein the first connection acquisition request is used to acquire a connection to a target database; an initiating module, configured to initiate a second connection acquisition request to a target connection pool through the current service thread according to the first connection acquisition request, wherein the target connection pool is used to store connections of the target database; A first judgment module, used for judging whether there is an available connection in the target connection pool according to the second connection acquisition request; A second judgment module is used to judge whether all occupied service threads have initiated a second connection acquisition request to the target connection pool if there is no available connection in the target connection pool, and the occupied service threads are service threads that are using the connection in the target connection pool to perform database operations; The output module is used to output deadlock exception information if all occupied service threads have initiated a second connection acquisition request to the target connection pool, wherein the deadlock exception information is used to indicate that all occupied service threads are deadlocked.
11. An electronic device, It is characterized in that include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the method according to any one of claims 1 to 9.
12. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 9 when executed by a processor.
13. A computer program product, It is characterized in that The invention comprises a computer program, which implements the method according to any one of claims 1 to 9 when being executed by a processor.
