WO2011134233A1 - Method and device for monitoring database connection pool - Google Patents

Method and device for monitoring database connection pool Download PDF

Info

Publication number
WO2011134233A1
WO2011134233A1 PCT/CN2010/077270 CN2010077270W WO2011134233A1 WO 2011134233 A1 WO2011134233 A1 WO 2011134233A1 CN 2010077270 W CN2010077270 W CN 2010077270W WO 2011134233 A1 WO2011134233 A1 WO 2011134233A1
Authority
WO
WIPO (PCT)
Prior art keywords
database connection
database
object instance
jdbc
class
Prior art date
Application number
PCT/CN2010/077270
Other languages
French (fr)
Chinese (zh)
Inventor
王萍
Original Assignee
中兴通讯股份有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 中兴通讯股份有限公司 filed Critical 中兴通讯股份有限公司
Publication of WO2011134233A1 publication Critical patent/WO2011134233A1/en

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/34Recording or statistical evaluation of computer activity, e.g. of down time, of input/output operation ; Recording or statistical evaluation of user activity, e.g. usability assessment
    • G06F11/3466Performance evaluation by tracing or monitoring
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2201/00Indexing scheme relating to error detection, to error correction, and to monitoring
    • G06F2201/80Database-specific techniques

Definitions

  • the present invention relates to the field of JAVA application technologies, and in particular, to a method and device for monitoring a database connection pool. Background technique
  • JAVA is a general term for the JAVA programming language and JAVA platform introduced by Sun Microsystems.
  • JAVA programming language (referred to as JAVA language) is an object-oriented, distributed, robust and secure, structure-neutral, portable, multi-threaded dynamic language.
  • the JAVA platform consists of a Java Virtual Machine (VM) and a Java Application Programming Interface (API).
  • the JAVA API provides a standard interface independent of the operating system for JAVA applications.
  • JAVA Data Base Connectivity JDBC, JAVA Data Base Connectivity
  • SQL Structured Query Language
  • Database access is one of its core technologies in JAVA-based applications, especially for enterprise users.
  • the database connection pool technology is introduced in the application system.
  • the basic idea of a database connection pool is to create a "buffer pool" for database connections.
  • the database connection pool will create a certain number of database connections into the database connection pool during initialization.
  • the number of database connections initially created is determined by the set minimum number of connections, and the maximum number of connections in the database connection pool is set to prevent the application system. An endless connection to the database.
  • the database connection pool caches the database connection, which can reduce the system resource overhead, but cannot monitor the database operations performed by each database connection.
  • the database connection is caused. An exception occurs in the pool.
  • the database connection pool can only catch exceptions, but it cannot locate which SQL statement is executed by which business.
  • the invention provides a method and a device for monitoring a database connection pool, which are used for tracking SQL statements executed by a database connection, thereby providing a basis for accurately and quickly realizing the problem location of an abnormal situation in the database connection pool.
  • a method for monitoring a database connection pool comprising:
  • JDBC JAVA database connection
  • SQL Structured Query Language
  • the statement tracks the variables and methods that need to be used to save all the object instances created by the database connection and their SQL statement tracking information.
  • the abnormality in the database connection pool is monitored. Obtaining all object instances created by the database connection; and
  • a monitoring device for a database connection pool comprising:
  • An obtaining unit configured to obtain a database connection from a database connection pool
  • a creating unit configured to create an object instance of a JAVA database connection JDBC interface implementation class for executing a structured query language SQL statement based on the obtained database connection, and implement the public parameter class definition inherited by the JDBC interface implementation class
  • the SQL statement keeps track of the variables and methods that need to be used, and saves all object instances created by the database connection and their SQL statement tracking information;
  • a monitoring unit configured to acquire all object instances created by the database connection when an abnormality occurs in the database connection pool is monitored during the execution of the database operation by using the obtained database connection;
  • an obtaining unit configured to obtain, for each object instance, the SQL statement tracking information of the object instance.
  • the method and device for monitoring database connection pool provided by the invention, real-time caches the related SQL statement tracking information in the process of performing database operations in each database connection, and obtains the above-mentioned SQL statement tracking information when an abnormal situation occurs in the database connection pool, thereby realizing
  • the tracking of the SQL statements executed by the database connection provides a basis for accurately and quickly realizing the problem location of the abnormal situation in the database connection execution database operation, and improves the stability of the database connection pool.
  • FIG. 1 is a schematic diagram of an internal class relationship of an improved database connection pool in an embodiment of the present invention
  • FIG. 2 is a schematic structural diagram of each key inner class of a database connection pool in an embodiment of the present invention
  • 3 is a flowchart of a method for creating a database connection according to an embodiment of the present invention
  • FIG. 4 is a flowchart of a method for monitoring a database connection pool according to an embodiment of the present invention
  • FIG. 5 is a schematic diagram of a method for creating an instance of a PoolPreparedStatement object according to an embodiment of the present invention
  • FIG. 6 is a block diagram of a monitoring device for a database connection pool according to an embodiment of the present invention. detailed description
  • the embodiment of the invention provides a method and a device for monitoring a database connection pool in a JAVA application, which is used for tracking the SQL statement executed by the database connection, so as to accurately and quickly locate the database connection pool in the JAVA application process.
  • the inventors have found that in order to implement the tracking of the SQL statements executed by the database connection, thereby providing a basis for accurately and quickly realizing the problem location of the abnormal situation in the database resource pool, and improving the stability of the database connection pool, the database connection pool needs to be performed. Improved, real-time cache of SQL statement tracking information in the process of performing database operations in each database connection, such as SQL statements executed by database connections in the database connection pool and execution time thereof, so that the abnormality can be accurately and quickly located in the event of an abnormal situation The exception caused by which SQL statement executed by which business.
  • JDBC is a JAVA API for executing SQL statements.
  • JDBC consists of a set of classes and interfaces written in the Java language.
  • the JDBC technology is also used to access the database, and the SQL statement executed by the database connection is tracked based on the interface provided by the JDBC.
  • PoolStatement The database connection pool internally implements the Statement class, inherits the public parameter class StatementUtil , and implements the JDBC standard database interface class Statement , that is, the PoolStatement is the JDBC standard database interface class Statement corresponding to the JDBC interface implementation class.
  • poolPreparedStatement The database connection pool internally implements the PreparedStatement class, inherits the JDBC interface implementation class PoolStatement, and implements the JDBC standard database interface class PreparedStatement (the JDBC standard database interface class PreparedStatement inherits the JDBC standard database interface class Statement), that is, the PoolPreparedStatement is the JDBC standard database.
  • poolCallableStatement The database connection pool internally implements the CallableStatement class, inherits the JDBC interface implementation class PoolPreparedStatement, and implements the JDBC standard database interface class CallableStatement (the JDBC standard database interface class CallableStatement inherits the JDBC standard database interface class PreparedStatement), that is, the PoolCallableStatement is the JDBC standard database.
  • the JDBC interface implementation class corresponding to the interface class CallableStatement.
  • PoolConnection Database connection pool internal definition Connection interface class, definition Get the database connection related information, such as the method of obtaining the database connection pool where the database connection is located, and obtain the database connection creation time.
  • PoolConnectionlmpl Database connection pool internal Connection implementation class, implement JDBC standard database interface class Connection and database connection pool internal definition Connection interface class PoolConnection, database connection pool cache a certain number
  • the PoolConnectionImpl object instance, that is, PoolConnectionImpl is the JDBC interface implementation class corresponding to the JDBC standard database interface class Connection.
  • createStatement JDBC standard database interface class Connection defined method, by calling this method can get the Statement object instance, and then through a series of execution methods to achieve database operations.
  • prepareStatementQ JDBC standard database interface class Connection defined method, by calling this method can get the PreparedStatement object instance, and then through a series of implementation methods to achieve database operations.
  • connectionPoolImpl Prior to this, several key elements of the connection pool implementation class ConnectionPoolImpl in the prior art are described:
  • the variable managed: ArrayList type is used to cache a database connection in the database connection pool, that is, a Connection object instance.
  • a certain number of PoolConnectionImpl object instances are cached.
  • newPooledConnection() This method is used to create a database connection, returning a PoolConnectionImpl object instance (equivalent to the Connection object instance in the normal connection pool;), the database connection pool will create the database connection into the variable managed cache.
  • getConection() This method is used to get the database connection for business use, and returns the PoolConnectionImpl object instance (equivalent to the Connection object instance in the normal connection pool).
  • Variable conn Connection object instance, used to complete various database operations.
  • ConnectionPoolImpl object instance used to save the database connection pool object to which the database connection belongs.
  • statementSet Set object instance, used to save the instance of the Statement object created by the database connection, is also an object instance of the StatementUtil subclass, in order to obtain the SQL statement of the database connection execution and SQL statement tracking information such as execution time.
  • Variable createTime The creation time of the database connection.
  • Variable lastCheckinTime The time the database connection was released after it was last used.
  • Variable lastCheckoutTime The time when the database connection was last acquired. It can be combined with lastCheckinTime to determine the usage time of the database connection and whether the usage is released.
  • addStatement(Statement) Adds an instance of the Statement object to the statementSet collection variable. This method is called when the database connection creates a Statement object instance.
  • poolStatement(Statement) used to create a PoolStatement object instance
  • the Statement object instance is taken as an input parameter and stored in the PoolStatement object instance. This method returns the created PoolStatement object instance.
  • poolPreparedStatement(PreparedStatement) used to create The PoolPreparedStatement object instance, the PreparedStatement object instance is taken as an input parameter, and saved in the PoolPreparedStatement object instance, which returns the created PoolPreparedStatement object instance.
  • poolCallableStatement(CallableStatement) Used to create a PoolCallableStatement object instance.
  • the CallableStatement object instance is used as an input parameter and saved in the PoolCallableStatement object instance. This method returns the created PoolCallableStatement object instance.
  • createStatementO Implements a method in the Connection interface, which is used to create a Statement object instance, and uses it as the input parameter poolStatementQ to create a PoolStatement object instance. At the same time, the method addStatement() is called, and finally the created PoolStatement object instance is returned.
  • prepareStatementO Implements the method in the Connection interface, which is used to create a PreparedStatement object instance, and uses it as the input method poolPreparedStatement() to create a PoolPreparedStatement object instance, and also calls the method addStatement() , which finally returns the created PoolPreparedStatement object instance.
  • variable pooledConnection the instance of the PoolConnectionlmpl object to which the instance object belongs.
  • Variable startExecTime The time when the Statement object instance starts executing database operations.
  • Variable putedParameters List type, used to save the parameters of the Statement object instance.
  • Variable executeFlag Execution flag, where executeFlag takes a value of 0 to indicate unexecuted, executeFlag takes a value of 1 to execute commonSql, executeFlag has a value of 2 indicates that preparedSql is executed, and executeFlag has a value of 3 to execute batchSql.
  • batchSql List type, used to save the SQL statement when executeBatch.
  • StatementUtilO The constructor of the StatementUtil object instance, which is a poolConnectionlmpl object instance and a Statement object instance, used to initialize the variable pooledConnection and the more statements
  • addParameter() Add the incoming parameters to the variable putedParameters.
  • setExecuteFlag() Sets the executeFlag flag to distinguish which SQL statement is currently executing.
  • appandBatchSQL() Add the SQL statement to batchSql and use this method when executeFlag is 3.
  • setPreparedSql() Set the preparedSql, use this method when executeFlag is 2.
  • setCommonSql() Set commonSql, use this method when executeFlag is 1.
  • executeOver() This method is called after the execution ends, clearing the executeFlag execution flag (set to 0, which means it is not executed) and startExecTime.
  • JDBC standard database interface class includes Connection, Statement . PreparedStatement . CallableStatement , where Connection defines the database connection, Statement, PreparedStatement, and CallableStatement define the variables and methods used to execute the SQL statement.
  • connection Through the improvement of the database connection pool, the JDBC standard database interface classes Connection, Statement, PreparedStatement, and CallableStatement correspond to the JDBC interface implementation classes PoolConnectionlmpl, PoolStatement, PoolPreparedStatement, and PoolCallableStatement respectively, that is, Connection and PoolConnectionlmpl correspond to Statement and PoolStatement, PreparedStatement and PoolPreparedStatement Correspondingly, CallableStatement corresponds to PoolCallableStatement.
  • the above JDBC interface implementation class can be used as a JDBC standard database interface class, and can also add personalized variables and methods, that is, the database connection pool caches the JDBC standard database interface class Connection corresponding to the JDBC interface implementation class PoolConnectionlmpl Object instance, the actual return of the PoolConnectionlmpl object instance when the business obtains the database connection.
  • the above improvements are transparent to the business, and the business uses the database connection as usual according to the JDBC standard database interface.
  • the business obtains the Statement, PreparedStatement, and CallableStatement object instances by calling the methods created by the JDBC standard database interface class Connection, createStatement(), prepare Statement(), and prepareCall()
  • the JDBC standard database interface class actually returned by the PoolConnectionlmpl object instance corresponds.
  • the object instance of the JDBC interface implementation class that is, the PoolStatement, PoolPreparedStatement, and PoolCallableStatement object instances, the above improvements are also transparent to the business, and the business performs database operations according to the JDBC standard database interface as usual.
  • the extension to the JDBC standard database interface class is reflected in two aspects: properties and inheritance.
  • Adding attributes that is, adding variables to the above JDBC interface implementation class, for saving phases
  • the object instance and its related information such as the JDBC interface implementation class PoolConnectionlmpl added variable conn (used to save the Connection object instance), pool (used to save the ConnectionPoolImpl object instance), etc.
  • JDBC interface implementation class PoolPreparedStatement in the booster port The variable preparedStatement (used to save the PreparedStatement object instance), the JDBC interface implementation class PoolCallableStatement added a variable callableStatement (used to save the CallableStatement object instance).
  • the JDBC interface implementation class PoolStatement, PoolPreparedStatement, PoolCallableStatement implements the JDBC standard database interface class Statement, PreparedStatement, CallableStatement, and the public parameter class StatementUtil
  • the public parameter class StatementUtil defines the variables and methods needed to implement SQL statement tracking.
  • the JDBC standard database interface class Statement belongs to the Connection object instance that is the database connection, the Statement object instance, the Statement object instance to start the database operation time, etc.
  • the internal Statement implementation class can be achieved by inheriting the public parameter class StatementUtil to achieve SQL statement tracking Variables and methods.
  • Each JDBC interface implementation class calls the method defined by the JDBC standard database interface class when implementing the JDBC standard database interface.
  • the database operation is finally implemented through the JDBC standard database interface, but the additional operation is added.
  • prepareStatement () execution flow is: first call conn.prepareStatement (sql), then return the PreparedStatement object instance and itself as a parameter to create a PoolPreparedStatement object instance , then save the SQL statement, and finally return the created PoolPreparedStatement object instance.
  • the implementation of other methods is similar to the implementation of the above method, and details are not described herein.
  • the database connection pool provided by the embodiment of the present invention is based on the improved database connection pool.
  • the monitoring method is described in detail.
  • the embodiment of the present invention first provides a method for creating a database connection in a database connection pool.
  • the implementation flowchart is as shown in FIG. 3, and includes the following steps:
  • the database connection conn and the database connection pool object are used as input functions of the poolConnectionlmpl to create a PoolConnectionlmpl object instance, that is, the database connection pConiio
  • the embodiment of the invention provides a method for monitoring a database connection pool. As shown in FIG. 4, the method includes the following steps:
  • the method Connection getConection() is used to obtain the PoolConnectionlmpl object instance, ie the database connection pconn, the method getConection() returns the PoolConnectionlmpl object instance, and the database connection conn is a variable in the database connection pconn.
  • pconn Based on the obtained database connection, pconn creates an object instance of a JDBC interface implementation class for executing the SQL statement.
  • the JDBC interface implementation class for executing the SQL statement includes a PoolStatement, a PoolPreparedStatement, and a PoolCallableStatement
  • the corresponding object instance includes a PoolStatement object instance, a PoolPreparedStatement object instance, and a PoolCallableStatement object instance
  • the database connection pconn is used to execute the SQL.
  • the object instance of the JDBC interface implementation class of the statement can be one of a PoolStatement object instance, a PoolPreparedStatement object instance, a PoolCallableStatement object instance, or any combination.
  • an implementation process of creating a PoolPreparedStatement object instance ppstat is taken as an example. For details, refer to FIG. 5.
  • the implementation process of creating a PoolStatement object instance and a PoolCallableStatement object instance is similar to the implementation process of creating a PoolPreparedStatement object instance, and is no longer Narration.
  • the object instance of the JDBC interface implementation class created by the obtained database connection pconn in S402 is the PoolPreparedStatement object instance ppstat, and in the implementation of S402, the method of calling the PoolPreparedStatement object instance ppstat setPreparedSql() , save the SQL statement executed by the PreparedStatement object instance; add the PoolPreparedStatement object instance ppstat to the statementSet collection variable by calling the method addStatement().
  • the statementSet collection variable is obtained by calling the method getStatementSet(), and the statementSet collection variable stores all the object instances created by the database connection;
  • the object instance is obtained by calling the getSQLString() and getExecTimeQ methods of each object instance.
  • the SQL statement trace information includes the SQL statement executed by the database connection and the execution time and other information, wherein the methods getSQLString() and getExecTime() are defined by the public parameter class StatementUtil.
  • the method further includes the following steps:
  • pconn refers to putting the database connection pconn back into the database connection pool.
  • the method may further include the following steps:
  • the obtained SQL statement tracking information of each object instance is printed in a log file for the user to locate the problem of the abnormal situation.
  • the method of creating a PoolPreparedStatement object instance includes the following steps:
  • an embodiment of the present invention provides a monitoring device for a database connection pool. As shown in FIG. 6, the device includes:
  • the obtaining unit 601 is configured to obtain a database connection from the database connection pool.
  • a creating unit 602 configured to create a SQL statement based on the obtained database connection
  • the JDBC interface implements the object instance of the class, and according to the public parameter class defined by the JDBC interface implementation class, implements the SQL statement to track the variables and methods to be used, saves all the object instances created by the database connection and its SQL statement tracking. information.
  • the monitoring unit 603 is configured to: when an abnormality occurs in the database connection pool is monitored during the database operation performed by using the obtained database connection, obtain all object instances created by the database connection.
  • the obtaining unit 604 is configured to obtain SQL statement tracking information of the object instance for each object instance.
  • the monitoring device of the database connection pool may further include:
  • the clear unit 605 is used to clear all the object instances created by the saved database connection when the database connection is released.
  • the monitoring device of the database connection pool may further include:
  • the printing unit 606 is configured to print the obtained SQL statement tracking information of each object instance in a log file, so that the user can locate the problem of the abnormal situation.
  • the method and device for monitoring the database connection pool provided by the embodiment of the present invention, real-time cache the related SQL statement tracking information in the process of performing database operations in each database connection, and obtain the above-mentioned SQL statement tracking information when an abnormality occurs in the database connection pool, thereby
  • the tracking of the SQL statements executed by the database connection is realized, which provides a basis for accurately and quickly realizing the problem location of the abnormal situation in the process of performing database operations in the database connection, and improves the stability of the database connection pool.
  • embodiments of the present invention can be provided as a method, apparatus, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or a combination of software and hardware. Moreover, the invention can be embodied in the form of one or more computer program products embodied on a computer-usable storage medium (including but not limited to disk storage, CD-ROM, optical storage, etc.) in which computer usable program code is embodied.
  • a computer-usable storage medium including but not limited to disk storage, CD-ROM, optical storage, etc.
  • 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 device to produce a machine for the execution of instructions for execution by a processor of a computer or other programmable data processing device.
  • the computer program instructions can also be stored in a computer readable memory that can direct a computer or other programmable data processing device to operate in a particular manner, such that the instructions stored in the computer readable memory produce an article of manufacture comprising the instruction device.
  • the apparatus implements the functions specified in one or more blocks of a flow or a flow and/or block diagram of the flowchart.

