登录DBCP [英] Logging in DBCP

查看:132
本文介绍了登录DBCP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Commons DBCP。有一项任务是跟踪DBCP的内部行为 - 活动和空闲连接的数量。

I'm using Apache Commons DBCP. There is a task to track the inner behavior of the DBCP - number of active and idle connections.

我发现DBCP根本没有任何此类日志记录。是的,当从池中借用连接时,可以编写输出BasicDataSource状态的代码。但是,在返回或关闭连接时,无法跟踪BasicDataSource的状态,因为连接对象对池没有任何了解。

I found out that DBCP lacks any such logging at all. Yes, tt is possible to write the code that outputs the status of the BasicDataSource when connection is borrowed from the pool. However there is no way to track the status of the BasicDataSource when connection is returned or closed, because connection object knows nothing about the pool.

任何想法?

推荐答案

我认为方面可能是解决方案你的quandry。退房:

I think aspects may be the solution to your quandry. Check out:

  • http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-introduction-spring-defn
  • http://www.ibm.com/developerworks/java/library/j-ajdt/
  • http://www.eclipse.org/aspectj/doc/released/progguide/index.html

基本上,您可以编写一个或两个方面来锁定DBCP内部某些方法的执行。

Basically, you can write an aspect or two that will "latch onto" the execution of some methods inside DBCP.

类似于:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.ProceedingJoinPoint;

@Aspect
public class AroundExample {

  @Around("org.apache.commons.dbcp.PoolingDataSource.getConnection()")
  public Object doBasicPStuff(ProceedingJoinPoint pjp) throws Throwable {
    // write code to do what you want
    final PoolingDataSource ds = (PoolingDataSource) pjp.getThis();
    // log whatever you want

    // let it finish
    Object retVal = pjp.proceed();
    // stop stopwatch
    return retVal;
  }

}

这只是一个很小的例子。方面非常强大,有很多不同的方法可以做你想要的。代码取决于你是否使用Spring,以及你想要记录的是什么。

That's just a tiny example. Aspects are really powerful and there's a bunch of different ways to do what you want. The code depends on whether you're using Spring or not, and what exactly you wanna log.

P.S。我还没有测试过上面的代码。

P.S. I haven't tested the above code.

这篇关于登录DBCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