2017-07-20 41 views
2

现状,以获得当前数据库池的状态使用Tomcat的数据源 - 如何访问数据源弹簧JNDI

我做Web和REST API服务器利用JMeter应力负荷测试,但一些transations'响应时间延迟很多,所以我使用Spring Aspect来获取方法处理时间。我无法设置的是某些过程调用花费了太多时间,因此试图通过使用特定事务写入日志来检查数据库进程时间(获取con,释放con,纯数据库进程时间)。 JMX不是一个选项,因为我无法使用它来跟踪事务。我只想在ThreadContext上留下数据库池状态,以便我可以同时检查慢事务和数据库池状态。

从Tomcat中使用DB数据源在这里不被视为不想在项目文件中进行数据库设置。

在Spring项目中使用数据源不是我正在考虑的一个选项。

当前设置

Spring项目的事务管理器使用Tomcat DBCP池与Oracle数据源(oracle.jdbc.OracleDriver与javax.sql.DataSource中)

的applicationContext.xml - DB设置

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="java:/comp/env/jdbc/svc"/> 
    <property name="resourceRef" value="true"/> 
</bean> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="mapperLocations" value="classpath*:../sql/**.xml"/> 
    <property name="dataSource"><ref bean="dataSource"/></property> 
</bean> 

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
    <constructor-arg ref="sqlSessionFactory"/> 
</bean> 

<bean id="oracleTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> 
<bean id="transactionManager" class="com.xxx.xxx.api.transaction.TransactionManager"> 
    <property name="transactionManagers"> 
     <list> 
      <ref bean="oracleTransactionManager"/> 
     </list> 
    </property> 
</bean> 

试图做的事情...记录数据库池状态

我想使用Spring Aspect在某个dao类中的函数被调用时写入日志。 我想写日志就像是DB池状态如

  1. 活动连接计数
  2. 空闲连接计数
  3. 最大活动连接设置
  4. 最大空闲连接设置

和等等。

问题

是否有可能从春季项目中访问Tomcat的DB池? 下面将介绍这样的方法。

  1. getNumIdle()
  2. getWaitCount()
  3. getNumActive()

回答

0

您可以简单地创建一个代理的tomcatJdbcPoolDataSource,并把它作为春天豆。我创建了C3P0池化数据源的代理。后来我用所需的配置创建了我的类的spring bean,并将其用作数据源。我相信你可以做类似的事情。

public class C3PODataSourceProxy extends AbstractComboPooledDataSource { 

    public C3PODataSourceProxy() { 
     super(); 
    } 

    public C3PODataSourceProxy(boolean autoregister) { 
     super(autoregister); 
    } 

    public C3PODataSourceProxy(String configName) { 
     super(configName); 
    } 

    @Override 
    public Connection getConnection() throws SQLException { 
     try { 
      Connection connection = super.getConnection(); 
      //You can call the below methods and log it, send it to some other class etc 
      getNumIdleConnections(); 
      getNumBusyConnections(); 
      return connection; 
     } catch (Exception exception) { 
      //log the exception 
      throw exception; 
     } 
    } 

    public Connection getConnection(String username, String password) throws SQLException { 

     try { 
      Connection connection = super.getConnection(username, password); 
       //You can call the below methods and log it, send it to some other class etc 
      getNumIdleConnections(username, password); 
      getNumBusyConnections(username, password); 
      return connection; 
     } catch (Exception exception) { 
      //log the exception 
      throw exception; 
     } 
    } 

} 
+0

感谢您的回答,我会看看它是否适用于我的项目。 – handicop

+0

@ handicop你有没有试过这个解决方案? – yaswanth

+0

我能够设置驱动程序,并可以看到计数。也解决了这个问题。如果任何使用相同sid的应用程序多次尝试使用错误的密码登录,并且没有策略来锁定整个sid,则oracle数据库有一个导致内存锁定的错误。用错误密码查找守护进程,并解决延迟过程。感谢您的询问。 – handicop

相关问题