Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in 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.

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.

Any ideas?

like image 964
kachanov Avatar asked Aug 30 '10 00:08

kachanov


People also ask

What is Commons DBCP used for?

This Commons package provides an opportunity to coordinate the efforts required to create and maintain an efficient, feature-rich package under the ASF license. The commons-dbcp2 artifact relies on code in the commons-pool2 artifact to provide the underlying object pool mechanisms.

What is tomcat DBCP?

Tomcat DBCP is just a renamed version of Apache Commons DBCP, with also a different internal package name prefix. At build time, Tomcat fetches the Commons DBCP sources (the version depends on the Tomcat version, for instance Tomcat 7.0. 27 uses Commons DBCP 1.4), and does package name replacement ( org.

What is org Apache Commons DBCP BasicDataSource?

org. apache. commons. dbcp. BasicDataSource is actually a connection pool, from which you can borrow/return connections to any flavour of database: Oracle, Sybase, DB2, etc.


1 Answers

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

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

Something like:

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

}

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. I haven't tested the above code.

like image 147
The Alchemist Avatar answered Oct 07 '22 20:10

The Alchemist