{"id":6802,"date":"2024-07-28T07:01:01","date_gmt":"2024-07-27T23:01:01","guid":{"rendered":""},"modified":"2024-07-28T07:01:01","modified_gmt":"2024-07-27T23:01:01","slug":"python\u76d1\u63a7\u65e5\u5fd7\u811a\u672c_python\u8bb0\u5f55\u65e5\u5fd7","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6802.html","title":{"rendered":"python\u76d1\u63a7\u65e5\u5fd7\u811a\u672c_python\u8bb0\u5f55\u65e5\u5fd7"},"content":{"rendered":"
P6Spy\u4f7f\u7528\uff1a[url]http:\/\/donald-draper.iteye.com\/blog\/[\/url]
\n
P6Spy\u6e90\u7801\u5206\u6790-\u5c5e\u6027\u6587\u4ef6\u52a0\u8f7d\uff1a[url]http:\/\/donald-draper.iteye.com\/admin\/blogs\/[\/url]
\n
P6Spy\u6e90\u7801\u5206\u6790-Connection\u83b7\u53d6\uff0c\u65e5\u5fd7\u6253\u5370\uff1a[url]http:\/\/donald-draper.iteye.com\/admin\/blogs\/[\/url]
\n
\u4f7f\u7528P6Spy\u7684\u65f6\u5019\u7528\u5230\u8fd9\u4e00\u53e5\u6211\u4eec\u6765\u770b\u8fd9\u4e00\u53e5\u7684\u5185\u6db5\uff1a
\n
P6DataSource p6DSource = new P6DataSource(cpDSource)
\n <\/p>\n
\/\/ p6DSource = new P6DataSource(cpDSource)
public class P6DataSource extends P6Base
implements DataSource, Referenceable, Serializable
{ \n
\/\/source\u662f\u901a\u8fc7\u6784\u9020\u4f20\u5165\u7684\u6570\u636e\u6e90c3p0\u6216Druid
public P6DataSource(DataSource source)
{ \n
rds = source;
}
\/\/\u521d\u59cb\u5316\u9a71\u52a8\u53ca\u65e5\u5fd7\u6a21\u5757
public static void initMethod()
{ \n
P6SpyDriverCore.initMethod((com.p6spy.engine.spy.P6SpyDriver.class).getName());
}
\/\/\u83b7\u53d6Connection
public Connection getConnection()
throws SQLException
{ \n
if(rds == null)
bindDataSource();
return P6SpyDriverCore.wrapConnection(rds.getConnection());
}
protected DataSource rds;
protected String rdsName;
\/\/\u901a\u8fc7static\u8bed\u53e5\u5757\u8c03\u7528\u521d\u59cb\u5316\u65b9\u6cd5
static
{ \n
initMethod();
}
}<\/code><\/pre>\n\u8d85\u65f6\u65e5\u5fd7\u7ebf\u8def\u89e3\u6790\uff1a
\n
P6DataSource\uff0c\u7684getConnection\u65b9\u6cd5\u901a\u8fc7 P6SpyDriverCore.wrapConnection(rds.getConnection())\u83b7\u53d6\u8fde\u63a5;
\n
\u518d\u6765\u770b\u770bP6SpyDriverCore\u7684wrapConnection\u7684\u65b9\u6cd5\u90fd\u505a\u4e86\u4ec0\u4e48\u4e8b\u60c5
\n <\/p>\n
public abstract class P6SpyDriverCore
implements Driver
{ \n
public static synchronized void initMethod(String spydriver)
{ \n
if(initialized)
return;
String path = P6SpyProperties.getPropertiesPath();
if(path == null)
{ \n
foundSpyProperties = false;
return;
}
foundSpyProperties = true;
\/\/\u521d\u59cb\u5316spy.properties\u5c5e\u6027\u6587\u4ef6
P6SpyProperties properties = new P6SpyProperties();
P6SpyOptions coreOptions = new P6SpyOptions();
OptionReloader.add(coreOptions, properties);
String className = \"no class\";
String classType = \"driver\";
try
{ \n
\/\/realdriver
ArrayList driverNames = null;
\/\/\u65e5\u5fd7\u6a21\u5757
ArrayList modules = null;
\/\/\u83b7\u53d6\u9a71\u52a8\u540d
driverNames = P6SpyOptions.allDriverNames();
\/\/\u83b7\u53d6\u6240\u6709\u65e5\u5fd7\u6a21\u5757
modules = P6SpyOptions.allModules();
boolean hasModules = modules.size() > 0;
Iterator i = null;
classType = \"driver\";
Driver realDriver;
for(i = driverNames.iterator(); i.hasNext(); P6LogQuery.logDebug(\"Registered driver: \" + className + \", realdriver: \" + realDriver))
{ \n
P6SpyDriver spy = null;
if(hasModules)
{ \n
spy = new P6SpyDriver();
DriverManager.registerDriver(spy);
}
className = (String)i.next();
deregister(className);
realDriver = (Driver)P6Util.forName(className).newInstance();
if(P6SpyOptions.getDeregisterDrivers())
\/\/\u6ce8\u518c\u9a71\u52a8realdriver=com.mysql.jdbc.Driver
DriverManager.registerDriver(realDriver);
if(hasModules)
{ \n
spy.setPassthru(realDriver);
realDrivers.add(realDriver);
}
}
if(hasModules)
{ \n
factories = new ArrayList();
classType = \"factory\";
com.p6spy.engine.common.P6Options options;
for(i = modules.iterator(); i.hasNext(); P6LogQuery.logDebug(\"Registered factory: \" + className + \" with options: \" + options))
{ \n
className = (String)i.next();
\/\/module.log=com.p6spy.engine.logging.P6LogFactory
\/\/module.outage=com.p6spy.engine.outage.P6OutageFactory
P6Factory factory = (P6Factory)P6Util.forName(className).newInstance();
factories.add(factory);
options = factory.getOptions();
if(options != null)
OptionReloader.add(options, properties);
}
}
initialized = true;
for(Enumeration e = DriverManager.getDrivers(); e.hasMoreElements(); P6LogQuery.logDebug(\"Driver manager reporting driver registered: \" + e.nextElement()));
}
catch(Exception e)
{ \n
String err = \"Error registering \" + classType + \" [\" + className + \"]\\nCaused By: \" + e.toString();
P6LogQuery.logError(err);
throw new P6DriverNotFoundError(err);
}
}
\/\/P6DataSource\u7684getConnection\u65b9\u6cd5\u6761\u7528P6SpyDriverCore\u7684wrapConnection(Connection realConnection)\u65b9\u6cd5
public static Connection wrapConnection(Connection realConnection)
throws SQLException
{ \n
Connection con = realConnection;
if(factories != null)
{ \n
for(Iterator it = factories.iterator(); it.hasNext();)
{ \n
P6Factory factory = (P6Factory)it.next();
\/\/\u8fd9\u91cc\u662f\u91cd\u70b9\uff0c\u8fd9\u91cc\u662f\u901a\u8fc7P6Factory\u6765\u83b7\u53d6\u8fde\u63a5\uff0cP6SpyDriverCore
\/\/\u5728\u521d\u59cb\u5316initMethod\u5df2\u7ecfP6LogFactory\uff0cP6OutageFactory
\/\/module.log=com.p6spy.engine.logging.P6LogFactory
\/\/module.outage=com.p6spy.engine.outage.P6OutageFactory
con = factory.getConnection(con);
}
}
return con;
}
protected Driver passthru;
protected static boolean initialized = false;
protected static ArrayList factories;
protected static ArrayList realDrivers = new ArrayList();
protected static boolean foundSpyProperties;
}<\/code><\/pre>\n\u8fd9\u4e00\u53e5\u5f88\u91cd\u8981\uff1acon = factory.getConnection(con)\uff0c\u8fd9\u4e2afactory\u5b9e\u9645\u4e0a\u662fP6LogFactory\u6216P6OutageFactory
\n
\u518d\u6765\u770b\u770bP6OutageFactory\u7684getConnection()\u7684\u65b9\u6cd5
\n <\/p>\n
public class P6OutageFactory extends P6CoreFactory
{ \/\/\u8fd4\u56deP6OutageConnection
public Connection getConnection(Connection conn)
throws SQLException
{ \n
return new P6OutageConnection(this, conn);
}
\/\/\u8fd4\u56deP6OutagePreparedStatement
public PreparedStatement getPreparedStatement(PreparedStatement real, P6Connection conn, String p0)
throws SQLException
{ \n
return new P6OutagePreparedStatement(this, real, conn, p0);
}
}<\/code><\/pre>\n\u518d\u770bP6OutageConnection
\n <\/p>\n
public class P6OutageConnection extends P6Connection
implements Connection
{ \n
\/\/\u63d0\u4ea4
public void commit()
throws SQLException
{ \n
long startTime = System.currentTimeMillis();
\/\/outagedetection=true
if(P6OutageOptions.getOutageDetection())
\/\/P6OutageDetector,\u6ce8\u518cstatement\u884c\u4e3acommit\u4e8b\u4ef6
P6OutageDetector.getInstance().registerInvocation(this, startTime, \"commit\", \"\", \"\");
try
{ \n
passthru.commit();
}
finally
{ \n
if(P6OutageOptions.getOutageDetection())
P6OutageDetector.getInstance().unregisterInvocation(this);
}
}
}<\/code><\/pre>\n\u518d\u770bP6Connection
\n <\/p>\n
public class P6Connection extends P6Base
implements Connection
{ \n
\u83b7\u53d6\u9884\u7f16\u8bd1Statement,\u770b\u5230\u8fd9\u662f\u4e0d\u662f\u5f88\u719f\u6089\u7684JDBC\u7684Connection.prepareStatement(String sql)
public PreparedStatement prepareStatement(String p0)
throws SQLException
{ \n
\u8fd9\u91cc\u6761\u7528\u7684\u5b9e\u9645\u5c31\u662fP6OutageFactory\u7684getPreparedStatement\u7684\u65b9\u6cd5\uff0c\u8fd4\u56deP6OutagePreparedStatement
return getP6Factory().getPreparedStatement(passthru.prepareStatement(p0), this, p0);
}
}<\/code><\/pre>\n\u518d\u770bP6OutagePreparedStatement
\n <\/p>\n
public class P6OutagePreparedStatement extends P6PreparedStatement
implements PreparedStatement
{ \n
public boolean execute()
throws SQLException
{ \n
long startTime = System.currentTimeMillis();
\/\/outagedetection=true
if(P6OutageOptions.getOutageDetection())
\/\/\u5c06statement\u6ce8\u518c\u5230P6OutageDetector\uff08statement\u8d85\u65f6\u63a2\u6d4b\u5668\uff09
P6OutageDetector.getInstance().registerInvocation(this, startTime, \"statement\", preparedQuery, getQueryFromPreparedStatement());
boolean flag;
try
{ \n
flag = prepStmtPassthru.execute();
}
finally
{ \n
\/\/outagedetection=true
if(P6OutageOptions.getOutageDetection())
\/\/\u5f53statement\u6267\u884c\u5b8c\u6bd5\uff0c\u5c06statement\u4eceP6OutageDetector\uff08statement\u8d85\u65f6\u63a2\u6d4b\u5668\uff09\u79fb\u9664
P6OutageDetector.getInstance().unregisterInvocation(this);
}
return flag;
}
}<\/code><\/pre>\n\u518d\u770bP6OutageOptions
\n <\/p>\n
\/\/\u8d85\u65f6\u5c5e\u6027\u914d\u7f6e
\/\/outagedetection=true
\/\/outagedetectioninterval=5
public class P6OutageOptions extends P6Options
{ \n
public static boolean getOutageDetection()
{ \n
return outageDetection;
}
public static void setOutageDetection(String _outagedetection)
{ \n
outageDetection = P6Util.isTrue(_outagedetection, false);
}
public static long getOutageDetectionInterval()
{ \n
return outageDetectionInterval;
}
public static long getOutageDetectionIntervalMS()
{ \n
return outageMs;
}
public static void setOutageDetectionInterval(String _outagedetectioninterval)
{ \n
outageDetectionInterval = P6Util.parseLong(_outagedetectioninterval, -1L);
outageMs = outageDetectionInterval * 1000L;
}
protected static boolean outageDetection;
protected static long outageDetectionInterval;
protected static long outageMs;
}<\/code><\/pre>\n\u518d\u770bP6OutageDetector\uff08statement\u8d85\u65f6\u63a2\u6d4b\u5668\uff09
\n <\/p>\n
\/\/\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7ebf\u7a0b
public class P6OutageDetector
implements Runnable
{ \n
protected P6OutageDetector()
{ \n
pendingMessages = null;
haltThread = false;
pendingMessages = new Hashtable();
P6LogQuery.logDebug(\"P6Spy - P6OutageDetector has been invoked.\");
P6LogQuery.logDebug(\"P6Spy - P6OutageOptions.getOutageDetectionIntervalMS() = \" + P6OutageOptions.getOutageDetectionIntervalMS());
}
\/\/\u5355\u4f8b\u6a21\u5f0f\u83b7\u53d6\u63a2\u6d4b\u5668\u5b9e\u4f8b
public static synchronized P6OutageDetector getInstance()
{ \n
if(instance == null)
{ \n
instance = new P6OutageDetector();
ThreadGroup group = new ThreadGroup(\"P6SpyThreadGroup\");
\/\/\u653e\u5728\u540e\u518d\u8fd0\u884c
group.setDaemon(true);
Thread outageThread = new Thread(group, instance, \"P6SpyOutageThread\");
outageThread.start();
}
return instance;
}
public void run()
{ \n
while(!haltThread)
{ \n
detectOutage();
try
{ \n
\/\/\u7761\u7720outagedetectioninterval=5\u79d2
Thread.sleep(P6OutageOptions.getOutageDetectionIntervalMS());
}
catch(Exception e) { }
}
}
public void shutdown()
{ \n
haltThread = true;
}
\/\/\u5c06statement\u6ce8\u518c\u5230\u63a2\u6d4b\u5668
public void registerInvocation(Object jdbcObject, long startTime, String category, String ps, String sql)
{ \n
\/\/pendingMessages\u5176\u5b9e\u662f\u4e00\u4e2aHashMap\uff0cprivate Hashtable pendingMessages;
pendingMessages.put(jdbcObject, new InvocationInfo(startTime, category, ps, sql));
}
\/\/\u5c06statement\u4ece\u63a2\u6d4b\u5668\u79fb\u9664
public void unregisterInvocation(Object jdbcObject)
{ \n
pendingMessages.remove(jdbcObject);
}
\/\/\u63a2\u6d4b\u662f\u5426\u6709\u8d85\u65f6\u7684statement
private void detectOutage()
{ \n
int listSize = pendingMessages.size();
if(listSize == 0)
return;
P6LogQuery.logDebug(\"P6Spy - detectOutage.pendingMessage.size = \" + listSize);
long currentTime = System.currentTimeMillis();
long threshold = P6OutageOptions.getOutageDetectionIntervalMS();
Set keys = pendingMessages.keySet();
for(Iterator keyIter = keys.iterator(); keyIter.hasNext();)
{ \n
InvocationInfo ii = (InvocationInfo)pendingMessages.get(keyIter.next());
\/\/\u5224\u65adstatement\u662f\u5426\u8d85\u65f6
if(ii != null && currentTime - ii.startTime > threshold)
{ \n
\/\/\u6253\u5370\u65e5\u5fd7
P6LogQuery.logDebug(\"P6Spy - statement exceeded threshold - check log.\");
logOutage(ii);
}
}
}
\/\/\u6253\u5370\u65e5\u5fd7
private void logOutage(InvocationInfo ii)
{ \n
P6LogQuery.logElapsed(-1, ii.startTime, \"OUTAGE\", ii.preparedStmt, ii.sql);
}
private Hashtable pendingMessages;
private boolean haltThread;
private static P6OutageDetector instance = null;
private static final boolean debug = true;
}<\/code><\/pre>\nP6LogQuery\u7c7b\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5206\u6790\u4e86\uff0c\u5728\u524d\u9762\u5df2\u7ecf\u8bf4\u8fc7\u4e86
\n
[size=medium]\u603b\u7ed3\uff1a[\/size]
\n
[color=green]\u901a\u8fc7\u4ee5\u4e0a\u7684\u5206\u6790\uff0c\u76f8\u4fe1\u5927\u5bb6\u5bf9P6Spy\u8d85\u65f6\u63a2\u6d4b\uff0cconnection\u83b7\u53d6\uff0c\u65e5\u5fd7\u6253\u5370\u6709\u4e86
\n
\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4e3b\u8981 \u70b9\uff0cstatic\u8bed\u53e5\u5757\uff0c\u521d\u59cb\u5316\u7c7b\uff0c\u5229\u7528\u5de5\u573a\u6a21\u5f0f\u83b7\u53d6connection\uff0c
\n
\u5355\u4f8b\u6a21\u5f0f\u83b7\u53d6\u63a2\u6d4b\u5668\uff0c\u65e5\u5fd7\u7684\u6253\u5370\u4e3b\u8981\u901a\u8fc7\u5c01\u88c5\u5b9e\u73b0jdbc\u7684statement\u6765\u5d4c\u5165\u3002[\/color]<\/p>\n","protected":false},"excerpt":{"rendered":"python\u76d1\u63a7\u65e5\u5fd7\u811a\u672c_python\u8bb0\u5f55\u65e5\u5fd7P6Spy\u4f7f\u7528\uff1a[url]http:\/\/donald-draper.iteye.com\/blog\/[\/url]P6Spy\u6e90\u7801\u5206\u6790-\u5c5e\u6027\u6587\u4ef6\u52a0\u8f7d...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6802"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6802"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6802\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}