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?
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.
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.
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.
I think aspects may be the solution to your quandry. Check out:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With