Abstract

A method and a device for monitoring a database connection pool are disclosed. The method includes: obtaining a database connection through a database connection pool. An object instance of a JAVA Database Connectivity (JDBC) interface implementing class is created based on the database connection, the JDBC interface implementing class is used for executing SQL statements. According to the implementing SQL statement defined by the public parameters class inheriting from the JDBC interface implementing class, the arguments and the method to be used are traced. All the object instances created by the database connection and the tracing information of the SQL statements thereof are stored. During the process of using the database connection to execute a database operation, when exceptions occurred in the monitored connection pool, all the object instances created by the database connection is obtained, and for each object instances, the tracing information of the SQL statement of the object instance is obtained.

Description

一种数据库连接池的监控方法及装置 技术领域  Method and device for monitoring database connection pool
本发明涉及 JAVA应用技术领域,尤其涉及一种数据库连接池的监控方 法及装置。 背景技术  The present invention relates to the field of JAVA application technologies, and in particular, to a method and device for monitoring a database connection pool. Background technique
JAVA是由 Sun Microsystems公司推出的 JAVA程序设计语言和 JAVA 平台的总称。 JAVA程序设计语言 (简称 JAVA语言)是一种面向对象的、 分布式的、健壮安全的、结构中立的、可移植的、 多线程的动态语言。 JAVA 平台由 JAVA虚拟机( VM, Virtual Machine )和 JAVA应用程序接口 (API, Application Programming Interface )构成。 JAVA API为 JAVA应用程序提供 独立于操作系统的标准接口,其中 JAVA数据库连接( JDBC, JAVA Data Base Connectivity )是用于执行结构化查询语言( SQL, Structured Query Language ) 语句的 JAVA API, JDBC由一组釆用 JAVA语言编写的类和接口组成, 可以 与数据库建立连接、 发送 SQL语句并处理执行结果。 SQL是一种数据库查 询的程序设计语言, 用于存取数据以及查询、 更新和管理关系数据库系统。  JAVA is a general term for the JAVA programming language and JAVA platform introduced by Sun Microsystems. JAVA programming language (referred to as JAVA language) is an object-oriented, distributed, robust and secure, structure-neutral, portable, multi-threaded dynamic language. The JAVA platform consists of a Java Virtual Machine (VM) and a Java Application Programming Interface (API). The JAVA API provides a standard interface independent of the operating system for JAVA applications. JAVA Data Base Connectivity (JDBC, JAVA Data Base Connectivity) is a JAVA API for executing structured query language (SQL, Structured Query Language) statements. The group consists of classes and interfaces written in the JAVA language. It can establish connections with the database, send SQL statements, and process execution results. SQL is a programming language for database queries that access data and query, update, and manage relational database systems.
在基于 JAVA的应用系统中,尤其是面向企业用户的应用系统,数据库 访问是其核心技术之一。 为了解决频繁访问数据库时存在的系统资源开销 大、 响应速度慢、 系统稳定性低、 内存泄漏等问题, 应用系统中引入了数 据库连接池技术。 数据库连接池的基本思想是为数据库连接建立一个 "緩 冲池"。 数据库连接池在初始化时将创建一定数量的数据库连接放入数据库 连接池中, 初始创建的数据库连接的数量由设定的最小连接数确定, 同时 设定数据库连接池的最大连接数以防止应用系统与数据库的无尽连接。 当 需要建立数据库连接时, 只需从数据库连接池中取出一个数据库连接, 使 用完毕之后再将该数据库连接放回数据库连接池即可, 既避免了数据库连 接的频繁建立、 关闭所带来的系统资源开销, 又使得每一个数据库连接得 以高效、 安全的复用。 Database access is one of its core technologies in JAVA-based applications, especially for enterprise users. In order to solve the problems of frequent system resource overhead, slow response, low system stability, and memory leaks when accessing the database frequently, the database connection pool technology is introduced in the application system. The basic idea of a database connection pool is to create a "buffer pool" for database connections. The database connection pool will create a certain number of database connections into the database connection pool during initialization. The number of database connections initially created is determined by the set minimum number of connections, and the maximum number of connections in the database connection pool is set to prevent the application system. An endless connection to the database. When you need to establish a database connection, you only need to take a database connection from the database connection pool, so that After the use is completed, the database connection can be put back into the database connection pool, which avoids the system resource overhead caused by frequent establishment and shutdown of the database connection, and enables efficient and secure reuse of each database connection.
现有技术中, 数据库连接池中緩存数据库连接, 能够减少系统资源开 销, 但是无法监控每个数据库连接所执行的数据库操作, 一旦业务使用数 据库连接的时间超长或者执行非法数据库操作会导致数据库连接池发生异 常情况, 数据库连接池只能捕获到异常情况的发生, 但是无法定位是由哪 个业务执行的哪个 SQL语句导致的异常。 为了避免大量日志带来的系统资 源开销, 也不可能在日志中记录各业务执行的所有 SQL语句, 使得运行多 个业务或者业务执行的 SQL语句较多时, 针对异常情况的问题定位尤其困 难。 发明内容  In the prior art, the database connection pool caches the database connection, which can reduce the system resource overhead, but cannot monitor the database operations performed by each database connection. Once the service uses the database connection for a long time or performs an illegal database operation, the database connection is caused. An exception occurs in the pool. The database connection pool can only catch exceptions, but it cannot locate which SQL statement is executed by which business. In order to avoid the system resource overhead caused by a large number of logs, it is also impossible to record all the SQL statements executed by each service in the log, so that it is particularly difficult to locate the problem of abnormal conditions when running multiple services or executing many SQL statements. Summary of the invention
本发明提供一种数据库连接池的监控方法及装置, 用以实现对数据库 连接执行的 SQL语句的跟踪, 从而为准确、 快速地实现数据库连接池中异 常情况的问题定位提供基础。  The invention provides a method and a device for monitoring a database connection pool, which are used for tracking SQL statements executed by a database connection, thereby providing a basis for accurately and quickly realizing the problem location of an abnormal situation in the database connection pool.
一种数据库连接池的监控方法, 该方法包括:  A method for monitoring a database connection pool, the method comprising:
从数据库连接池中获取数据库连接;  Obtain a database connection from the database connection pool;
基于获取到的数据库连接创建用于执行结构化查询语言( SQL )语句的 JAVA数据库连接 ( JDBC )接口实现类的对象实例, 并根据所述 JDBC接 口实现类所继承的公共参数类定义的实现 SQL语句跟踪需要使用的变量和 方法,保存所述数据库连接所创建的所有对象实例及其 SQL语句跟踪信息; 在使用获取到的数据库连接执行数据库操作的过程中, 监控到数据库 连接池中出现异常情况时, 获取所述数据库连接所创建的所有对象实例; 并  Creating an object instance of a JAVA database connection (JDBC) interface implementation class for executing a Structured Query Language (SQL) statement based on the obtained database connection, and implementing SQL according to the public parameter class defined by the JDBC interface implementation class The statement tracks the variables and methods that need to be used to save all the object instances created by the database connection and their SQL statement tracking information. During the execution of the database operation using the obtained database connection, the abnormality in the database connection pool is monitored. Obtaining all object instances created by the database connection; and
针对每一个对象实例, 获得所述对象实例的 SQL语句跟踪信息。 一种数据库连接池的监控装置, 该装置包括: For each object instance, the SQL statement tracking information of the object instance is obtained. A monitoring device for a database connection pool, the device comprising:
获取单元, 用于从数据库连接池中获取数据库连接;  An obtaining unit, configured to obtain a database connection from a database connection pool;
创建单元, 用于基于获取到的数据库连接创建用于执行结构化查询语 言 SQL语句的 JAVA数据库连接 JDBC接口实现类的对象实例, 并根据所 述 JDBC接口实现类所继承的公共参数类定义的实现 SQL语句跟踪需要使 用的变量和方法, 保存所述数据库连接所创建的所有对象实例及其 SQL语 句跟踪信息;  a creating unit, configured to create an object instance of a JAVA database connection JDBC interface implementation class for executing a structured query language SQL statement based on the obtained database connection, and implement the public parameter class definition inherited by the JDBC interface implementation class The SQL statement keeps track of the variables and methods that need to be used, and saves all object instances created by the database connection and their SQL statement tracking information;
监控单元, 用于在使用获取到的数据库连接执行数据库操作的过程中, 监控到数据库连接池中出现异常情况时, 获取所述数据库连接所创建的所 有对象实例;  a monitoring unit, configured to acquire all object instances created by the database connection when an abnormality occurs in the database connection pool is monitored during the execution of the database operation by using the obtained database connection;
获得单元, 用于针对每一个对象实例, 获得所述对象实例的 SQL语句 跟踪信息。  And an obtaining unit, configured to obtain, for each object instance, the SQL statement tracking information of the object instance.
本发明提供的数据库连接池的监控方法及装置, 实时緩存每一个数据 库连接执行数据库操作的过程中相关的 SQL语句跟踪信息, 在数据库连接 池出现异常情况时获得上述 SQL语句跟踪信息, 从而实现对数据库连接执 行的 SQL语句的跟踪, 为准确、 快速地实现数据库连接执行数据库操作的 过程中异常情况的问题定位提供基础, 提高数据库连接池的稳固性。  The method and device for monitoring database connection pool provided by the invention, real-time caches the related SQL statement tracking information in the process of performing database operations in each database connection, and obtains the above-mentioned SQL statement tracking information when an abnormal situation occurs in the database connection pool, thereby realizing The tracking of the SQL statements executed by the database connection provides a basis for accurately and quickly realizing the problem location of the abnormal situation in the database connection execution database operation, and improves the stability of the database connection pool.
本发明的其它特征和优点将在随后的说明书中阐述, 并且, 部分地从 说明书中变得显而易见, 或者通过实施本发明而了解。 本发明的目的和其 他优点可通过在所写的说明书、 权利要求书、 以及附图中所特别指出的结 构来实现和获得。 附图说明  Other features and advantages of the invention will be set forth in the description which follows, The objectives and other advantages of the invention will be realized and attained by the <RTI DRAWINGS
图 1为本发明实施例中改进后的数据库连接池内部类关系示意图; 图 2为本发明实施例中改进后的数据库连接池各关键内部类的结构示 意图; 图 3为本发明实施例中创建数据库连接的方法流程图; 1 is a schematic diagram of an internal class relationship of an improved database connection pool in an embodiment of the present invention; FIG. 2 is a schematic structural diagram of each key inner class of a database connection pool in an embodiment of the present invention; 3 is a flowchart of a method for creating a database connection according to an embodiment of the present invention;
图 4为本发明实施例中数据库连接池的监控方法流程图;  4 is a flowchart of a method for monitoring a database connection pool according to an embodiment of the present invention;
图 5为本发明实施例中创建 PoolPreparedStatement对象实例的方法; 图 6为本发明实施例中数据库连接池的监控装置框图。 具体实施方式  FIG. 5 is a schematic diagram of a method for creating an instance of a PoolPreparedStatement object according to an embodiment of the present invention; FIG. 6 is a block diagram of a monitoring device for a database connection pool according to an embodiment of the present invention. detailed description
本发明实施例提供一种 JAVA应用程序中的数据库连接池的监控方法 及装置, 用以实现对数据库连接执行的 SQL语句的跟踪, 从而为准确、 快 速的定位在 JAVA应用过程中数据库连接池中出现的异常情况提供基础。  The embodiment of the invention provides a method and a device for monitoring a database connection pool in a JAVA application, which is used for tracking the SQL statement executed by the database connection, so as to accurately and quickly locate the database connection pool in the JAVA application process. The basis for anomalies that arise.
以下结合说明书附图对本发明的优选实施例进行说明, 应当理解, 此 处所描述的优选实施例仅用于说明和解释本发明, 并不用于限定本发明, 合。  The preferred embodiments of the present invention are described with reference to the accompanying drawings, and the preferred embodiments described herein are intended to illustrate and explain the invention.
本发明人发现, 为了实现对数据库连接执行的 SQL语句的跟踪, 从而 为准确、 快速地实现数据库资源池中异常情况的问题定位提供基础, 提高 数据库连接池的稳固性, 需要对数据库连接池进行改进, 实时緩存各数据 库连接执行数据库操作的过程中的 SQL语句跟踪信息, 例如数据库连接池 中各数据库连接所执行的 SQL语句及其执行时间, 以便在发生异常情况时 能够准确、 快速地定位出由哪个业务执行的哪个 SQL语句导致的异常。  The inventors have found that in order to implement the tracking of the SQL statements executed by the database connection, thereby providing a basis for accurately and quickly realizing the problem location of the abnormal situation in the database resource pool, and improving the stability of the database connection pool, the database connection pool needs to be performed. Improved, real-time cache of SQL statement tracking information in the process of performing database operations in each database connection, such as SQL statements executed by database connections in the database connection pool and execution time thereof, so that the abnormality can be accurately and quickly located in the event of an abnormal situation The exception caused by which SQL statement executed by which business.
在基于 JAVA的应用系统中, JDBC是用于执行 SQL语句的 JAVA API, JDBC由一组釆用 JAVA语言编写的类和接口组成。 本发明实施例中, 同样 使用 JDBC技术访问数据库,基于 JDBC提供的接口实现对数据库连接执行 的 SQL语句的跟踪。  In a JAVA-based application system, JDBC is a JAVA API for executing SQL statements. JDBC consists of a set of classes and interfaces written in the Java language. In the embodiment of the present invention, the JDBC technology is also used to access the database, and the SQL statement executed by the database connection is tracked based on the interface provided by the JDBC.
改进后的数据库连接池内部类关系示意图请参见图 1 (在图 1中 JDBC 标准数据库接口类简称 JDBC接口类), 下面结合说明书附图 1对改进后的 数据库连接池中各内部类及其相互关系进行说明, 其中: [1] StatementUtil: 公共参数类, 用于定义实现 SQL语句跟踪需要使用 的变量和方法, 包括 JDBC标准数据库接口类 Statement所属的 Connection 对象实例即数据库连接、 Statement对象实例、 Statement对象实例开始执行 数据库操作即 SQL语句的时间等等, 公共参数类 StatementUtil是改进后的 数据库连接池中其他内部 Statement实现类的基类。 See Figure 1 for the improved internal relationship of the database connection pool (in the JDBC standard database interface class referred to as JDBC interface class in Figure 1), the internal classes in the improved database connection pool and their mutuals are described below with reference to Figure 1 of the specification. The relationship is explained, where: [1] StatementUtil: Public parameter class, used to define the variables and methods needed to implement SQL statement tracking, including the Connection object instance of the JDBC standard database interface class Statement, ie the database connection, the Statement object instance, the Statement object instance to start the database operation That is, the time of the SQL statement, etc., the public parameter class StatementUtil is the base class of other internal Statement implementation classes in the improved database connection pool.
[2] PoolStatement: 数据库连接池内部实现 Statement类, 继承公共参数 类 StatementUtil , 并实现 JDBC 标准数据库接口类 Statement , 即 PoolStatement是 JDBC标准数据库接口类 Statement对应的 JDBC接口实现 类。  [2] PoolStatement: The database connection pool internally implements the Statement class, inherits the public parameter class StatementUtil , and implements the JDBC standard database interface class Statement , that is, the PoolStatement is the JDBC standard database interface class Statement corresponding to the JDBC interface implementation class.
[3] PoolPreparedStatement: 数据库连接池内部实现 PreparedStatement 类, 继承 JDBC接口实现类 PoolStatement, 并实现 JDBC标准数据库接口 类 PreparedStatement( JDBC标准数据库接口类 PreparedStatement继承 JDBC 标准数据库接口类 Statement ), 即 PoolPreparedStatement是 JDBC标准数据 库接口类 PreparedStatement对应的 JDBC接口实现类。  [3] PoolPreparedStatement: The database connection pool internally implements the PreparedStatement class, inherits the JDBC interface implementation class PoolStatement, and implements the JDBC standard database interface class PreparedStatement (the JDBC standard database interface class PreparedStatement inherits the JDBC standard database interface class Statement), that is, the PoolPreparedStatement is the JDBC standard database. The JDBC interface implementation class corresponding to the interface class PreparedStatement.
[4] PoolCallableStatement:数据库连接池内部实现 CallableStatement类, 继承 JDBC接口实现类 PoolPreparedStatement, 并实现 JDBC标准数据库接 口类 CallableStatement ( JDBC 标准数据库接口类 CallableStatement 继承 JDBC标准数据库接口类 PreparedStatement ), 即 PoolCallableStatement是 JDBC标准数据库接口类 CallableStatement对应的 JDBC接口实现类。  [4] PoolCallableStatement: The database connection pool internally implements the CallableStatement class, inherits the JDBC interface implementation class PoolPreparedStatement, and implements the JDBC standard database interface class CallableStatement (the JDBC standard database interface class CallableStatement inherits the JDBC standard database interface class PreparedStatement), that is, the PoolCallableStatement is the JDBC standard database. The JDBC interface implementation class corresponding to the interface class CallableStatement.
[5] PoolConnection: 数据库连接池内部定义 Connection接口类, 定义 获取数据库连接相关信息的方法, 例如获取数据库连接所在数据库连接池 的方法, 获取数据库连接的创建时间的方法。  [5] PoolConnection: Database connection pool internal definition Connection interface class, definition Get the database connection related information, such as the method of obtaining the database connection pool where the database connection is located, and obtain the database connection creation time.
[6] PoolConnectionlmpl: 数据库连接池内部 Connection实现类, 实现 JDBC标准数据库接口类 Connection和数据库连接池内部定义 Connection 接 口 类 PoolConnection , 数据库连接池 中 緩存一定数量的 PoolConnectionImpl对象实例, 即 PoolConnectionImpl是 JDBC标准数据库 接口类 Connection对应的 JDBC接口实现类。 [6] PoolConnectionlmpl: Database connection pool internal Connection implementation class, implement JDBC standard database interface class Connection and database connection pool internal definition Connection interface class PoolConnection, database connection pool cache a certain number The PoolConnectionImpl object instance, that is, PoolConnectionImpl is the JDBC interface implementation class corresponding to the JDBC standard database interface class Connection.
[7] getConection(): 数据库连接池中的基本方法, 通过调用该方法能够 获取数据库连接, 改进后的数据库连接池中, 该方法返回的是 PoolConnectionImpl对象实例, 由于 PoolConnectionImpl实现了 JDBC标准 数据库接口, 因此对业务来说仍然使用 Connection对象实例的方法。  [7] getConection(): The basic method in the database connection pool. By calling this method, the database connection can be obtained. In the improved database connection pool, the method returns the PoolConnectionImpl object instance. Since PoolConnectionImpl implements the JDBC standard database interface, Therefore, the method of the Connection object instance is still used for the business.
[8] createStatement(): JDBC标准数据库接口类 Connection定义的方法, 通过调用该方法能够获得 Statement对象实例, 进而通过一系列执行方法实 现数据库操作。  [8] createStatement (): JDBC standard database interface class Connection defined method, by calling this method can get the Statement object instance, and then through a series of execution methods to achieve database operations.
[9] prepareStatementQ: JDBC标准数据库接口类 Connection定义的方 法, 通过调用该方法能够获得 PreparedStatement对象实例, 进而通过一系 列执行方法实现数据库操作。  [9] prepareStatementQ: JDBC standard database interface class Connection defined method, by calling this method can get the PreparedStatement object instance, and then through a series of implementation methods to achieve database operations.
[ 10] prepareCall(): JDBC标准数据库接口类 Connection定义的方法, 通过调用该方法能够获得 CallableStatement对象实例,进而通过一系列执行 方法实现数据库操作。  [10] prepareCall (): JDBC standard database interface class Connection defined method, by calling this method can get the CallableStatement object instance, and then through a series of execution methods to achieve database operations.
改进后的数据库连接池中各关键内部类的结构示意图如图 2所示, 下 面对改进后的数据库连接池中几个关键内部类的关键元素进行说明。  The structure diagram of each key inner class in the improved database connection pool is shown in Figure 2. The key elements of several key inner classes in the improved database connection pool are described below.
在此之前,对现有技术中数据库连接池实现类 ConnectionPoolImpl的几 个关键元素进行说明:  Prior to this, several key elements of the connection pool implementation class ConnectionPoolImpl in the prior art are described:
1 )变量 managed: ArrayList类型, 用于緩存数据库连接池中的数据库 连接, 即 Connection对象实例; 相应的, 本发明实施例中緩存一定数量的 PoolConnectionImpl对象实例。  1) The variable managed: ArrayList type is used to cache a database connection in the database connection pool, that is, a Connection object instance. Correspondingly, in the embodiment of the present invention, a certain number of PoolConnectionImpl object instances are cached.
2 ) newPooledConnection(): 该方法用于创建数据库连接, 返回 PoolConnectionImpl对象实例 (等同于普通连接池中的 Connection对象实 例;), 数据库连接池将创建的数据库连接放入变量 managed中緩存。 3 ) getConection() : 该方法用于获取数据库连接供业务使用, 返回 PoolConnectionImpl对象实例 (等同于普通连接池中的 Connection对象实 例 )。 2) newPooledConnection(): This method is used to create a database connection, returning a PoolConnectionImpl object instance (equivalent to the Connection object instance in the normal connection pool;), the database connection pool will create the database connection into the variable managed cache. 3) getConection() : This method is used to get the database connection for business use, and returns the PoolConnectionImpl object instance (equivalent to the Connection object instance in the normal connection pool).
在现有数据库连接池实现类 ConnectionPoolImpl的基础上,对改进后的 数据库连接池中内部 Connection实现类 PoolConnectionImpl的关键元素进 行说明:  Based on the existing database connection pool implementation class ConnectionPoolImpl, the key elements of the internal Connection implementation class PoolConnectionImpl in the improved database connection pool are described:
1 ) 变量 conn: Connection对象实例, 用于完成各种数据库操作。  1) Variable conn: Connection object instance, used to complete various database operations.
2 ) 变量 pool: ConnectionPoolImpl对象实例, 用于保存该数据库连接 所属的数据库连接池对象。  2) Variable pool: ConnectionPoolImpl object instance, used to save the database connection pool object to which the database connection belongs.
3 ) statementSet: Set对象实例,用于保存数据库连接所创建的 Statement 对象实例, 也是 StatementUtil子类的对象实例, 以便从中获取该数据库连 接执行的 SQL语句以及执行时间等 SQL语句跟踪信息。  3) statementSet: Set object instance, used to save the instance of the Statement object created by the database connection, is also an object instance of the StatementUtil subclass, in order to obtain the SQL statement of the database connection execution and SQL statement tracking information such as execution time.
4 ) 变量 createTime: 数据库连接的创建时间。  4) Variable createTime: The creation time of the database connection.
5 ) 变量 lastCheckinTime: 数据库连接上次被使用之后释放的时间。 6 ) 变量 lastCheckoutTime: 数据库连接上次被获取的时间, 可以结合 lastCheckinTime确定数据库连接的使用时间以及本次使用是否被释放。  5) Variable lastCheckinTime: The time the database connection was released after it was last used. 6) Variable lastCheckoutTime: The time when the database connection was last acquired. It can be combined with lastCheckinTime to determine the usage time of the database connection and whether the usage is released.
7 ) addStatement(Statement): 将入参 Statement 对象实例添力口到 statementSet集合变量中, 数据库连接创建 Statement对象实例时调用该方 法。  7) addStatement(Statement): Adds an instance of the Statement object to the statementSet collection variable. This method is called when the database connection creates a Statement object instance.
8 ) remove AllStatements(): 清空 statementSet中的所有 Statement对象 实例, 数据库连接被释放时调用该方法。  8) remove AllStatements(): Clears all Statement object instances in the statementSet, which is called when the database connection is released.
9 ) poolStatement(Statement): 用于创建 PoolStatement 对象实例, 9) poolStatement(Statement): used to create a PoolStatement object instance,
Statement对象实例作为入参, 并保存在 PoolStatement对象实例中, 该方法 返回创建的 PoolStatement对象实例。 The Statement object instance is taken as an input parameter and stored in the PoolStatement object instance. This method returns the created PoolStatement object instance.
10 ) poolPreparedStatement(PreparedStatement) : 用 于 创 建 PoolPreparedStatement对象实例 , PreparedStatement对象实例作为入参, 并 保存在 PoolPreparedStatement 对象实例 中 , 该方法返回创建的 PoolPreparedStatement对象实例。 10) poolPreparedStatement(PreparedStatement) : used to create The PoolPreparedStatement object instance, the PreparedStatement object instance is taken as an input parameter, and saved in the PoolPreparedStatement object instance, which returns the created PoolPreparedStatement object instance.
11 ) poolCallableStatement(CallableStatement) : 用 于 创 建 PoolCallableStatement对象实例, CallableStatement对象实例作为入参, 并 保存在 PoolCallableStatement 对象实例中 , 该方法返回创建的 PoolCallableStatement对象实例。  11) poolCallableStatement(CallableStatement): Used to create a PoolCallableStatement object instance. The CallableStatement object instance is used as an input parameter and saved in the PoolCallableStatement object instance. This method returns the created PoolCallableStatement object instance.
12 ) createStatementO: 实现 Connection 接口中的方法, 用于创建 Statement 对象实例, 并将其作为入参调用方法 poolStatementQ以创建 PoolStatement对象实例, 同时调用方法 addStatement() , 最终返回创建的 PoolStatement对象实例。  12) createStatementO: Implements a method in the Connection interface, which is used to create a Statement object instance, and uses it as the input parameter poolStatementQ to create a PoolStatement object instance. At the same time, the method addStatement() is called, and finally the created PoolStatement object instance is returned.
13 ) prepareStatementO: 实现 Connection接口中的方法, 用于创建 PreparedStatement 对 象 实 例 , 并 将 其 作 为 入 参 调 用 方 法 poolPreparedStatement()以创建 PoolPreparedStatement对象实例 ,同时调用方 法 addStatement() , 最终返回创建的 PoolPreparedStatement对象实例。  13) prepareStatementO: Implements the method in the Connection interface, which is used to create a PreparedStatement object instance, and uses it as the input method poolPreparedStatement() to create a PoolPreparedStatement object instance, and also calls the method addStatement() , which finally returns the created PoolPreparedStatement object instance.
14 ) prepareCall(): 实现 Connection 接口中的方法, 用于创建 CallableStatement 对 象 实 例 , 并 将 其 作 为 入 参 调 用 方 法 poolCallableStatement()以创建 PoolCallableStatement对象实例, 同时调用方 法 addStatement() , 最终返回创建的 PoolCallableStatement对象实例。  14) prepareCall(): Implements a method in the Connection interface, which is used to create a CallableStatement object instance, and uses it as the inbound call method poolCallableStatement() to create a PoolCallableStatement object instance. At the same time, the method addStatement() is called, and finally the created PoolCallableStatement object is returned. Example.
最后, 对改进后的数据库连接池中公共参数类 StatementUtil的关键元 素进行说明:  Finally, the key elements of the public parameter class StatementUtil in the improved database connection pool are described:
1 ) 变 量 pooledConnection : Statement 对 象 实 例 所 属 的 PoolConnectionlmpl对象实例。  1) The variable pooledConnection : the instance of the PoolConnectionlmpl object to which the instance object belongs.
2 ) 变量 statement: 本对象实例关联的 Statement对象实例。  2) Variable statement: An instance of the Statement object associated with this object instance.
3 )变量 startExecTime: Statement对象实例开始执行数据库操作的时间。 4 )变量 putedParameters: List类型, 用于保存 Statement对象实例的参 数。 3) Variable startExecTime: The time when the Statement object instance starts executing database operations. 4) Variable putedParameters: List type, used to save the parameters of the Statement object instance.
5 ) 变量 executeFlag: 执行标志, 其中 executeFlag取值为 0表示未执 行, executeFlag取值为 1表示执行 commonSql, executeFlag取值为 2表示 执行 preparedSql, executeFlag取值为 3表示执行 batchSql。  5) Variable executeFlag: Execution flag, where executeFlag takes a value of 0 to indicate unexecuted, executeFlag takes a value of 1 to execute commonSql, executeFlag has a value of 2 indicates that preparedSql is executed, and executeFlag has a value of 3 to execute batchSql.
6 ) batchSql: List类型, 用于保存 executeBatch时的 SQL语句。  6) batchSql: List type, used to save the SQL statement when executeBatch.
7 )preparedSql:用于保存 PreparedStatement对象实例执行的 SQL语句。 7) preparedSql: used to save the SQL statement executed by the PreparedStatement object instance.
8 ) commonSql: 用于保存 Statement对象实例执行的 SQL语句。 8) commonSql: used to save the SQL statement executed by the Statement object instance.
9 ) StatementUtilO: StatementUtil 对象实例的构造函数, 入参为 PoolConnectionlmpl 对象实例和 Statement 对象实例, 用于初始化变量 pooledConnection和更量 statements  9) StatementUtilO: The constructor of the StatementUtil object instance, which is a poolConnectionlmpl object instance and a Statement object instance, used to initialize the variable pooledConnection and the more statements
10 ) addParameter(): 将传入的参数添力口到变量 putedParameters中。 11 ) setExecuteFlag(): 设置 executeFlag标志, 用于区别当前执行的是 哪种 SQL语句。  10) addParameter(): Add the incoming parameters to the variable putedParameters. 11) setExecuteFlag(): Sets the executeFlag flag to distinguish which SQL statement is currently executing.
12 ) appandBatchSQL(): 将 SQL语句添加到 batchSql中, executeFlag 为 3时使用该方法。  12) appandBatchSQL(): Add the SQL statement to batchSql and use this method when executeFlag is 3.
13 ) setPreparedSql():设置 preparedSql, executeFlag为 2时使用该方法。 14 ) setCommonSql(): 设置 commonSql, executeFlag为 1时使用该方 法。  13) setPreparedSql(): Set the preparedSql, use this method when executeFlag is 2. 14) setCommonSql(): Set commonSql, use this method when executeFlag is 1.
15 ) isExecTimeout(): 判断执行是否超时, 入参为超时时间。  15) isExecTimeout(): Determines whether the execution timed out and the input time is the timeout period.
16 ) executeOver(): 执行结束后调用该方法, 清空 executeFlag执行标 志位(设置为 0, 即表示未执行) 以及 startExecTime。  16) executeOver(): This method is called after the execution ends, clearing the executeFlag execution flag (set to 0, which means it is not executed) and startExecTime.
下面, 详细阐述改进后的数据库连接池的实现原理, 主要包括三个方 面的内容:  In the following, the implementation principle of the improved database connection pool is elaborated, which mainly includes three aspects:
(一) JDBC标准数据库接口类的实现 JDBC标准数据库接口类包括 Connection、 Statement . PreparedStatement . CallableStatement , 其中 Connection 定义数据库连接, Statement、 PreparedStatement, CallableStatement分别定义用于执行 SQL语句的变量和 方法。 通过对数据库连接池的改进, JDBC标准数据库接口类 Connection, Statement、 PreparedStatement, CallableStatement分别对应 JDBC接口实现 类 PoolConnectionlmpl 、 PoolStatement 、 PoolPreparedStatement 和 PoolCallableStatement , 即 Connection 和 PoolConnectionlmpl 相对应 , Statement 和 PoolStatement 相 对 应 , PreparedStatement 和 PoolPreparedStatement相对应 , CallableStatement和 PoolCallableStatement 相对应。 (1) Implementation of JDBC standard database interface class The JDBC standard database interface class includes Connection, Statement . PreparedStatement . CallableStatement , where Connection defines the database connection, Statement, PreparedStatement, and CallableStatement define the variables and methods used to execute the SQL statement. Through the improvement of the database connection pool, the JDBC standard database interface classes Connection, Statement, PreparedStatement, and CallableStatement correspond to the JDBC interface implementation classes PoolConnectionlmpl, PoolStatement, PoolPreparedStatement, and PoolCallableStatement respectively, that is, Connection and PoolConnectionlmpl correspond to Statement and PoolStatement, PreparedStatement and PoolPreparedStatement Correspondingly, CallableStatement corresponds to PoolCallableStatement.
上述 JDBC接口实现类既可以作为 JDBC标准数据库接口类使用,又可 以增加个性化的变量和方法, 也就是说, 数据库连接池中緩存的是 JDBC 标准数据库接口类 Connection对应的 JDBC接口实现类 PoolConnectionlmpl 的对象实例,业务获取数据库连接时实际返回的是 PoolConnectionlmpl对象 实例, 上述改进对业务来说是透明的, 业务照常按照 JDBC 标准数据库接 口使用数据库连接。 同理, 业务通过调用 JDBC 标准数据库接口类 Connection定义的方法 createStatement()、 prepare Statement()、 prepareCall() 获取 Statement、 PreparedStatement、 CallableStatement 对象实例时 , PoolConnectionlmpl对象实例实际返回的上述 JDBC标准数据库接口类对应 的 JDBC接口实现类的对象实例, 即 PoolStatement、 PoolPreparedStatement、 PoolCallableStatement对象实例, 上述改进对业务来说同样是透明的, 业务 照常按照 JDBC标准数据库接口进行数据库操作。  The above JDBC interface implementation class can be used as a JDBC standard database interface class, and can also add personalized variables and methods, that is, the database connection pool caches the JDBC standard database interface class Connection corresponding to the JDBC interface implementation class PoolConnectionlmpl Object instance, the actual return of the PoolConnectionlmpl object instance when the business obtains the database connection. The above improvements are transparent to the business, and the business uses the database connection as usual according to the JDBC standard database interface. Similarly, when the business obtains the Statement, PreparedStatement, and CallableStatement object instances by calling the methods created by the JDBC standard database interface class Connection, createStatement(), prepare Statement(), and prepareCall(), the JDBC standard database interface class actually returned by the PoolConnectionlmpl object instance corresponds. The object instance of the JDBC interface implementation class, that is, the PoolStatement, PoolPreparedStatement, and PoolCallableStatement object instances, the above improvements are also transparent to the business, and the business performs database operations according to the JDBC standard database interface as usual.
(二) JDBC标准数据库接口类的扩展  (2) Extension of the JDBC standard database interface class
对 JDBC 标准数据库接口类的扩展体现在增加了两个方面的内容: 属 性和继承。 增加属性即上述 JDBC接口实现类中增加了变量, 用于保存相 应的对象实例及其相关信息, 例如 JDBC接口实现类 PoolConnectionlmpl 中增加了变量 conn (用于保存 Connection对象实例 )、 pool (用于保存 ConnectionPoolImpl 对 象 实 例 ) 等 等 , JDBC 接 口 实 现 类 PoolPreparedStatement 中增力口了 变量 preparedStatement ( 用 于保存 PreparedStatement对象实例), JDBC接口实现类 PoolCallableStatement中增 加了变量 callableStatement (用于保存 CallableStatement对象实例)。 增加继 承即 JDBC 接 口 实现类 PoolStatement 、 PoolPreparedStatement 、 PoolCallableStatement 实现 JDBC 标准数据库接口 类 Statement 、 PreparedStatement , CallableStatement的同时,继寿公共参数类 StatementUtil, 公共参数类 StatementUtil定义了实现 SQL语句跟踪需要使用的变量和方 法, 包括 JDBC标准数据库接口类 Statement所属的 Connection对象实例即 数据库连接、 Statement对象实例、 Statement对象实例开始执行数据库操作 的时间等等, 内部 Statement实现类通过继承公共参数类 StatementUtil可以 获得实现 SQL语句跟踪所需的变量和方法。 The extension to the JDBC standard database interface class is reflected in two aspects: properties and inheritance. Adding attributes, that is, adding variables to the above JDBC interface implementation class, for saving phases The object instance and its related information, such as the JDBC interface implementation class PoolConnectionlmpl added variable conn (used to save the Connection object instance), pool (used to save the ConnectionPoolImpl object instance), etc., JDBC interface implementation class PoolPreparedStatement in the booster port The variable preparedStatement (used to save the PreparedStatement object instance), the JDBC interface implementation class PoolCallableStatement added a variable callableStatement (used to save the CallableStatement object instance). Add inheritance, that is, the JDBC interface implementation class PoolStatement, PoolPreparedStatement, PoolCallableStatement implements the JDBC standard database interface class Statement, PreparedStatement, CallableStatement, and the public parameter class StatementUtil, the public parameter class StatementUtil defines the variables and methods needed to implement SQL statement tracking. Including the JDBC standard database interface class Statement belongs to the Connection object instance that is the database connection, the Statement object instance, the Statement object instance to start the database operation time, etc., the internal Statement implementation class can be achieved by inheriting the public parameter class StatementUtil to achieve SQL statement tracking Variables and methods.
(三) JDBC标准数据库接口类定义的方法的实现  (3) Implementation of the method defined by the JDBC standard database interface class
各 JDBC接口实现类在实现 JDBC标准数据库接口时调用对应 JDBC标 准数据库接口类定义的方法, 同时为了实现 SQL语句的跟踪进行了特定处 理, 最终还是通过 JDBC 标准数据库接口实现数据库操作, 只是额外增加 了对 SQL语句的緩存等相关处理,如 JDBC接口实现类 PoolConnectionlmpl 中 的 方 法 prepareStatement() 的 执 行 流 程 为 : 先 调 用 conn.prepareStatement(sql) , 再将返回的 PreparedStatement对象实例和自身 作为参数创建 PoolPreparedStatement对象实例, 然后保存 SQL语句, 最后 返回创建的 PoolPreparedStatement对象实例。其他方法的实现与上述方法的 实现相类似, 具体不再赘述。  Each JDBC interface implementation class calls the method defined by the JDBC standard database interface class when implementing the JDBC standard database interface. At the same time, in order to implement the specific processing of the SQL statement tracking, the database operation is finally implemented through the JDBC standard database interface, but the additional operation is added. For the processing of SQL statements, such as the JDBC interface implementation class PoolConnectionlmpl method prepareStatement () execution flow is: first call conn.prepareStatement (sql), then return the PreparedStatement object instance and itself as a parameter to create a PoolPreparedStatement object instance , then save the SQL statement, and finally return the created PoolPreparedStatement object instance. The implementation of other methods is similar to the implementation of the above method, and details are not described herein.
基于改进后的数据库连接池, 对本发明实施例提供的数据库连接池的 监控方法进行详细说明。 The database connection pool provided by the embodiment of the present invention is based on the improved database connection pool. The monitoring method is described in detail.
本发明实施例首先提供了一种在数据库连接池中创建数据库连接的方 法, 实现流程图如图 3所示, 包括如下步骤:  The embodiment of the present invention first provides a method for creating a database connection in a database connection pool. The implementation flowchart is as shown in FIG. 3, and includes the following steps:
S301、 初始化变量 managed, 变量 managed用于緩存数据库连接。 S302、 调用方法 newPooledConnectionO以创建 Connection对象实例即 数据库连接 conn。  S301. Initialize the variable managed, and the variable managed is used to cache the database connection. S302. Call the method newPooledConnectionO to create a Connection object instance, that is, the database connection conn.
S303、 将数据库连接 conn 和数据库连接池对象作为入参调用 PoolConnectionlmpl的构造函数, 创建 PoolConnectionlmpl对象实例即数据 库连接 pConiio  S303. The database connection conn and the database connection pool object are used as input functions of the poolConnectionlmpl to create a PoolConnectionlmpl object instance, that is, the database connection pConiio
S304、 将创建的数据库连接 pConn添加到变量 managed中緩存。  S304. Add the created database connection pConn to the variable managed cache.
S305、判断创建的数据库连接 pConn是否达到预先设定的最大连接数, 如果是, 则流程结束, 如果否, 则返回步骤 S302。  S305. Determine whether the created database connection pConn reaches the preset maximum number of connections. If yes, the process ends. If no, the process returns to step S302.
本发明实施例提供了一种数据库连接池的监控方法, 如图 4所示, 包 括如下步骤:  The embodiment of the invention provides a method for monitoring a database connection pool. As shown in FIG. 4, the method includes the following steps:
S401、从数据库连接池中获取 PoolConnectionlmpl对象实例即数据库连 接 pconn„  S401. Obtain a PoolConnectionlmpl object instance from a database connection pool, that is, a database connection pconn„
具体实施中, 通过调用方法 getConection()获取 PoolConnectionlmpl对 象实例即数据库连接 pconn, 方法 getConection()返回 PoolConnectionlmpl 对象实例, 数据库连接 conn是数据库连接 pconn中的一个变量。  In the specific implementation, the method Connection getConection() is used to obtain the PoolConnectionlmpl object instance, ie the database connection pconn, the method getConection() returns the PoolConnectionlmpl object instance, and the database connection conn is a variable in the database connection pconn.
S402、基于获取到的数据库连接 pconn创建用于执行 SQL语句的 JDBC 接口实现类的对象实例。  S402. Based on the obtained database connection, pconn creates an object instance of a JDBC interface implementation class for executing the SQL statement.
具体实施中, 所述用于执行 SQL 语句的 JDBC 接口实现类包括 PoolStatement、 PoolPreparedStatement和 PoolCallableStatement,相应的对象 实例包括 PoolStatement 对象实例、 PoolPreparedStatement 对象实例和 PoolCallableStatement对象实例; 数据库连接 pconn所创建的用于执行 SQL 语句的 JDBC 接口实现类的对象实例可以为 PoolStatement对象实例、 PoolPreparedStatement对象实例、 PoolCallableStatement对象实例其中之一 或者任意组合。 In a specific implementation, the JDBC interface implementation class for executing the SQL statement includes a PoolStatement, a PoolPreparedStatement, and a PoolCallableStatement, and the corresponding object instance includes a PoolStatement object instance, a PoolPreparedStatement object instance, and a PoolCallableStatement object instance; and the database connection pconn is used to execute the SQL. The object instance of the JDBC interface implementation class of the statement can be one of a PoolStatement object instance, a PoolPreparedStatement object instance, a PoolCallableStatement object instance, or any combination.
本发明实施例中,以创建 PoolPreparedStatement对象实例 ppstat的实现 流程为例进行说明, 具体请参见图 5 , 创建 PoolStatement 对象实例和 PoolCallableStatement对象实例的实现流程与创建 PoolPreparedStatement对 象实例的实现流程相类似, 不再赘述。  In the embodiment of the present invention, an implementation process of creating a PoolPreparedStatement object instance ppstat is taken as an example. For details, refer to FIG. 5. The implementation process of creating a PoolStatement object instance and a PoolCallableStatement object instance is similar to the implementation process of creating a PoolPreparedStatement object instance, and is no longer Narration.
S403、 根据用于执行 SQL语句的 JDBC接口实现类所继承的公共参数 类定义的实现 SQL语句跟踪需要使用的变量和方法,保存数据库连接 pconn 所创建的所有对象实例及其 SQL语句跟踪信息。  S403. Implementing a public parameter inherited by the JDBC interface implementation class for executing the SQL statement. Implementing the SQL statement to track the variables and methods to be used, and saving all object instances created by the database connection pconn and their SQL statement tracking information.
举例进行说明, 假设 S402中基于获取到的数据库连接 pconn创建的所 述 JDBC接口实现类的对象实例为 PoolPreparedStatement对象实例 ppstat, 则在 S402的具体实施中, 通过调用 PoolPreparedStatement对象实例 ppstat 的方法 setPreparedSql(),保存 PreparedStatement对象实例执行的 SQL语句; 通过调用方法 addStatement()将 PoolPreparedStatement对象实例 ppstat添力口 到 statementSet集合变量。  For example, it is assumed that the object instance of the JDBC interface implementation class created by the obtained database connection pconn in S402 is the PoolPreparedStatement object instance ppstat, and in the implementation of S402, the method of calling the PoolPreparedStatement object instance ppstat setPreparedSql() , save the SQL statement executed by the PreparedStatement object instance; add the PoolPreparedStatement object instance ppstat to the statementSet collection variable by calling the method addStatement().
5404、 在使用获取到的数据库连接 pconn执行数据库操作的过程中, 监控到数据库连接池中出现异常情况时, 获取该数据库连接所创建的所有 对象实例。  5404. In the process of performing a database operation using the obtained database connection pconn, when an abnormality occurs in the database connection pool is monitored, all object instances created by the database connection are obtained.
具体实施中, 数据库连接池监控任务监控到数据库连接池中出现异常 情况时, 通过调用方法 getStatementSet()获取 statementSet 集合变量, statementSet集合变量中存储该数据库连接所创建的所有对象实例;  In the specific implementation, when the database connection pool monitoring task monitors an abnormal situation in the database connection pool, the statementSet collection variable is obtained by calling the method getStatementSet(), and the statementSet collection variable stores all the object instances created by the database connection;
5405、 针对每一个对象实例, 获得该对象实例的 SQL语句跟踪信息。 具体实施中, 针对 statementSet集合变量中的每一个对象实例, 通过调 用每一个对象实例的方法 getSQLString()和 getExecTimeQ ,获得该对象实例 的 SQL语句跟踪信息, SQL语句跟踪信息包括数据库连接执行的 SQL语 句以及执行时间等信息, 其中, 方法 getSQLString()和 getExecTime()由公共 参数类 StatementUtil定义。 5405. Obtain, according to each object instance, the SQL statement tracking information of the object instance. In a specific implementation, for each object instance in the statementSet collection variable, the object instance is obtained by calling the getSQLString() and getExecTimeQ methods of each object instance. The SQL statement trace information, the SQL statement trace information includes the SQL statement executed by the database connection and the execution time and other information, wherein the methods getSQLString() and getExecTime() are defined by the public parameter class StatementUtil.
至此, 已经实现了对数据库连接池的监控, 具体实施中, 该方法还包 括如下步骤:  At this point, the monitoring of the database connection pool has been implemented. In the specific implementation, the method further includes the following steps:
在释放数据库连接 pconn时, 清空保存的该数据库连接所创建的所有 对象实例,释放数据库连接 pconn是指将数据库连接 pconn放回数据库连接 池中。  When the database connection pconn is released, all the object instances created by the saved database connection are emptied, and the database connection is released. pconn refers to putting the database connection pconn back into the database connection pool.
进一步, 为了便于用户进行异常情况的问题定位, 该方法还可包括如 下步骤:  Further, in order to facilitate the user to locate the problem of the abnormal situation, the method may further include the following steps:
将获得的各对象实例的 SQL语句跟踪信息打印在日志文件中, 供用户 进行异常情况的问题定位。  The obtained SQL statement tracking information of each object instance is printed in a log file for the user to locate the problem of the abnormal situation.
创建 PoolPreparedStatement对象实例的方法,如图 5所示, 包括如下步 骤:  The method of creating a PoolPreparedStatement object instance, as shown in Figure 5, includes the following steps:
S501、 定义 JDBC接口实现类 PoolPreparedStatement的变量 ppstat, 并 赋值为 null。  S501. Define a variable ppstat of the JDBC interface implementation class PoolPreparedStatement, and assign a value of null.
S502、 调用数据库连接 conn 定义的方法 prepareStatement()以创建 PreparedStatement对象实例 pstat。  S502. Call the database connection conn defined method prepareStatement() to create a PreparedStatement object instance pstat.
S503、 将数据库连接 pconn和 PreparedStatement对象实例 pstat作为入 参调用 PoolPreparedStatement的构造函数,创建 PoolPreparedStatement对象 实例 ppstat。  S503. Connect the database connection pconn and the PreparedStatement object instance pstat as a constructor of the PoolPreparedStatement to create a PoolPreparedStatement object instance ppstat.
基于同一技术构思, 本发明实施例提供了一种数据库连接池的监控装 置, 如图 6所示, 该装置包括:  Based on the same technical concept, an embodiment of the present invention provides a monitoring device for a database connection pool. As shown in FIG. 6, the device includes:
获取单元 601 , 用于从数据库连接池中获取数据库连接。  The obtaining unit 601 is configured to obtain a database connection from the database connection pool.
创建单元 602 , 用于基于获取到的数据库连接创建用于执行 SQL语句 的 JDBC接口实现类的对象实例,并根据该 JDBC接口实现类所继承的公共 参数类定义的实现 SQL语句跟踪需要使用的变量和方法, 保存该数据库连 接所创建的所有对象实例及其 SQL语句跟踪信息。 a creating unit 602, configured to create a SQL statement based on the obtained database connection The JDBC interface implements the object instance of the class, and according to the public parameter class defined by the JDBC interface implementation class, implements the SQL statement to track the variables and methods to be used, saves all the object instances created by the database connection and its SQL statement tracking. information.
监控单元 603 ,用于在使用获取到的数据库连接执行数据库操作的过程 中, 监控到数据库连接池中出现异常情况时, 获取该数据库连接所创建的 所有对象实例。  The monitoring unit 603 is configured to: when an abnormality occurs in the database connection pool is monitored during the database operation performed by using the obtained database connection, obtain all object instances created by the database connection.
获得单元 604, 用于针对每一个对象实例, 获得该对象实例的 SQL语 句跟踪信息。  The obtaining unit 604 is configured to obtain SQL statement tracking information of the object instance for each object instance.
具体实施中, 该数据库连接池的监控装置还可包括:  In a specific implementation, the monitoring device of the database connection pool may further include:
清空单元 605 , 用于在释放该数据库连接时, 清空保存的该数据库连接 所创建的所有对象实例。  The clear unit 605 is used to clear all the object instances created by the saved database connection when the database connection is released.
具体实施中, 为了方便用户进行异常情况的问题定位, 该数据库连接 池的监控装置还可包括:  In a specific implementation, in order to facilitate the user to locate the problem of the abnormal situation, the monitoring device of the database connection pool may further include:
打印单元 606, 用于将获得的各对象实例的 SQL语句跟踪信息打印在 日志文件中, 供用户进行异常情况的问题定位。  The printing unit 606 is configured to print the obtained SQL statement tracking information of each object instance in a log file, so that the user can locate the problem of the abnormal situation.
本发明实施例提供的数据库连接池的监控方法及装置, 实时緩存每一 个数据库连接执行数据库操作的过程中相关的 SQL语句跟踪信息, 在数据 库连接池出现异常情况时获得上述 SQL语句跟踪信息, 从而实现对数据库 连接执行的 SQL语句的跟踪, 为准确、 快速地实现数据库连接执行数据库 操作的过程中异常情况的问题定位提供基础, 提高数据库连接池的稳固性。  The method and device for monitoring the database connection pool provided by the embodiment of the present invention, real-time cache the related SQL statement tracking information in the process of performing database operations in each database connection, and obtain the above-mentioned SQL statement tracking information when an abnormality occurs in the database connection pool, thereby The tracking of the SQL statements executed by the database connection is realized, which provides a basis for accurately and quickly realizing the problem location of the abnormal situation in the process of performing database operations in the database connection, and improves the stability of the database connection pool.
本领域的技术人员应明白, 本发明的实施例可提供为方法、 装置、 或 计算机程序产品。 因此, 本发明可釆用完全硬件实施例、 完全软件实施例、 或结合软件和硬件方面的实施例的形式。 而且, 本发明可釆用在一个或多 个其中包含有计算机可用程序代码的计算机可用存储介质 (包括但不限于 磁盘存储器、 CD-ROM、 光学存储器等)上实施的计算机程序产品的形式。 本发明是参照根据本发明实施例的方法、 装置和计算机程序产品的流 程图和 /或方框图来描述的。应理解可由计算机程序指令实现流程图和 /或方 框图中的每一流程和 /或方框、以及流程图和 /或方框图中的流程和 /或方框的 结合。 可提供这些计算机程序指令到通用计算机、 专用计算机、 嵌入式处 理机或其他可编程数据处理设备的处理器以产生一个机器, 使得通过计算 机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图 一个流程或多个流程和 /或方框图一个方框或多个方框中指定的功能的装 置。 Those skilled in the art will appreciate that embodiments of the present invention can be provided as a method, apparatus, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or a combination of software and hardware. Moreover, the invention can be embodied in the form of one or more computer program products embodied on a computer-usable storage medium (including but not limited to disk storage, CD-ROM, optical storage, etc.) in which computer usable program code is embodied. The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus, and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flowchart illustrations and/or FIG. 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 device to produce a machine for the execution of instructions for execution by a processor of a computer or other programmable data processing device. Means for implementing the functions specified in one or more of the flow or in a block or blocks of the flow chart.
这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理 设备以特定方式工作的计算机可读存储器中, 使得存储在该计算机可读存 储器中的指令产生包括指令装置的制造品, 该指令装置实现在流程图一个 流程或多个流程和 /或方框图一个方框或多个方框中指定的功能。  The computer program instructions can also be stored in a computer readable memory that can direct a computer or other programmable data processing device to operate in a particular manner, such that the instructions stored in the computer readable memory produce an article of manufacture comprising the instruction device. The apparatus implements the functions specified in one or more blocks of a flow or a flow and/or block diagram of the flowchart.
这些计算机程序指令也可装载到计算机或其他可编程数据处理设备 上, 使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机 实现的处理, 从而在计算机或其他可编程设备上执行的指令提供用于实现 在流程图一个流程或多个流程和 /或方框图一个方框或多个方框中指定的功 能的步骤。 本发明的精神和范围。 这样, 倘若本发明的这些修改和变型属于本发明权 利要求及其等同技术的范围之内, 则本发明也意图包含这些改动和变型在 内。  These computer program instructions can also be loaded onto a computer or other programmable data processing device such that a series of operational steps are performed on a computer or other programmable device to produce computer-implemented processing for execution on a computer or other programmable device. The instructions provide steps for implementing the functions specified in one or more of the flow or in a block or blocks of a flow diagram. The spirit and scope of the invention. Thus, it is intended that the present invention cover the modifications and modifications of the invention

