有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自执行超时的java Mysql异常

我有以下java代码片段:

Connection conn = DriverManager.getConnection(connString, user, password);
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                                      java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);
stmt.setMaxRows(100);
stmt.setQueryTimeout(2);

try {
    stmt.execute("SELECT SLEEP(100)"); // (1)
} finally {
    stmt.close();
    conn.close();
}

它设计用于模拟将超时的长时间运行的查询。但是,它引发的异常不是预期的异常:

Streaming result set com.mysql.jdbc.RowDataDynamic@ is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries. java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@ is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.

仔细看,当close方法试图将MaxRows重置为其默认值时,似乎发生了错误。但是,如果我将第(1)行替换为

stmt.executeQuery("SELECT SLEEP(100)");

引发了适当的异常com.mysql.jdbc.exceptions.MySQLTimeoutException

我希望运行execute,对结果进行流式处理,对行数进行限制,并且不会出现此错误。有什么建议吗


共 (0) 个答案