我使用jdbc连接到一个数据库,并将多个行作为ResultSet的一部分。我在ResultSet上运行了一个sql,但不知何故最终只运行了第一行。例如: query1是我的结果集,它生成两个pid行。然后,我使用query2来终止pid。我希望query2遍历query1中的所有行,并一个接一个地执行terminate命令。另一点需要注意的是,根据结果集,query1可以有2行以上的行。下面是我正在使用的代码,任何帮助都是非常感谢的。顺便说一句,我确实尝试过在while中使用嵌套的for循环,但它不会像我在数据库日志中看到的那样重新发出query2。
Statement stmt = connection.createStatement();
ResultSet res = stmt.executeQuery(
"SELECT \n" +
"kl.pid as blocking_pid,\n" +
"ka.usename as blocking_user,\n" +
"ka.query as blocking_query,\n" +
"bl.pid as blocked_pid,\n" +
"a.usename as blocked_user, \n" +
"a.query as blocked_query, \n" +
"to_char(age(now(), a.query_start),'HH24h:MIm:SSs') as age\n" +
"FROM pg_catalog.pg_locks bl\n" +
"JOIN pg_catalog.pg_stat_activity a \n" +
"ON bl.pid = a.pid\n" +
"JOIN pg_catalog.pg_locks kl \n" +
"ON bl.locktype = kl.locktype\n" +
"and bl.database is not distinct from kl.database\n" +
"and bl.relation is not distinct from kl.relation\n" +
"and bl.page is not distinct from kl.page\n" +
"and bl.tuple is not distinct from kl.tuple\n" +
"and bl.virtualxid is not distinct from kl.virtualxid\n" +
"and bl.transactionid is not distinct from kl.transactionid\n" +
"and bl.classid is not distinct from kl.classid\n" +
"and bl.objid is not distinct from kl.objid\n" +
"and bl.objsubid is not distinct from kl.objsubid\n" +
"and bl.pid <> kl.pid \n" +
"JOIN pg_catalog.pg_stat_activity ka \n" +
"ON kl.pid = ka.pid\n" +
"WHERE kl.granted and not bl.granted\n" +
"ORDER BY a.query_start");
//Get all the blocking pids and run pg_terminate_backend
while (res.next()) {
String pid = res.getString("blocking_pid");
stmt.execute("SELECT pg_terminate_backend(" + String.valueOf(pid) + ")");
}
stmt.close();
connection.close();发布于 2015-03-19 04:25:55
我想通了。以防任何人面临同样/类似的问题。我用相同的变量重用了query2中的jdbc连接。一旦我在query2中使用了单独的jdbc调用,它就可以工作了。
https://stackoverflow.com/questions/29063022
复制相似问题