Claims

权利要求书 Claim
1、 一种数据库连接池的监控方法, 其特征在于, 该方法包括: 从数据库连接池中获取数据库连接;  A method for monitoring a database connection pool, the method comprising: obtaining a database connection from a database connection pool;
基于获取到的数据库连接创建用于执行结构化查询语言( SQL )语句的 JAVA数据库连接(JDBC )接口实现类的对象实例, 并根据所述 JDBC接 口实现类所继承的公共参数类定义的实现 SQL语句跟踪需要使用的变量和 方法,保存所述数据库连接所创建的所有对象实例及其 SQL语句跟踪信息; 在使用获取到的数据库连接执行数据库操作的过程中, 监控到数据库 连接池中出现异常情况时, 获取所述数据库连接所创建的所有对象实例; 针对每一个对象实例, 获得所述对象实例的 SQL语句跟踪信息。  Creating an object instance of a JAVA database connection (JDBC) interface implementation class for executing a Structured Query Language (SQL) statement based on the obtained database connection, and implementing SQL according to the public parameter class defined by the JDBC interface implementation class The statement tracks the variables and methods that need to be used to save all the object instances created by the database connection and their SQL statement tracking information. During the execution of the database operation using the obtained database connection, the abnormality in the database connection pool is monitored. Obtaining all object instances created by the database connection; obtaining, for each object instance, SQL statement tracking information of the object instance.
2、 根据权利要求 1所述的方法, 其特征在于, 该方法还包括: 在释放所述数据库连接时, 清空保存的所述数据库连接所创建的所有 对象实例。  2. The method according to claim 1, wherein the method further comprises: when the database connection is released, clearing all saved object instances created by the database connection.
3、 根据权利要求 1 所述的方法, 其特征在于, 所述对象实例的 SQL 语句跟踪信息包括所述对象实例执行的 SQL语句以及执行时间。  3. The method according to claim 1, wherein the SQL statement tracking information of the object instance comprises an SQL statement executed by the object instance and an execution time.
4、 根据权利要求 1、 2或 3所述的方法, 其特征在于, 该方法还包括: 将获得的各对象实例的 SQL语句跟踪信息打印在日志文件中, 供用户 进行异常情况的问题定位。  The method according to claim 1, 2 or 3, wherein the method further comprises: printing the obtained SQL statement tracking information of each object instance in a log file for the user to locate the problem of the abnormal situation.
5、 根据权利要求 1、 2或 3所述的方法, 其特征在于, 所述用于执行 SQL语句的 JDBC接口实现类包括:实现 JDBC标准数据库接口类 Statement 的 JDBC 接口实现类 PoolStatement、 实现 JDBC 标准数据库接口类 PreparedStatement 的 JDBC接口实现类 PoolPreparedStatement、 以及实现 JDBC 标准数据库接口类 CallableStatement 的 JDBC 接口 实现类 PoolCallableStatement。  The method according to claim 1, 2 or 3, wherein the JDBC interface implementation class for executing the SQL statement comprises: implementing a JDBC standard database interface class Statement JDBC interface implementation class PoolStatement, implementing a JDBC standard The JDBC interface implementation class PoolPreparedStatement of the database interface class PreparedStatement, and the JDBC interface implementation class PoolCallableStatement that implements the JDBC standard database interface class CallableStatement.
6、 根据权利要求 5 所述的方法, 其特征在于, 创建所述 PoolPreparedStatement对象实例包括: 6. The method of claim 5, wherein: creating the PoolPreparedStatement object instances include:
创建 PreparedStatement对象实例;  Create a PreparedStatement object instance;
将获取到的数据库连接和 PreparedStatement 对象实例作为入参调用 PoolPreparedStatement的构造函数, 创建 PoolPreparedStatement对象实例。  Create a PoolPreparedStatement object instance by using the obtained database connection and the PreparedStatement object instance as the constructor of the PoolPreparedStatement.
7、 根据权利要求 1、 2或 3所述的方法, 其特征在于, 所述数据库连 接为 PoolConnectionlmpl对象实例, PoolConnectionlmpl为实现 JDBC标准 数据库接口类 Connection的 JDBC接口实现类, 在所述数据库连接池中创 建所述数据库连接包括:  The method according to claim 1, 2 or 3, wherein the database connection is a PoolConnectionlmpl object instance, and the PoolConnectionlmpl is a JDBC interface implementation class for implementing a JDBC standard database interface class Connection, in the database connection pool. Creating the database connection includes:
初始化数据库连接池中用于緩存数据库连接的变量;  Initialize variables in the database connection pool that are used to cache database connections;
创建 Connection对象实例, 将 Connection对象实例和数据库连接池对 象作为入参调用 PoolConnectionlmpl的构造函数,创建数据库连接,并将创 建的数据库连接添加到所述变量中緩存;  Create a Connection object instance, call the Connection object instance and the database connection pool object as the constructor of the PoolConnectionlmpl input parameter, create a database connection, and add the created database connection to the variable cache;
当创建的数据库连接的数量达到预先设定的最大连接数时, 停止数据 库连接的创建。  The creation of a database connection is stopped when the number of database connections created reaches the preset maximum number of connections.
8、 一种数据库连接池的监控装置, 其特征在于, 该装置包括: 获取单元, 用于从数据库连接池中获取数据库连接;  A monitoring device for a database connection pool, the device comprising: an obtaining unit, configured to acquire a database connection from a database connection pool;
创建单元, 用于基于获取到的数据库连接创建用于执行结构化查询语 言 SQL语句的 JAVA数据库连接 JDBC接口实现类的对象实例, 并根据所 述 JDBC接口实现类所继承的公共参数类定义的实现 SQL语句跟踪需要使 用的变量和方法, 保存所述数据库连接所创建的所有对象实例及其 SQL语 句跟踪信息;  a creating unit, configured to create an object instance of a JAVA database connection JDBC interface implementation class for executing a structured query language SQL statement based on the obtained database connection, and implement the public parameter class definition inherited by the JDBC interface implementation class The SQL statement keeps track of the variables and methods that need to be used, and saves all object instances created by the database connection and their SQL statement tracking information;
监控单元, 用于在使用获取到的数据库连接执行数据库操作的过程中, 监控到数据库连接池中出现异常情况时, 获取所述数据库连接所创建的所 有对象实例;  a monitoring unit, configured to acquire all object instances created by the database connection when an abnormality occurs in the database connection pool is monitored during the execution of the database operation by using the obtained database connection;
获得单元, 用于针对每一个对象实例, 获得所述对象实例的 SQL语句 跟踪信息。 An obtaining unit, configured to obtain an SQL statement of the object instance for each object instance tracking information.
9、 根据权利要求 8所述的装置, 其特征在于, 该装置还包括: 清空单元, 用于在释放所述数据库连接时, 清空保存的所述数据库连 接所创建的所有对象实例。  The device according to claim 8, wherein the device further comprises: an emptying unit, configured to clear all the object instances created by the saved database connection when the database connection is released.
10、 根据权利要求 8或 9所述的装置, 其特征在于, 该装置还包括: 打印单元, 用于将获得的各对象实例的 SQL语句跟踪信息打印在曰志 文件中, 供用户进行异常情况的问题定位。  The device according to claim 8 or 9, wherein the device further comprises: a printing unit, configured to print the obtained SQL statement tracking information of each object instance in a file for the user to perform an abnormal situation. Problem positioning.
PCT/CN2010/077270 2010-04-29 2010-09-25 Method and device for monitoring database connection pool WO2011134233A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201010160673.5A CN101840352B (en) 2010-04-29 2010-04-29 Method and device for monitoring database connection pool
CN201010160673.5 2010-04-29

