Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saillinux/2005045 to your computer and use it in GitHub Desktop.
Save saillinux/2005045 to your computer and use it in GitHub Desktop.
Btrace scripts to monitor DBCP
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
import java.util.Map;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnEvent;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Self;
@BTrace
public class DbcpActiveConnectionMonitor {
private static Map<Object, Object> activeNumsByUrl = newHashMap();
private static final String DS_CLASS = "org.apache.commons.dbcp.BasicDataSource";
@OnMethod(clazz = DS_CLASS, method = "getConnection")
public static void onGetConnection(@Self Object basicDataSource) {
Class<?> dsClass = classOf(basicDataSource);
Object url = getUrl(dsClass, basicDataSource);
Object numberOfActive = getNumberOfActive(basicDataSource, dsClass);
put(activeNumsByUrl,url,numberOfActive);
}
@OnEvent
public static void print() {
println("-------------- active connection numbers by DB connection --------------------");
if (size(activeNumsByUrl) != 0) {
printMap(activeNumsByUrl);
} else {
println("no 'getConnection' calls");
}
Sys.exit(0);
}
private static Object getUrl(Class<?> dsClass, Object basicDataSource) {
Field urlField = field(dsClass, "url");
return get(urlField, basicDataSource);
}
private static Object getNumberOfActive(Object basicDataSource, Class<?> dsClass) {
Field poolField = field(dsClass, "connectionPool");
Object pool = get(poolField, basicDataSource);
Field numActiveField = field(classOf(pool), "_numActive");
Object numActive = get(numActiveField, pool);
return numActive;
}
}
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Self;
@BTrace
public class DbcpMonitor {
private static final String DS_CLASS = "org.apache.commons.dbcp.BasicDataSource";
@OnMethod(clazz = DS_CLASS, method = "getConnection")
public static void onGetConnection(@Self Object basicDataSource) {
Class<?> dsClass = classOf(basicDataSource);
printTitle(dsClass, basicDataSource);
printFields(basicDataSource);
printNumberOfActive(basicDataSource, dsClass);
println("=======================================");
Sys.exit(0);
}
private static void printTitle(Class<?> dsClass, Object basicDataSource) {
Field urlField = field(dsClass, "url");
Object url = get(urlField, basicDataSource);
print("=====DBCP BasicDataSource info (");
print(url);
println(" ) ========");
}
private static void printNumberOfActive(Object basicDataSource, Class<?> dsClass) {
Field poolField = field(dsClass, "connectionPool");
Object numActive = getNumActive(basicDataSource, poolField);
println(concat("===== number of Active : ", str(numActive)));
}
private static Object getNumActive(Object basicDataSource, Field poolField) {
Object pool = get(poolField, basicDataSource);
Field numActiveField = field(classOf(pool), "_numActive");
Object numActive = get(numActiveField, pool);
return numActive;
}
}
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Self;
@BTrace
public class DbcpMonitorSimple {
private static final String DS_CLASS = "org.apache.commons.dbcp.BasicDataSource";
@OnMethod(clazz = DS_CLASS, method = "getConnection")
public static void onGetConnection(@Self Object basicDataSource) {
Field urlField = field(DS_CLASS, "url");
Object url = get(urlField, basicDataSource);
print("=====DBCP BasicDataSource info (");
print(url);
println(" ) ==========");
printFields(basicDataSource);
Field poolField = field(DS_CLASS, "connectionPool");
Object pool = get(poolField, basicDataSource);
println("=====connectionPool (GenericObjectPool) info====");
printFields(pool);
println("==========");
Sys.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment