繁体   English   中英

使用tomcat数据源-如何通过spring jndi访问数据源以获取当前数据库池状态

[英]using tomcat datasource - how to access datasource to get current db pool status by spring jndi

现在的情况

我正在Web和rest api服务器上使用Jmeter进行压力负载测试,但是某些转换的响应时间会延迟很多,因此我正在使用Spring Aspect来获取方法处理时间。 我无法设置的是某些过程调用会花费太多时间,因此尝试通过写入具有特定事务的日志来检查数据库处理时间(获取con,释放con,纯db处理时间)。 JMX不是一种选择,因为我无法使用它来跟踪交易。 我只想保留ThreadContext标记为DB池状态,以便我可以同时检查慢速事务和DB池状态。

这里不考虑使用来自Tomcat的DB数据源,因为不想在项目文件中进行DB设置。

我目前不考虑在Spring项目中使用数据源。

当前设置

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

applicationContext.xml-数据库设置

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

尝试做...记录数据库池状态

每当尝试调用某些dao类中的函数时,我都试图使用Spring Aspect编写日志。 我要写入的日志类似于数据库池状态,例如

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

等等。

是否可以从spring项目访问Tomcat的数据库池? 下面将有类似这样的方法。

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

您可以简单地创建tomcatJdbcPoolDataSource的代理并将其用作spring bean。 我已经为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;
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM