BTrace跟踪DBCP泄漏脚本

BTrace跟踪DBCP泄漏脚本

import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import static com.sun.btrace.BTraceUtils.timeMillis;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
import com.sun.btrace.annotations.Return;
import com.sun.btrace.annotations.OnEvent;
import com.sun.btrace.annotations.Self;
import com.sun.btrace.annotations.TLS;
@BTrace public class DataSourceTrace {
 private static Map map = BTraceUtils.newHashMap();
 
 @OnMethod(clazz = "org.apache.commons.dbcp.BasicDataSource", method = "getConnection", location = @Location(Kind.RETURN))
 public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method, @Return Connection conn) {
  //BTraceUtils.println(strcat("获取连接:",BTraceUtils.str(conn)));
  BTraceUtils.put(map, conn, BTraceUtils.jstackStr());
 }
 
 
 @OnMethod(clazz = "org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper", method = "close")
 public static void traceExecute2(@ProbeClassName String name,@ProbeMethodName String method, @Self Connection conn) {
  //BTraceUtils.println(strcat("释放连接:",BTraceUtils.str(conn)));
  BTraceUtils.remove(map, conn);
 }
 
 @OnEvent
 public static void exit(){  
  //这里打印泄漏连接的方法堆栈,运行一段时间后通过Ctrl+C,选择2,发送事件打印
          BTraceUtils.printMap(map);
 }
 
 
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。