Summary of the invention
In order to solve the problems of the technologies described above, the embodiment of the present application provides a kind of method and apparatus of realizing the SQL database monitoring, to improve the execution efficient of proxy class when monitoring, simultaneously, guarantees in the runtime SQL database to be realized monitoring.
The embodiment of the present application discloses following technical scheme:
A kind of method that realizes the SQL database monitoring comprises: be used to obtain the driving time-like that database connects when virtual machine loads, change the method name of described driving class Central Plains method; Regenerate with described driving class in the identical method of former method name, and in the method that regenerates, call the former method of having changed the method name in the described driving class; The rreturn value of amending method is revised as the database connection object that obtains and acts on behalf of connecting object; At the connecting object of described former method, carry out the object of SQL and execution result set pair by described connecting object being used for of obtaining and resemble and add monitor code, generate corresponding agent object, so that realize the monitoring that SQL carries out by agent object.
Preferably, described method also comprises: load before the driving class of obtaining the database connection at virtual machine, read the monitoring configuration file, when monitoring is carried out in described monitoring configuration file indication, described driving class is revised as agency's driving class.
Preferably, when the open and close of the connection of SQL database or affairs was monitored, the connecting object of described driving class was Connection.
A kind of device of monitoring SQL database comprises: the method name is changed the unit, is used for being used to obtain the driving time-like that database connects when virtual machine loads, and changes the method name of described driving class Central Plains method; Generation unit is used for regenerating the identical method of former method name with described driving class, and calls the former method of having changed the method name in the described driving class in the method that regenerates; Adding device, be used in described former method connecting object, carry out the object of SQL and execution result set pair by described connecting object being used to of obtaining and resemble and add monitor code, generate corresponding agent object, so that realize the monitoring that SQL carries out by agent object.
Preferably, described device also comprises: reading unit, be used for before virtual machine state is carried out the driving class of SQL database operation, reading the monitoring configuration file, and when monitoring is carried out in described monitoring configuration file indication, described driving class is revised as the agency drives class.
Preferably, when the open and close of the connection of SQL database or affairs was monitored, the connecting object of described driving class was Connection.
As can be seen from the above-described embodiment, unlike the prior art, the embodiment of the present application on-the-fly modifies the driving class of database manipulation in the process that service application is carried out, and the database access associated class is acted on behalf of, and realizes the monitoring that database SQL is carried out.Therefore, have the following advantages:
The embodiment of the present application can be revised the application driven class by the bytecode injection mode in the runtime in the process that service application is carried out, do not adopt the reflection mode, has improved the execution efficient of proxy class when monitoring.Can under real operation system network environment, monitor, accurately reflect the performance of system the performance of SQL database.
On the basis of not changing existing service code, realize monitoring, finish the decoupling zero of watchdog routine and business procedure, not only reduced the influence of monitor procedure, reduced system risk existing system, and be convenient to the maintenance service program, need not to revise the driver package of operation system.
Embodiment
For above-mentioned purpose, the feature and advantage that make the application can become apparent more, the embodiment of the present application is described in detail below in conjunction with accompanying drawing.
Embodiment one
See also Fig. 2, it realizes the process flow diagram of an embodiment of the method that SQL database is monitored for the application is a kind of, and this method may further comprise the steps:
Step 201:, change the method name of described driving class Central Plains method when virtual machine loads the driving time-like that obtains database manipulation;
For example, in the business application system that adopts the Spring framework, when virtual machine loads the driving class DriverManagerDataSource that carries out database manipulation, the mode that adopts bytecode to inject is revised the method that all obtain connection, as with method name getConnectionFromDriverManager, be modified to getConnectionFromDriverManager_$Monitor.
Step 202: regenerate with described driving class in the identical method of former method name, and in the method that regenerates, call the former method of having changed the method name in the described driving class;
Be example still with the getConnectionFromDriver-Manager in the above-mentioned DriverManagerDataSource class, regenerate and the identical method of this former method name of getConnectionFromDriverManager, and in the method that regenerates, call the former method of having changed the method name in the DriverManagerDataSource class, promptly call getConnectionFromDriverManager_$Monitor.
Step 203: the rreturn value of amending method, be revised as the database connection object that obtains and act on behalf of connecting object;
Be example with the getConnectionFromDriver-Manager in the above-mentioned DriverManagerDataSource class still, Connection changes ProxyConnection into rreturn value.
Step 204: at the connecting object of described former method, carry out the object of SQL and execution result set pair by described connecting object being used for of obtaining and resemble and add monitor code, generate corresponding agent object, so that by acting on behalf of the monitoring that connecting object realizes that SQL carries out.
As being ProxyConnection with the Connection object modification, Statement is revised as ProxyStatement etc.
As can be seen from the above-described embodiment, unlike the prior art, the embodiment of the present application is revised and is obtained the driving class that database connects in the process that service application is carried out, and returns and acts on behalf of connecting object.Therefore, have the following advantages:
The embodiment of the present application can the monitoring to SQL database in the runtime in the process that service application is carried out.The mode that adopts bytecode to inject is finished the modification to the database-driven class, avoids adopting reflection way to call wherein former manner of execution, is implanted in the monitor code that drives in the class and carries out in proper order, has improved the execution efficient of proxy class when monitoring.
Can under real operation system network environment, monitor, accurately reflect the performance of system the performance of SQL database.
On the basis of not changing existing service code, realize monitoring, finish the decoupling zero of watchdog routine and business procedure, not only reduced the influence of monitor procedure, reduced system risk, and be convenient to the maintenance service program existing system.
Embodiment two
Present embodiment also provides a kind of method that realizes the SQL database monitoring, present embodiment is with the difference of embodiment one: virtual machine load obtain the driving class that database is connected before, read the monitoring configuration file, because having indicated, this monitoring configuration file whether needs to carry out monitoring, therefore, virtual machine, just will drive class and be revised as agency's driving class when needs are carried out monitoring according to the indication of monitoring configuration file.See also Fig. 3, it realizes the process flow diagram of another embodiment of the method that SQL database is monitored for the application is a kind of, and this method may further comprise the steps:
Step 301: virtual machine reads the monitoring configuration file;
Step 302: when monitoring is carried out in described monitoring configuration file indication, load the driving time-like that obtains the database connection, change the method name of described driving class Central Plains method at virtual machine;
Step 303: regenerate with described driving class in the identical method of former method name, and in the method that regenerates, call the former method of having changed the method name in the described driving class;
Step 304: at the connecting object of described former method, carry out the object of SQL and execution result set pair by described connecting object being used for of obtaining and resemble and add monitor code, generate corresponding agent object, so that realize the monitoring that SQL carries out by agent object.
Above-mentioned steps 302-step 304 is illustrated in embodiment one, so locate to repeat no more.
Need to prove that when monitoring was not carried out in the indication of monitoring configuration file, the virtual machine normal load was carried out the driving class of SQL database operation.
As can be seen from the above-described embodiment, unlike the prior art, the embodiment of the present application is added monitor code in the driving class of carrying out the SQL database operation in the process that service application is carried out, and realization will drive class and be revised as agency's driving class.Therefore, have the following advantages:
Whether the embodiment of the present application can be monitored SQL database according to the configuration file decision in the process that service application starts.The mode that adopts bytecode to inject is finished the modification to the database-driven class, need not to revise the database-driven bag of service application, avoids adopting reflection way to call wherein former manner of execution, has improved execution efficient.
Can under real operation system network environment, monitor, accurately reflect the performance of system the performance of SQL database.
On the basis of not changing existing service code, realize monitoring, finish the decoupling zero of watchdog routine and business procedure, not only reduced the influence of monitor procedure, reduced system risk, and be convenient to the maintenance service program existing system.
Embodiment three
Present embodiment provides a kind of driving class by proxy database to finish the method for SQL monitoring.See also Fig. 4, it realizes the process flow diagram of another embodiment of the method that SQL database is monitored for the application is a kind of, and this method may further comprise the steps:
Step 401: obtain the agency by the database-driven class of revising and connect class;
Step 402: the agency connects class and write down monitor message when finishing original linkage function, the execution object that obtains by agency's connection class for act on behalf of the Statement object (as, ProxyStatement);
Step 403: the agency carries out object ProxyStatement record monitor message when carrying out SQL;
Step 404: the proxy results set pair resembles ProxyResultSet record result set information.
As can be seen from the above-described embodiment, unlike the prior art, the embodiment of the present application is revised the database-driven class in the process that service application is carried out, the database connection of obtaining is revised as the agency connects class, finish the monitoring that SQL is carried out by the relevant proxy class of database manipulation.Therefore, have the following advantages:
The embodiment of the present application is in the process that service application is carried out, can monitor SQL in the runtime, the mode that adopts bytecode to inject is finished the modification to the database-driven class, avoid adopting reflection way to call wherein former manner of execution, be implanted in the monitor code that drives in the class and carry out in proper order, improved the execution efficient of proxy class when monitoring.
Can under real operation system network environment, monitor, accurately reflect the performance of system the performance of SQL database.
On the basis of not changing existing service code, realize monitoring, finish the decoupling zero of watchdog routine and business procedure, not only reduced the influence of monitor procedure, reduced system risk, and be convenient to the maintenance service program existing system.
Embodiment four
A kind ofly realize that the method for SQL database monitoring is corresponding with above-mentioned, the embodiment of the present application also provides a kind of device of realizing the SQL database monitoring.See also Fig. 5, it realizes an embodiment of the device that SQL database is monitored for the application is a kind of structural representation comprises: the method name is changed unit 501, generation unit 502 and adding device 503.Principle of work below in conjunction with this device is further introduced its inner structure and annexation.
The method name is changed unit 501, is used for loading the driving time-like of carrying out the SQL database operation when virtual machine, changes the method name of described driving class Central Plains method;
Generation unit 502 is used for regenerating the identical method of former method name with described driving class, and calls the former method of having changed the method name in the described driving class in the method that regenerates;
Adding device 503, be used in described former method connecting object, carry out the object of SQL and execution result set pair by described connecting object being used to of obtaining and resemble and add monitor code, generate corresponding agent object, so that realize the monitoring that SQL carries out by agent object.
Preferably, see also Fig. 6, it realizes the structural representation of an embodiment of the device that SQL database is monitored for the application is a kind of, as shown in Figure 6, this device also comprises: reading unit is used for reading the monitoring configuration file before virtual machine drives class, when monitoring is carried out in described monitoring configuration file indication, described driving class is revised as the agency drives class.
Preferably, when the open and close of the connection of SQL database or affairs was monitored, the connecting object of described driving class was Connection.
As can be seen from the above-described embodiment, unlike the prior art, the embodiment of the present application is revised the database-driven class in the process that service application is carried out, the database connection of obtaining is revised as the agency connects class, finish the monitoring that SQL is carried out by the relevant proxy class of database manipulation.Therefore, have the following advantages:
The embodiment of the present application is in the process that service application is carried out, can monitor SQL in the runtime, the mode that adopts bytecode to inject is finished the modification to the database-driven class, avoid adopting reflection way to call wherein former manner of execution, be implanted in the monitor code that drives in the class and carry out in proper order, improved the execution efficient of proxy class when monitoring.
Can under real operation system network environment, monitor, accurately reflect the performance of system the performance of SQL database.
On the basis of not changing existing service code, realize monitoring, finish the decoupling zero of watchdog routine and business procedure, not only reduced the influence of monitor procedure, reduced system risk, and be convenient to the maintenance service program existing system.
Need to prove, one of ordinary skill in the art will appreciate that all or part of flow process that realizes in the foregoing description method, be to instruct relevant hardware to finish by computer program, described program can be stored in the computer read/write memory medium, this program can comprise the flow process as the embodiment of above-mentioned each side method when carrying out.Wherein, described storage medium can be magnetic disc, CD, read-only storage memory body (Read-Only Memory, ROM) or at random store memory body (Random AccessMemory, RAM) etc.
More than a kind of method and apparatus of SQL database monitoring of realizing was described in detail to the application provides, used specific embodiment herein the application's principle and embodiment are set forth, the explanation of above embodiment just is used to help to understand the application's method and core concept thereof; Simultaneously, for one of ordinary skill in the art, according to the application's thought, the part that all can change in specific embodiments and applications, in sum, this description should not be construed as the restriction to the application.