Java 如何监控 c3p0 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4104125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:39:46  来源:igfitidea点击:

How to monitor c3p0 connections

javahibernatec3p0health-monitoring

提问by Llistes Sugra

I am using Hibernate in my JBoss war, using c3p0 for connection pooling, both configured within a hibernate.cfg.xml config file in my classpath

我在我的 JBoss War中使用 Hibernate,使用 c3p0 进行连接池,两者都在我的类路径中的 hibernate.cfg.xml 配置文件中配置

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

I've seen server.log generates lines with interesting information about the connection pool:

我已经看到 server.log 生成包含有关连接池的有趣信息的行:

DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] trace com.mchange.v2.resourcepool.BasicResourcePool@63f5e4b6 [managed: 10, unused: 9, excluded: 0]

调试 [com.mchange.v2.resourcepool.BasicResourcePool] 跟踪 com.mchange.v2.resourcepool.BasicResourcePool@63f5e4b6 [管理:10,未使用:9,排除:0]

For my monitoring pool (I am using nagios) I'd like to provide a JSP telling how many connections are being used and how many are free, as the log file says.

对于我的监控池(我正在使用 nagios),我想提供一个 JSP,告诉我们正在使用多少连接以及有多少是免费的,正如日志文件所说。

How can I ask c3p0 how many managed and unused connections are there?

我如何询问 c3p0 有多少托管和未使用的连接?

采纳答案by Pascal Thivent

You can monitor your connection pool(s) via JMX. From the documentation:

您可以通过JMX监控您的连接池。从文档:

Configuring and Managing c3p0 via JMX

If JMX libraries and a JMX MBeanServer are available in your environment (they are include in JDK 1.5 and above), you can inspect and configure your c3p0 datasources via a JMX administration tool (such as jconsole, bundled with jdk 1.5). You will find that c3p0 registers MBeans under com.mchange.v2.c3p0, one with statistics about the library as a whole (called C3P0Registry), and an MBean for each PooledDataSourceyou deploy. You can view and modify your DataSource's configuration properties, track the activity of Connection, Statement, and Thread pools, and reset pools and DataSources via the PooledDataSourceMBean. (You may wish to view the API docs of PooledDataSourcefor documentation of the available operations.)

通过 JMX 配置和管理 c3p0

如果 JMX 库和 JMX MBeanServer 在您的环境中可用(它们包含在 JDK 1.5 及更高版本中),您可以通过 JMX 管理工具(例如 jconsole,与 jdk 1.5 捆绑在一起)检查和配置您的 c3p0 数据源。您会发现 c3p0 在 下注册了 MBean com.mchange.v2.c3p0,其中包含关于整个库的统计信息(称为C3P0Registry),以及PooledDataSource您部署的每个 MBean 。您可以查看和修改数据源的配置属性,跟踪连接、语句和线程池的活动,并通过PooledDataSourceMBean重置池和数据源 。(您可能希望查看PooledDataSource可用操作的文档的 API 文档 。)

By the way, there seem to be JMX plugins for Nagios, you're not forced to use a JSP.

顺便说一句,Nagios 似乎有 JMX 插件,您不必被迫使用 JSP。

回答by d4v3y0rk

You can monitor with Icinga/Nagios like this.

您可以像这样使用 Icinga/Nagios 进行监控。

  1. Download JMXQuery from google code. You will need to check out revision 18 like so.

    svn checkout -r 18 http://jmxquery.googlecode.com/svn/trunk/ jmxquery-read-only

  2. Download this patch. wildcard patch for c3p0

  3. use this command to patch the source code: (make sure you are in the jmxquery-read-only/src/main directory)

    patch -p0 -i wildcard_patch.diff

  4. now download Apache Mavenand extract it using this command

    tar -zxvf apache-maven-*-bin.tar.gz

  5. now cd into the jmxquery-read-only folder and run the following command (assuming the apache maven and the jmxquery are in the same folder)

    ../apache-maven-*/bin/mvn compile

  6. then run the following command:

    ../apache-maven-3.0.3/bin/mvn package

  7. now you should have produced a jmxquery.jar file that you can use to query the c3p0 connection pool like so: (the check_jmx file can be obtained from just downloading the jmxquery code from the google code site like normal. using this link)

    check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:1090/jmxrmi -O com.mchange.v2.c3p0:type=PooledDataSource* -N 1 -A numBusyConnections -w 50 -c 100

  1. 从谷歌代码下载 JMXQuery。您将需要像这样检查修订版 18。

    svn checkout -r 18 http://jmxquery.googlecode.com/svn/trunk/ jmxquery-read-only

  2. 下载这个补丁。c3p0 的通配符补丁

  3. 用这个命令打补丁源码:(确保你在jmxquery-read-only/src/main目录下)

    patch -p0 -i wildcard_patch.diff

  4. 现在下载Apache Maven并使用此命令提取它

    tar -zxvf apache-maven-*-bin.tar.gz

  5. 现在 cd 进入 jmxquery-read-only 文件夹并运行以下命令(假设 apache maven 和 jmxquery 在同一文件夹中)

    ../apache-maven-*/bin/mvn compile

  6. 然后运行以下命令:

    ../apache-maven-3.0.3/bin/mvn package

  7. 现在您应该已经生成了一个 jmxquery.jar 文件,您可以使用它来查询 c3p0 连接池,如下所示:(check_jmx 文件可以像往常一样从谷歌代码站点下载 jmxquery 代码获得。使用此链接

    check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:1090/jmxrmi -O com.mchange.v2.c3p0:type=PooledDataSource* -N 1 -A numBusyConnections -w 50 -c 100