Publications (1)

Publication Number Publication Date
WO2011134233A1 true WO2011134233A1 (en) 2011-11-03

Family

ID=42743738

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2010/077270 WO2011134233A1 (en) 2010-04-29 2010-09-25 Method and device for monitoring database connection pool

Country Status (2)

Country Link
CN (1) CN101840352B (en)
WO (1) WO2011134233A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112925841A (en) * 2021-03-26 2021-06-08 瀚高基础软件股份有限公司 Distributed JDBC implementation method, device and computer-readable storage medium

Families Citing this family (20)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101840352B (en) * 2010-04-29 2014-09-10 中兴通讯股份有限公司 Method and device for monitoring database connection pool
CN101980207B (en) * 2010-11-09 2012-05-30 中国电信股份有限公司 Method and system for implementing database access
CN102110162B (en) * 2011-02-24 2013-03-20 国家电网公司 Method and device for realizing monitoring on SQL (structured query language) database
CN102385628B (en) * 2011-11-14 2015-05-13 北京锐安科技有限公司 Java data base connectivity (JDBC)-based data distributed processing method
CN102594727A (en) * 2012-02-07 2012-07-18 苏州阔地网络科技有限公司 Distributed instant communication system and communication method thereof
CN102594728A (en) * 2012-02-09 2012-07-18 苏州阔地网络科技有限公司 Distributed instant communication method and system
CN104778185B (en) * 2014-01-15 2018-01-30 中国移动通信集团北京有限公司 Anomaly sxtructure query language SQL statement determines method and server
CN104361000A (en) * 2014-09-24 2015-02-18 广东电网有限责任公司佛山供电局 Access method and system for relational database based on self-built database connection pool
CN104484452A (en) * 2014-12-25 2015-04-01 广东电子工业研究院有限公司 Connection reusing and unified management and control method of multi-element isomerism relation database
CN105426285B (en) * 2015-10-28 2019-01-18 联动优势科技有限公司 The method and apparatus of database connection pool monitoring and management
CN105302565A (en) * 2015-11-10 2016-02-03 河海大学 Java-based data persistence assembly with built-in connection pool management function
CN109213789A (en) * 2017-07-07 2019-01-15 北京京东尚科信息技术有限公司 A kind of analysis method and device about business SQL
CN107748632B (en) * 2017-09-28 2021-11-02 努比亚技术有限公司 Screen freezing monitoring method, mobile terminal and computer readable storage medium
CN108108479A (en) * 2018-01-04 2018-06-01 山东中创软件商用中间件股份有限公司 A kind of database connecting detection method, system, equipment and computer media
CN109739486A (en) * 2019-01-03 2019-05-10 深圳英飞拓科技股份有限公司 Multi-data source database manipulation implementation method and device based on JdbcTemplate
CN110209688B (en) * 2019-04-16 2023-08-18 中国平安人寿保险股份有限公司 Structured query statement positioning method, device, computer device and storage medium
CN111309507B (en) * 2020-02-13 2022-05-10 支付宝(杭州)信息技术有限公司 Memory leak detection method and device and machine-readable storage medium
CN111767187B (en) * 2020-05-19 2023-11-14 中国建设银行股份有限公司 Method for monitoring JDBC connection pool state and related equipment
CN112988401B (en) * 2021-05-07 2021-08-17 北京轻松筹信息技术有限公司 Method and device for collecting information in process and electronic equipment
CN116737818B (en) * 2023-08-15 2023-12-22 浩鲸云计算科技股份有限公司 Connection leakage detection method and system for draid database connection pool

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20070083525A1 (en) * 2005-10-11 2007-04-12 Rahul Srivastava JDBC debugging enhancements
CN101226497A (en) * 2007-12-10 2008-07-23 金蝶软件(中国)有限公司 Application program monitoring system and monitoring method
CN101840352A (en) * 2010-04-29 2010-09-22 中兴通讯股份有限公司 Method and device for monitoring database connection pool

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN100337235C (en) * 2003-06-23 2007-09-12 华为技术有限公司 Method and apparatus for accessing database
JP2009110439A (en) * 2007-10-31 2009-05-21 Nec Corp Database access system, database access method and program
CN100578509C (en) * 2008-07-18 2010-01-06 北京航空航天大学 Long lasting implementing method for data

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20070083525A1 (en) * 2005-10-11 2007-04-12 Rahul Srivastava JDBC debugging enhancements
CN101226497A (en) * 2007-12-10 2008-07-23 金蝶软件(中国)有限公司 Application program monitoring system and monitoring method
CN101840352A (en) * 2010-04-29 2010-09-22 中兴通讯股份有限公司 Method and device for monitoring database connection pool

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112925841A (en) * 2021-03-26 2021-06-08 瀚高基础软件股份有限公司 Distributed JDBC implementation method, device and computer-readable storage medium
CN112925841B (en) * 2021-03-26 2022-11-08 瀚高基础软件股份有限公司 Distributed JDBC implementation method, device and computer-readable storage medium

