我有一个程序使用准备好的语句更新数据库中的表,并且正在尝试查看已实现的所有SQL语句的输出。

我的研究建议P6Spy看起来不错,我下载了它,将P6Spy.jar作为依赖项添加到我的库中,修改为spy.properties文件以连接以使用oracle驱动程序作为realdriver,将spy.properties添加到我的类路径中,并在我的代码中将驱动程序更改为“ com.p6spy.engine.spy.P6SpyDriver”。

一切正常,生成了spy.log文件,但它不包含SQL语句。而是,spy.log文件中的输出是:

1374244954573|-1||debug||com.p6spy.engine.common.P6SpyOptions reloading properties
1374244954578|-1||info||Using properties file: C:\Users\gboss\logging\spy.properties
1374244954579|-1||info||No value in environment for: getStackTrace, using: false
1374244954579|-1||info||No value in environment for: getDeregisterDrivers, using: false
1374244954579|-1||info||No value in environment for: getUsePrefix, using: false
1374244954579|-1||info||No value in environment for: getExecutionThreshold, using: 0
1374244954579|-1||info||No value in environment for: getAutoflush, using: true
1374244954579|-1||info||No value in environment for: getExclude, using:
1374244954579|-1||info||No value in environment for: getExcludecategories, using: info,debug,result,batch
1374244954579|-1||info||No value in environment for: getFilter, using: true
1374244954579|-1||info||No value in environment for: getInclude, using:
1374244954579|-1||info||No value in environment for: getIncludecategories, using:
1374244954579|-1||info||No value in environment for: getLogfile, using: c:/spy.log
1374244954579|-1||info||No value in environment for: getAppender, using: com.p6spy.engine.logging.appender.FileLogger
1374244954579|-1||info||No value in environment for: getRealdriver, using: oracle.jdbc.driver.OracleDriver
1374244954579|-1||info||No value in environment for: getRealdriver2, using:
1374244954580|-1||info||No value in environment for: getRealdriver3, using:
1374244954580|-1||info||No value in environment for: getAppend, using: true
1374244954580|-1||info||No value in environment for: getSpydriver, using: com.p6spy.engine.spy.P6SpyDriver
1374244954580|-1||info||No value in environment for: getDateformat, using:
1374244954580|-1||info||No value in environment for: getDateformatter, using: null
1374244954580|-1||info||No value in environment for: getStringmatcher, using: com.p6spy.engine.common.SubstringMatcher
1374244954580|-1||info||No value in environment for: getStringMatcherEngine, using: com.p6spy.engine.common.SubstringMatcher@13aaa14a
1374244954580|-1||info||No value in environment for: getStackTraceClass, using:
1374244954580|-1||info||No value in environment for: getSQLExpression, using: null
1374244954580|-1||info||No value in environment for: getReloadProperties, using: false
1374244954580|-1||info||No value in environment for: getReloadPropertiesInterval, using: 60
1374244954580|-1||info||No value in environment for: getJNDIContextFactory, using: null
1374244954580|-1||info||No value in environment for: getJNDIContextProviderURL, using: null
1374244954580|-1||info||No value in environment for: getJNDIContextCustom, using: null
1374244954580|-1||info||No value in environment for: getRealDataSource, using: null
1374244954580|-1||info||No value in environment for: getRealDataSourceClass, using: null
1374244954580|-1||info||No value in environment for: getRealDataSourceProperties, using: null


有没有人有使用P6Spy的经验并知道问题出在哪里?我读过spy.log应该只是在完成所有操作并运行程序之后生成sql语句,所以我在这里很困惑

编辑-顺便说一下,数据库已更新,因此SQL语句确实通过并更新了数据库

最佳答案

这通常是由于在注册了真正的JDBC驱动程序之后向DriverManager注册了P6SpyDriver。 P6Spy 1.3不需要您更改JDBC URL。当应用程序代码(或应用程序服务器)尝试从DriverManager获取合适的驱动程序时,它将循环遍历已注册的驱动程序,以查找处理URL的第一个驱动程序。如果在实际驱动程序之前未注册P6SpyDriver,则不会使用它。

有两种方法可以解决此问题:


在您的spy.properties中设置'deregisterdrivers = true'。


这将导致P6Spy注销真正的驱动程序,然后重新注册。这将导致DriverManager使用P6SpyDriver。但是,对于Java 6+,必须确保为实际驱动程序列出的驱动程序类与将自动注册的驱动程序类匹配。您可以从META-INF / services / java.sql.Driver获取此类名称。如果该文件在JDBC驱动程序的JAR文件中不存在,则它不支持自动注册。


在spy.properties中设置'useprefix = true'并修改JDBC URL以添加'p6spy:'前缀。


这将确保无论驱动程序的加载顺序如何,都将使用P6SpyDriver。这实际上是最可靠的方式...

有关1.3可用的各种配置选项,请参见http://p6spy.github.io/p6spy/1.3/configandusage.html

10-07 15:21