chen4765654

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;
import java.lang.Appendable;
@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)));
  Appendable buffer=BTraceUtils.Strings.newStringBuilder();
  BTraceUtils.Strings.append(buffer,BTraceUtils.timestamp("yyyy-MM-dd HH:mm:ss"));
  BTraceUtils.Strings.append(buffer," - ");
  BTraceUtils.Strings.append(buffer,BTraceUtils.jstackStr());
  BTraceUtils.put(map, conn,str(buffer));
 }
 
 
 @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);
 }
 
 
}

posted on 2014-12-16 16:20 紫色心情 阅读(532) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: