gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:01:16 24 4
gpt4 key购买 nike

现状

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

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

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

当前设置

Spring项目的事务管理器使用Tomcat DBCP pool with Oracle datasource(oracle.jdbc.OracleDriver with javax.sql.DataSource)

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 来编写日志。我要写的日志是像DB Pool status之类的

  1. Activity 连接数
  2. 空闲连接数
  3. 最大 Activity 连接设置
  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;
}
}

}

关于java - 使用 tomcat 数据源 - 如何通过 spring jndi 访问数据源以获取当前数据库池状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45203859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com