SQL execution tracing methods, devices, media, and systems
By using bytecode enhancement technology and JDBC layer proxy mechanism in Java Web applications, a non-intrusive binding between HTTP request context and SQL execution behavior is achieved, solving the problem of difficulty in tracing the relationship between HTTP requests and SQL execution behavior in Java Web applications, and improving the efficiency of performance diagnosis and log analysis.
Patent Information
- Application Number
- CN202611476068.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-21
- Publication Date
- 2026-11-17
AI Technical Summary
In Java Web applications, there is no intuitive relationship between HTTP requests and database SQL. Existing technologies have drawbacks such as strong intrusion, poor compatibility, inability to cover asynchronous thread switching scenarios, and high performance overhead in production environments, making it difficult to achieve non-intrusive binding between HTTP request context and SQL execution behavior.
The bytecode enhancement technique is used to intercept the HTTP request processing flow of the Web layer, collect request context information and bind it to thread-local variables, intercept database connection objects through the unified proxy mechanism of the JDBC layer, dynamically write the request context information into SQL statements, and asynchronously send them to the storage system for centralized storage.
It achieves non-intrusive binding of HTTP request context and SQL execution behavior without modifying business code or relying on a specific persistence layer framework, providing end-to-end, non-intrusive, and universal SQL execution tracing, and improving the efficiency of log analysis and performance diagnosis.
Smart Images

Figure CN123115188A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of SQL execution link tracing technology, and more specifically, to an SQL execution link tracing method, apparatus, medium, and system. Background Technology
[0002] In existing Java Web applications (Java applications running on a web server), there is a lack of intuitive correlation between HTTP requests and database SQL. Because SQL is typically scattered across persistence layer frameworks or business logic, and the HTTP context exists only at the web layer, it is difficult to trace which specific HTTP request triggered the SQL during log analysis and performance diagnostics. Existing technologies (such as solutions relying on the internal logic of persistence layer frameworks) suffer from drawbacks such as high invasiveness, poor compatibility (difficulty adapting to multiple frameworks / data sources), inability to cover asynchronous thread switching scenarios, and significant performance overhead in production environments.
[0003] It is evident that a pressing issue is how to achieve non-intrusive binding between HTTP request context and SQL execution behavior without modifying business code or relying on a specific persistence layer framework. Summary of the Invention
[0004] The main objective of this application is to provide a method, apparatus, medium, and system for SQL execution tracing, so as to at least solve the problem of how to achieve non-intrusive binding between HTTP request context and SQL execution behavior without modifying business code or relying on a specific persistence layer framework.
[0005] To achieve the above objectives, according to one aspect of this application, an SQL execution tracing method is provided, comprising: during the startup or operation of a Java Web application, intercepting the HTTP request processing flow of the Web layer using bytecode enhancement technology, collecting the request context information of the current HTTP request, and binding the request context information to the thread local variable of the current processing thread; after the application object management container is initialized, intercepting the data source object in the application object management container to ensure that all database connection objects obtained through the data source are uniformly proxied; when the business logic creates a pre-compiled SQL execution object through the proxied database connection object, intercepting the creation process, reading the request context information stored in the thread local variable, and writing the request context information as a comment into the SQL statement to be executed to generate an enhanced SQL statement; passing the enhanced SQL statement to the JDBC driver for execution, and collecting SQL parameter information and execution time information before or during execution, constructing the enhanced SQL statement, request context information, SQL parameter information, and execution time information into structured data, and asynchronously sending it to a storage system for centralized storage.
[0006] Optionally, collecting the request context information of the current HTTP request and binding the request context information to the thread local variable of the current processing thread includes: inserting interception logic at the entry method of the current HTTP request entering the application server, extracting the identification information, request path and request method of the current HTTP request as the request context information; and calling Java's thread local variable mechanism to store the extracted request context information in the thread local variable of the current processing thread.
[0007] Optionally, the method further includes: during the asynchronous task creation or submission phase triggered by the HTTP request, intercepting the execution interface or submission method of the asynchronous task, reading the request context information from the thread local variable of the current HTTP request thread; encapsulating the read request context information and passing it as a parameter or context attribute to the asynchronous task; when the asynchronous task is scheduled to a new asynchronous execution thread, inserting recovery logic at the front end of the asynchronous task execution logic, and writing the encapsulated request context information into the thread local variable corresponding to the new asynchronous execution thread, so that the asynchronous execution thread completes the enhanced binding of the SQL statement based on the request context information in the local variable of the asynchronous execution thread when performing database access operations.
[0008] Optionally, the data source object in the application object management container is intercepted by proxy, including: listening to the Bean initialization event or data source creation event of the application object management container; when the data source object initialization is detected to be complete, obtaining the proxy object of the data source object; and overriding the method of obtaining the connection object of the proxied data object so that the method of obtaining the connection object returns a database connection proxy object wrapped by the proxy, the database connection proxy object being used to intercept subsequent calls to create a precompiled statement object.
[0009] Optionally, the request context information is written as a comment into the SQL statement to be executed to generate an enhanced SQL statement, including: when the SQL execution object is a pre-compiled SQL execution object, obtaining the original SQL string template, constructing a comment string to identify the request source, and inserting the comment string into the head or a specified position of the original SQL string through an SQL comment syntax prefix to form an enhanced SQL string, wherein the comment string includes the request tracking ID, request path, and timestamp information; when the SQL execution object is a non-pre-compiled SQL execution object or a non-SQL execution operation, executing the pass-through process and determining that no SQL content modification is required.
[0010] Optionally, collecting SQL parameter information and execution time information includes: before the execution method is called, recording the current system timestamp as the start time; intercepting and obtaining the binding parameter values set for the SQL execution object to determine the SQL parameter information; after the execution method is called, recording the current system timestamp as the end time and calculating the execution time information; determining whether there is HTTP request context information in the local variables of the current processing thread, and if so, marking the collected data as associated data; if not, marking it as independent execution data and performing release processing.
[0011] Optionally, the method further includes: re-collecting the SQL execution status when the enhancement logic fails or an anomaly occurs.
[0012] According to another aspect of this application, an SQL execution tracing device is provided, comprising: a first processing unit, configured to intercept the HTTP request processing flow of the Web layer using bytecode enhancement technology during the startup or operation of a Java Web application, collect the request context information of the current HTTP request, and bind the request context information to the thread local variable of the current processing thread; a second processing unit, configured to intercept the data source object in the application object management container after the application object management container is initialized, so that all database connection objects obtained through the data source are uniformly proxied; a third processing unit, configured to intercept the creation process when the business logic creates a pre-compiled SQL execution object through the proxied database connection object, read the request context information stored in the thread local variable, and write the request context information into the SQL statement to be executed in the form of comments to generate an enhanced SQL statement; and a fourth processing unit, configured to pass the enhanced SQL statement to the JDBC driver for execution, and collect SQL parameter information and execution time information before or during execution, construct the enhanced SQL statement, request context information, SQL parameter information, and execution time information into structured data, and asynchronously send it to the storage system for centralized storage.
[0013] According to another aspect of this application, a computer-readable storage medium is provided, the computer-readable storage medium including a stored program, wherein, when the program is executed, it controls the device on which the computer-readable storage medium is located to perform any of the methods described.
[0014] According to another aspect of this application, an SQL execution tracing system is provided, comprising: one or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the one or more programs including methods for performing any one of the methods described.
[0015] By applying the technical solution of this application, core methods of Web components such as Servlets or Filters are dynamically intercepted during application runtime through bytecode enhancement technology. When an HTTP request arrives, context information such as the request identifier and path is automatically extracted and bound to a ThreadLocal. This process is entirely completed through bytecode redefinition during application startup or runtime, without modifying any business controller code, achieving transparent capture of request source information. A unified proxy mechanism at the JDBC layer eliminates the dependency on specific persistence layer frameworks. After the initialization of object management containers such as Spring, the `getConnection()` method of the underlying DataSource is directly intercepted, replacing the original database connection with a proxy object. When the proxied database connection creates a PreparedStatement or intercepts the execution of a method, the context information collected by the Web layer is read from the ThreadLocal of the current thread and dynamically injected into the SQL statement to be executed as SQL comments. A unique correspondence between HTTP requests and SQL execution was established. Subsequently, the enhanced SQL, parameters, execution time, and context were assembled into structured data and stored asynchronously. This enabled end-to-end, non-intrusive, and universal SQL execution tracing, thus solving the problem of how to achieve non-intrusive binding between HTTP request context and SQL execution behavior without modifying business code or relying on a specific persistence layer framework. Attached Figure Description
[0016] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments and descriptions of this application are used to explain this application and do not constitute an undue limitation of this application. In the drawings:
[0017] Figure 1 A flowchart illustrating an SQL execution tracing method according to an embodiment of this application is shown.
[0018] Figure 2 A structural block diagram of an SQL execution tracing apparatus provided according to an embodiment of this application is shown. Detailed Implementation
[0019] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. This application will now be described in detail with reference to the accompanying drawings and embodiments.
[0020] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0021] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate for the embodiments of this application described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0022] As described in the background section, existing Java Web applications (Java applications running on a web server) lack an intuitive relationship between HTTP requests and database SQL. Since SQL is typically scattered across persistence layer frameworks or business code, and the HTTP context exists only in the web layer, it is difficult to trace which specific HTTP request triggered the SQL during log analysis and performance diagnostics. Existing technologies (such as solutions relying on the internal logic of persistence layer frameworks) suffer from drawbacks such as high invasiveness, poor compatibility (difficulty adapting to multiple frameworks / data sources), inability to cover asynchronous thread switching scenarios, and high performance overhead in production environments. To address the problem of how to achieve non-intrusive binding between HTTP request context and SQL execution behavior without modifying business code or relying on a specific persistence layer framework implementation, embodiments of this application provide an SQL execution chain tracing method, apparatus, medium, and system.
[0023] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention.
[0024] This embodiment provides an SQL execution chain tracing method. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Also, although the logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0025] Figure 1This is a flowchart of the SQL execution chain tracing method according to an embodiment of this application. For example... Figure 1 As shown, the method includes the following steps:
[0026] Step S101: During the startup or operation of the Java Web application, bytecode enhancement technology is used to intercept the HTTP request processing flow of the Web layer, collect the request context information of the current HTTP request, and bind the above request context information to the thread local variable of the current processing thread.
[0027] Step S102: After the application object management container is initialized, the data source object in the application object management container is intercepted by proxy so that all database connection objects obtained through the data source are uniformly proxied.
[0028] Step S103: When the business logic creates a pre-compiled SQL execution object through the proxied database connection object, the creation process is intercepted, the request context information stored in the thread local variable is read, and the request context information is written into the SQL statement to be executed as a comment to generate the enhanced SQL statement.
[0029] Step S104: Pass the enhanced SQL statement to the JDBC driver for execution, and collect SQL parameter information and execution time information before or during execution. Construct the enhanced SQL statement, request context information, SQL parameter information and execution time information into structured data, and send it asynchronously to the storage system for centralized storage.
[0030] JDBC (Java Database Connectivity) is an application programming interface in the Java language that specifies how client programs access databases.
[0031] Bytecode enhancement technology refers to the technique of dynamically modifying, adding, or replacing the bytecode (i.e., instructions in the .class file) of a class during the Java Virtual Machine's loading of class files or during class execution, thereby changing the behavior or functionality of the class.
[0032] A Servlet is a small Java program that runs on a web server and is specifically designed to handle HTTP requests and generate HTTP responses.
[0033] A Filter is a pre- or post-processor for a Servlet. It can intercept and process requests or responses before they arrive at the Servlet or after the Servlet generates a response.
[0034] ThreadLocal provides each thread that uses the variable with an independent copy, allowing each thread to modify its own copy independently without affecting the copies of other threads.
[0035] DataSource: The data source object. It is typically configured and managed by the application server or framework (such as Spring), and encapsulates the detailed information of the database connection (username, password, driver class, etc.).
[0036] getConnection(): This is a method that requests a valid database connection object from the data source.
[0037] By leveraging bytecode enhancement technology, core methods of web components such as Servlets or Filters are dynamically intercepted during application runtime. When an HTTP request arrives, context information such as the request identifier and path is automatically extracted and bound to a ThreadLocal. This process is entirely completed through bytecode redefinition during application startup or runtime, without modifying any business controller code, achieving transparent capture of request origin information. A unified proxy mechanism at the JDBC layer eliminates dependencies on specific persistence layer frameworks. After object management containers like Spring are initialized, the `getConnection()` method of the underlying DataSource is directly intercepted, replacing the original database connection with a proxy object. When a PreparedStatement is created or a method execution is intercepted for the proxied database connection, context information collected by the web layer is read from the current thread's ThreadLocal and dynamically injected into the SQL statement to be executed as SQL comments. A unique correspondence between HTTP requests and SQL execution was established. Subsequently, the enhanced SQL, parameters, execution time, and context were assembled into structured data and stored asynchronously. This enabled end-to-end, non-intrusive, and universal SQL execution tracing, thus solving the problem of how to achieve non-intrusive binding between HTTP request context and SQL execution behavior without modifying business code or relying on a specific persistence layer framework.
[0038] In one embodiment of this application, collecting the request context information of the current HTTP request and binding the request context information to the thread local variable of the current processing thread includes: inserting interception logic at the entry method of the current HTTP request entering the application server, extracting the identification information, request path and request method of the current HTTP request as the request context information; and calling Java's thread local variable mechanism to store the extracted request context information in the thread local variable of the current processing thread.
[0039] By enhancing bytecode, interception logic is automatically inserted into the entry method without modifying any business logic, maintaining code purity. Leveraging the thread isolation features of thread-local variables (ThreadLocal), the request context is automatically shared across all levels within the same processing thread, avoiding parameter passing through multiple layers. This provides a data source for automatically retrieving this context and injecting SQL annotations at the JDBC layer, thereby establishing a unique correspondence between HTTP requests and specific SQL execution behaviors, enabling precise chain tracing.
[0040] In one embodiment of this application, the method further includes: during the asynchronous task creation or submission phase triggered by the HTTP request, intercepting the execution interface or submission method of the asynchronous task, and reading the request context information from the thread local variable of the current HTTP request thread; encapsulating the read request context information and passing it as a parameter or context attribute to the asynchronous task; when the asynchronous task is scheduled to a new asynchronous execution thread, inserting recovery logic at the front end of the asynchronous task execution logic, and writing the encapsulated request context information into the thread local variable corresponding to the new asynchronous execution thread, so that the asynchronous execution thread can perform enhanced binding of the SQL statement based on the request context information in the local variable of the asynchronous execution thread when performing database access operations.
[0041] Breaking away from the traditional limitation of ThreadLocal only supporting sharing within the same thread, it ensures that the context of HTTP requests can cross thread boundaries and be passed to asynchronous execution threads. It achieves unified tracking of SQL execution in both synchronous and asynchronous scenarios, preventing SQL generated by asynchronous tasks from being unable to be associated with the original request due to a lack of context. By intercepting asynchronous task submission and execution nodes, it automatically completes the extraction, passing, and restoration of context, eliminating the need for developers to manually pass context parameters between threads and maintaining the non-intrusiveness of business code.
[0042] In one embodiment of this application, the proxy interception of the data source object in the application object management container includes: listening to the Bean initialization event or data source creation event of the application object management container; when the data source object initialization is detected to be complete, obtaining the proxy object of the data source object; and overriding the method of obtaining the connection object of the proxied data object so that the method of obtaining the connection object returns a database connection proxy object wrapped by the proxy, which is used to intercept subsequent calls to create precompiled statement objects.
[0043] By using the getConnection method of the proxy data source, it ensures that all connections obtained through this data source within the application are proxy objects, thereby intercepting the creation process of all subsequent PreparedStatements and achieving indiscriminate coverage of SQL generated by various persistence layer frameworks.
[0044] In one embodiment of this application, the above-mentioned request context information is written into the SQL statement to be executed as a comment to generate an enhanced SQL statement. This includes: when the SQL execution object is a pre-compiled SQL execution object, obtaining the original SQL string template, constructing a comment string to identify the request source, and inserting the comment string into the head or a specified position of the original SQL string through an SQL comment syntax prefix to form an enhanced SQL string. The comment string includes the request tracking ID, request path, and timestamp information. When the SQL execution object is a non-pre-compiled SQL execution object or a non-SQL execution operation, a pass-through process is executed, and it is determined that no SQL content modification is required.
[0045] By injecting tracing information using SQL comment syntax, the enhanced SQL is ensured to be completely equivalent to the original SQL during database execution. This means it does not affect business logic or change the execution plan, significantly reducing security risks in the production environment. Precise insertion of comments containing tracing IDs, paths, and timestamps into the pre-compiled SQL provides a rich and structured metadata foundation for subsequent log analysis, performance bottleneck identification, and end-to-end tracing. By differentiating between pre-compiled and non-pre-compiled scenarios, non-target SQL execution is transparently passed, avoiding unnecessary string concatenation overhead while ensuring unified control and adaptation for all types of SQL execution behavior.
[0046] In one embodiment of this application, collecting SQL parameter information and execution time information includes: before the execution method is called, recording the current system timestamp as the start time; intercepting and obtaining the binding parameter values set for the SQL execution object to determine the SQL parameter information; after the execution method is called, recording the current system timestamp as the end time and calculating the execution time information; determining whether there is HTTP request context information in the local variables of the current processing thread; if so, marking the collected data as associated data; if not, marking it as independent execution data and performing release processing.
[0047] By accurately recording timestamps before and after execution and calculating execution time, combined with specific binding parameters obtained through interception, a leap from recording only SQL text to recording SQL instances (including parameters) has been achieved, providing accurate data for slow SQL location and parameter-level performance analysis. By determining the existence of the thread context, it automatically distinguishes between SQL triggered by HTTP requests and independent SQL triggered by background / scheduled tasks, ensuring that only SQL related to business requests is deeply associated and stored, avoiding interference from invalid data and reducing storage costs. Execution time, parameters, and request context are marked as associated data, forming a complete evidence chain of request → SQL → time → parameters, improving the efficiency and accuracy of problem troubleshooting. At the same time, a release mechanism ensures the efficient execution of non-associated SQL.
[0048] In one embodiment of this application, the method further includes: re-collecting the SQL execution status when the enhancement logic fails or is abnormal.
[0049] By re-collecting the SQL execution status, key execution information that might be lost in the main process (such as actual execution results and final execution time) is compensated for, ensuring that core tracing data remains complete and usable even in non-ideal operating environments.
[0050] The synchronous request binding triggers the SQL processing flow: Bytecode enhancement intercepts and extracts the request context (such as tracking ID, path, etc.), storing it in the local variables of the current thread. Subsequently, during the application container initialization phase, the data source object is intercepted by proxy, ensuring that the acquired database connection is also a proxy object. When the business logic creates a pre-compiled SQL execution object through the proxy connection, the creation process is intercepted, the request context is read from the thread-local variables, and injected as a comment into the header of the original SQL statement to generate enhanced SQL. This enhanced SQL is executed via JDBC, with interception before and after execution to collect parameters and execution time information. Finally, structured data containing the request context, SQL content, and execution time is asynchronously sent to the search engine storage system, achieving correlation tracing between HTTP requests and SQL execution.
[0051] The asynchronous request binding triggers SQL processing flow as follows: First, during the asynchronous task creation or submission phase, the execution or submission method is intercepted, and the interface is re-encapsulated. The request context information stored in the thread-local variables of the currently processing HTTP request is read and retrieved, and temporarily stored or encapsulated. Subsequently, when the asynchronous task is scheduled to the asynchronous execution thread, the temporarily stored or encapsulated request context information is restored to the thread-local variables corresponding to the asynchronous execution thread, ensuring that the asynchronous execution thread has the request context information corresponding to the HTTP request before executing database operations. In the asynchronous execution thread, when a pre-compiled SQL execution object is created through the database connection object, the restored request context information from the thread-local variables is read, and this information is written into the SQL statement, thereby generating an enhanced SQL statement associated with the HTTP request.
[0052] It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.
[0053] This application also provides an SQL execution link tracing device. It should be noted that the SQL execution link tracing device of this application can be used to execute the SQL execution link tracing method provided in this application. This device is used to implement the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0054] The following describes the SQL execution link tracing device provided in the embodiments of this application.
[0055] Figure 2 This is a structural block diagram of an SQL execution link tracing device according to an embodiment of this application. Figure 2As shown, the device includes: a first processing unit 21, used to intercept the HTTP request processing flow of the Web layer using bytecode enhancement technology during the startup or operation of a Java Web application, collect the request context information of the current HTTP request, and bind the request context information to the thread local variable of the current processing thread; a second processing unit 22, used to intercept the data source object in the application object management container after the application object management container is initialized, so that all database connection objects obtained through the data source are uniformly proxied; a third processing unit 23, used to intercept the creation process when the business logic creates a pre-compiled SQL execution object through the proxied database connection object, read the request context information stored in the thread local variable, and write the request context information into the SQL statement to be executed in the form of comments to generate an enhanced SQL statement; and a fourth processing unit 24, used to pass the enhanced SQL statement to the JDBC driver for execution, and collect SQL parameter information and execution time information before or during execution, construct the enhanced SQL statement, request context information, SQL parameter information, and execution time information into structured data, and asynchronously send it to the storage system for centralized storage.
[0056] In one embodiment of this application, the first processing unit includes: a first processing module for inserting interception logic at the entry method of the current HTTP request entering the application server, and extracting the identification information, request path and request method of the current HTTP request as the request context information; and a second processing module for calling Java's thread-local variable mechanism to store the extracted request context information in the thread-local variable of the current processing thread.
[0057] In one embodiment of this application, the apparatus further includes: a fifth processing unit configured to intercept the execution interface or submission method of the asynchronous task during the asynchronous task creation or submission phase triggered by the HTTP request, and read the request context information from the thread local variable of the current HTTP request thread; a sixth processing unit configured to encapsulate the read request context information and pass it as a parameter or context attribute to the asynchronous task; and a seventh processing unit configured to insert recovery logic at the front end of the asynchronous task execution logic when the asynchronous task is scheduled to a new asynchronous execution thread, and write the encapsulated request context information into the thread local variable corresponding to the new asynchronous execution thread, so that the asynchronous execution thread can perform enhanced binding of the SQL statement based on the request context information in the local variable of the asynchronous execution thread when performing database access operations.
[0058] In one embodiment of this application, the second processing unit includes: a third processing module for listening to the Bean initialization event or data source creation event of the application object management container; a fourth processing module for obtaining a proxy object of the data source object when the initialization of the data source object is detected to be complete; and a fifth processing module for overriding the method of obtaining the connection object of the proxied data object so that the method of obtaining the connection object returns a database connection proxy object wrapped by the proxy, wherein the database connection proxy object is used to intercept subsequent calls to create a precompiled statement object.
[0059] In one embodiment of this application, the third processing unit includes: a sixth processing module, which, when the SQL execution object is a pre-compiled SQL execution object, obtains the original SQL string template, constructs a comment string to identify the request source, and inserts the comment string into the head or a specified position of the original SQL string through an SQL comment syntax prefix to form an enhanced SQL string, wherein the comment string includes a request tracking ID, a request path, and timestamp information; and a seventh processing module, which, when the SQL execution object is a non-pre-compiled SQL execution object or a non-SQL execution operation, executes a pass-through process and determines that no SQL content modification is required.
[0060] In one embodiment of this application, the fourth processing unit includes: an eighth processing module for recording the current system timestamp as the start time before executing the method call; a ninth processing module for intercepting and obtaining the binding parameter values set for the SQL execution object to determine the SQL parameter information; a tenth processing module for recording the current system timestamp as the end time after executing the method call and calculating the execution time information; an eleventh processing module for determining whether HTTP request context information exists in the local variables of the current processing thread, and if so, marking the collected data as associated data; and a twelfth processing module for marking the data as independent execution data and performing release processing if the data does not exist.
[0061] In one embodiment of this application, the above-mentioned apparatus further includes: a collection unit for re-collecting the SQL execution status when the enhanced logic fails or is abnormal.
[0062] The aforementioned SQL execution link tracing device includes a processor and a memory. The first processing unit, second processing unit, third processing unit, and fourth processing unit are all stored as program units in the memory. The processor executes the program units stored in the memory to achieve the corresponding functions. All of the above modules are located in the same processor; alternatively, the modules may be located in different processors in any combination.
[0063] The processor contains a kernel, which retrieves the corresponding program units from memory. One or more kernels can be configured, and adjusting kernel parameters can solve the problem of how to achieve a non-intrusive binding between HTTP request context and SQL execution behavior without modifying business logic or relying on a specific persistence layer framework.
[0064] The memory may include non-permanent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0065] This invention provides a computer-readable storage medium that includes a stored program, wherein the program, when running, controls the device where the computer-readable storage medium is located to execute the SQL execution tracing method.
[0066] This invention provides a processor for running a program, wherein the program executes the SQL execution tracing method during runtime.
[0067] This invention provides a device including a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, it performs at least the following steps: During the startup or operation of a Java Web application, bytecode enhancement technology is used to intercept the HTTP request processing flow of the Web layer, collect the request context information of the current HTTP request, and bind the request context information to the thread-local variable of the current processing thread; after the application object management container is initialized, the data source object in the application object management container is intercepted by proxy, so that all database connection objects obtained through the data source are uniformly proxied; when the business logic creates a pre-compiled SQL execution object through the proxied database connection object, the creation process is intercepted, the request context information stored in the thread-local variable is read, and the request context information is written as a comment into the SQL statement to be executed, generating an enhanced SQL statement; the enhanced SQL statement is passed to the JDBC driver for execution, and SQL parameter information and execution time information are collected before or during execution; the enhanced SQL statement, request context information, SQL parameter information, and execution time information are constructed into structured data and asynchronously sent to the storage system for centralized storage. The devices mentioned in this article can be servers, PCs, tablets, mobile phones, etc.
[0068] This application also provides a computer program product, which, when executed on a data processing device, is suitable for executing an initialization program with at least the following method steps: During the startup or operation of a Java Web application, bytecode enhancement technology is used to intercept the HTTP request processing flow of the Web layer, collect the request context information of the current HTTP request, and bind the request context information to the thread local variable of the current processing thread; after the application object management container is initialized, the data source object in the application object management container is intercepted by proxy so that all database connection objects obtained through the data source are uniformly proxied; when the business logic creates a pre-compiled SQL execution object through the proxied database connection object, the creation process is intercepted, the request context information stored in the thread local variable is read, and the request context information is written into the SQL statement to be executed as a comment to generate an enhanced SQL statement; the enhanced SQL statement is passed to the JDBC driver for execution, and SQL parameter information and execution time information are collected before or during execution; the enhanced SQL statement, request context information, SQL parameter information, and execution time information are constructed into structured data and asynchronously sent to the storage system for centralized storage.
[0069] This application also provides an SQL execution tracing system, comprising: one or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include methods for performing any of the methods described above.
[0070] It is obvious to those skilled in the art that the modules or steps of the present invention described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. They can be implemented using computer-executable program code, and thus can be stored in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those described herein, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the present invention is not limited to any particular combination of hardware and software.
[0071] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0072] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0073] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0074] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0075] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0076] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, like read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0077] Computer-readable media include both permanent and non-permanent, removable and non-removable media that can store information by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0078] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0079] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0080] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.
Claims
1. A method for SQL execution link trace, characterized in that, include: During the startup or operation of a Java Web application, bytecode enhancement technology is used to intercept the HTTP request processing flow of the Web layer, collect the request context information of the current HTTP request, and bind the request context information to the thread local variable of the current processing thread. After the application object management container is initialized, the data source objects in the application object management container are intercepted by proxy so that all database connection objects obtained through the data source are uniformly proxied. When the business logic creates a pre-compiled SQL execution object through the proxied database connection object, the creation process is intercepted, the request context information stored in the thread local variable is read, and the request context information is written into the SQL statement to be executed as a comment to generate an enhanced SQL statement. The enhanced SQL statement is passed to the JDBC driver for execution. Before or during execution, SQL parameter information and execution time information are collected. The enhanced SQL statement, request context information, SQL parameter information and execution time information are constructed into structured data and sent asynchronously to the storage system for centralized storage.
2. The method according to claim 1, characterized in that, Collect the request context information of the current HTTP request and bind the request context information to the thread-local variable of the current processing thread, including: Interception logic is inserted at the entry point of the current HTTP request into the application server to extract the identification information, request path, and request method of the current HTTP request as the request context information. The Java thread-local variable mechanism is invoked to store the extracted request context information into the thread-local variable of the current processing thread.
3. The method according to claim 1, characterized in that, The method further includes: During the asynchronous task creation or submission phase triggered by an HTTP request, intercept the execution interface or submission method of the asynchronous task and read the request context information from the thread-local variable of the current HTTP request thread. The read request context information is encapsulated and passed as a parameter or context attribute to the asynchronous task; When the asynchronous task is scheduled to a new asynchronous execution thread, recovery logic is inserted at the front end of the asynchronous task execution logic. The encapsulated request context information is written into the thread local variable corresponding to the new asynchronous execution thread, so that the asynchronous execution thread can perform enhanced binding of the SQL statement based on the request context information in the local variable of the asynchronous execution thread when performing database access operations.
4. The method according to claim 1, characterized in that, Proxy interception of data source objects in the application object management container includes: Listen for Bean initialization events or data source creation events in the application object management container; When the initialization of the data source object is detected to be complete, the proxy object of the data source object is obtained; Override the method of obtaining the connection object of the proxied data object so that the method of obtaining the connection object returns a database connection proxy object wrapped by the proxy, which is used to intercept subsequent calls to create a precompiled statement object.
5. The method according to claim 1, characterized in that, The request context information is written as a comment into the SQL statement to be executed, generating an enhanced SQL statement, including: When the SQL execution object is a precompiled SQL execution object, the original SQL string template is obtained, a comment string for identifying the request source is constructed, and the comment string is inserted into the head or a specified position of the original SQL string through an SQL comment syntax prefix to form an enhanced SQL string. The comment string includes the request tracking ID, request path and timestamp information. If the SQL execution object is a non-precompiled SQL execution object or a non-SQL execution operation, execute the pass-through process and determine that no SQL content modification is required.
6. The method according to claim 1, characterized in that, Collect SQL parameter information and execution time information, including: Before executing the method call, record the current system timestamp as the start time; Intercept and obtain the binding parameter values set for the SQL execution object to determine the SQL parameter information; After the method call is executed, the current system timestamp is recorded as the end time, and the execution time information is calculated. Determine whether HTTP request context information exists in the local variables of the current processing thread. If it exists, mark the collected data as associated data; if it does not exist, mark it as independent execution data and perform the release process.
7. The method according to claim 1, characterized in that, The method further includes: When the enhanced logic fails or an exception occurs, the SQL execution status is re-collected.
8. An SQL execution tracing device, characterized in that, include: The first processing unit is used to intercept the HTTP request processing flow of the Web layer using bytecode enhancement technology during the startup or operation of a Java Web application, collect the request context information of the current HTTP request, and bind the request context information to the thread local variable of the current processing thread. The second processing unit is used to intercept the data source objects in the application object management container after the application object management container is initialized, so that all database connection objects obtained through the data source are uniformly proxied. The third processing unit is used to intercept the creation process when the business logic creates a pre-compiled SQL execution object through the proxied database connection object, read the request context information stored in the thread local variable, and write the request context information into the SQL statement to be executed in the form of comments to generate an enhanced SQL statement. The fourth processing unit is used to pass the enhanced SQL statement to the JDBC driver for execution, and collect SQL parameter information and execution time information before or during execution. The enhanced SQL statement, request context information, SQL parameter information and execution time information are constructed into structured data and sent asynchronously to the storage system for centralized storage.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored program, wherein, when the program is executed, it controls the device on which the computer-readable storage medium is located to perform the method according to any one of claims 1 to 7.
10. An SQL execution tracing system, characterized in that, include: One or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the one or more programs comprising methods for performing any one of claims 1 to 7.
