2012-10-16 33 views
1

使用java/Spring/Ibatis sqlserver是數據庫和datasourceorg.apache.commons.dbcp.BasicDataSource以下是數據源對象我想公開實時連接池計數,如正在使用中的數量和多少在閒置和我想用jmx任何快的想法來監控如何實現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> 

回答

1

是的,我們能做到這一點,只需啓用JMX對於具有JdbcTemplate的注入和正在使用的DAO方法和使用創造公共的getter你的DAO類方法返回以下方法的值,並在這裏你去jmx可以監視你。

http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html

getMinIdle() 
      Returns the minimum number of idle connections in the pool. 
int getNumActive() 
      [Read Only] The current number of active connections that have been allocated from this data source. 
int getNumIdle() 
5

接受的答案並沒有真正告訴你如何做到這一點。如果您使用Spring,可能的解決方案是使用MethodNameBasedMBeanInfoAssembler並列出您需要公開的BasicDataSource上的方法。假設你有一個配置爲ID爲dataSource的豆,請將此添加到您的Spring XML config

<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>