除了检测类的使用,你可以用java.sql.Connection
代理检测它们的生成吗?当您从工厂获得连接时,您会将其包装在您的代理中。当人们使用createStatement()
或其他限制调用时,您的代理将被检测为可以方法调用、记录查询字符串和/或报告堆栈跟踪。
public class ProxyConnection implements Connection {
private Connection realConnection;
public ProxyConnection(Connection realConnection) {
this.realConnection = realConnection;
}
public Statement createStatement() throws SQLException {
// could the offenders
createCounter.incrementAndGet();
// log the callers -- expensive so maybe every 100th or every 10 secs
logger.info("call to createStatment", new Exception("createStatement"));
// maybe just throw
if (throwOnBadCall) {
throw new SQLException("calls to createStatement aren't allowed"));
}
return realConnection.createStatement();
}
如果您不想在生产中变得过于繁重,那么您可以随时计算它们并使用一种volatile boolean logBadCall
标志来启用一段时间的检查以抽样寻找问题。也许最初您会进行一些采样,攻击 80% 的位置,然后只有在您处理完应用程序的高查询负载部分后才能永久启用检测。
如果您没有一个中心位置来包装连接,那么您可能必须将连接池或工厂包装在链上一点。
希望这可以帮助。