Also Published As

Publication number Publication date
CN101840352A (en) 2010-09-22
CN101840352B (en) 2014-09-10

Similar Documents

Publication Publication Date Title
WO2011134233A1 (en) Method and device for monitoring database connection pool
US8601469B2 (en) Method and system for customizing allocation statistics
JP4436036B2 (en) Information processing apparatus, trace processing method, program, and recording medium
Zhang et al. Research on lightweight MVC framework based on spring MVC and mybatis
US10325079B1 (en) Version management platform
WO2017041657A1 (en) Application interface management method and device
CN102929771B (en) Log recording apparatus and log recording method
US8589733B2 (en) Saving operational state of open applications when unexpected shutdown events occur
JP2006209774A (en) System and method for context awareness platform
TW201537343A (en) Recognition application scenarios, power management method, apparatus and terminal equipment
US7962782B2 (en) Modifying connection records
US8893272B2 (en) Method and device for recombining runtime instruction
US20130262388A1 (en) Database backup to highest-used page
WO2016033966A1 (en) Protection method and device for application data
US9336014B2 (en) Method, apparatus, and system for human-machine interaction
US20140047553A1 (en) Method and apparatus for data security reading
WO2020015116A1 (en) Database monitoring method and terminal device
CN105094811B (en) A kind of method and apparatus of event handling
US20130024863A1 (en) System and method for providing dynamic transaction optimizations
EP3079057B1 (en) Method and device for realizing virtual machine introspection
CN102411513B (en) Garbage collection method for mixed mode execution engine
CN102646066A (en) Method and device for obtaining starting-up time
RU2010149275A (en) RESTORATION OF THE RESOURCE MANAGEMENT OF THE PROCESSING, WHICH EXECUTES THE EXTERNAL CONTEXT OF EXECUTION
CN105677481B (en) A kind of data processing method, system and electronic equipment
CN103902890A (en) Monitoring method and monitoring system for Android program behaviors

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 10850564

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 10850564

Country of ref document: EP

Kind code of ref document: A1