我是第一次使用 jdbc 并从一个包含我之前编写的 sql 查询的文件中读取。尽管查询已正确执行并且我的程序继续运行,没有任何明显问题,但在我执行 statement.executeBatch();
之后,我无法再将任何内容写入我的日志。
-我正在使用 java.util.logging
通过 FileHandler
进行日志记录。为了读取我的“.sql”文件,我使用了 BufferedReader
和 FileReader
。我知道我不会分享很多代码来完全理解上下文,但这就是我内存中的全部。使用后我将关闭所有阅读器。
任何想法可能是什么问题?
MyLogger.log(Level.WARNING, "it does write");
statement.executeBatch();
MyLogger.log(Level.WARNING, "it doesn't write anymore");
statement.close();
MyLogger.log(Level.WARNING, "still doesn't");
谢谢
编辑: MyLogger 是一个带有统计日志方法的类
edit2: @Tim Biegeleisen
statement.executeBatch()
返回一个 int 数组,每个批处理一个。我试过了:
try {
int[] results = statement.executeBatch();
for (int result : results)
{
if (result == Statement.EXECUTE_FAILED)
{
MyLogger.log(Level.SEVERE, "batch failed, but driver seems to still be alive.");
System.out.println("batch failed, but driver seems to still be alive.");
}
}
} catch (SQLException e) {
MyLogger.log(Level.SEVERE, "the batch failed, and the driver died too.");
System.out.println("the batch failed, and the driver died too.");
}
它没有打印和记录任何内容。
edit3:我想我对我的关闭钩子(Hook)要求太多了。我不熟悉它,所以我不确定到底是什么问题。
最佳答案
在我看来最有可能的解释是您的代码在点击 executeBatch()
时滚动。在此之后,对记录器的后续调用似乎不起作用,因为它们根本没有被击中。一种简单的测试方法是用一个 try catch block 包围对 executeBatch()
的调用:
try {
int result = statement.executeBatch();
if (result == Statement.EXECUTE_FAILED) {
MyLogger.log(Level.ERROR, "batch failed, but driver seems to still be alive.");
}
} catch (SQLException e) {
MyLogger.log(Level.ERROR, "the batch failed, and the driver died too.");
}
关于java - 使用 jdbc 时的日志记录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768174/