使用jmx的Spring数据源连接管理器来公开实时连接数

d-m*_*man 2 connection-pooling spring-jdbc spring-jmx

使用java/Spring/Ibatis sqlserver的数据库,并datasourceorg.apache.commons.dbcp.BasicDataSource下面的数据源对象我要揭露实时连接池指望像现在有多少人在使用,有多少闲置的,我想使用监视jmx任何快速的想法如何实现

<bean id="wssModelDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="net.sourceforge.jtds.jdbcx.JtdsDataSource"/>
    <property name="url" value="com.wss.jdbc.ConnectionUrl=jdbc:jtds:sqlserver://x-x2/x_control_QA;appName=wss;sendStringParametersAsUnicode=false;loginTimeout=20;socketTimeout=180"/>
    <property name="username" value="xxx"/>
    <property name="password" value="xxx"/>
    <property name="maxActive" value="10"/>
    <property name="maxWait" value="10000"/>
  </bean>
Run Code Online (Sandbox Code Playgroud)

bla*_*ank 5

接受的答案并没有真正告诉你如何做到这一点.如果您使用Spring,可能的解决方案是使用MethodNameBasedMBeanInfoAssembler并列出BasicDataSource您需要公开的方法.假设您有一个配置了id的bean,dataSource请将其添加到Spring XML配置中:

<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
    <property name="locateExistingServerIfPossible" value="true" />
</bean>
<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="assembler">
      <bean class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">
        <property name="managedMethods">
        <list>
          <value>getNumActive</value>
          <value>getMaxActive</value>
          <value>getNumIdle</value>
          <value>getMaxIdle</value>
          <value>getMaxWait</value>
          <value>getInitialSize</value>
          </list>
        </property>
      </bean>
    </property>
    <property name="beans">
        <map>                   
        <entry key="dataSource:name=DataSource" value-ref="dataSource"/>    
    </map>
    </property>
    <property name="server" ref="mbeanServer" />
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)