采用以下代码:
public class A {
public PreparedStatement pstmt_pointer;
public void cancel() throws SQLException {
if(pstmt_pointer != null) {
System.out.println("Cancelling pstmt_pointer");
pstmt_pointer.cancel();
}
}
Connection conn;
public int getCount() throws SQLException {
long start2 = System.currentTimeMillis();
String sql = "select count(*) from table";
int count = 0;
try (PreparedStatement p_stmt = conn.prepareStatement(sql)) {
pstmt_pointer = p_stmt;
ResultSet result = p_stmt.executeQuery();
result.next();
count = result.getInt("count");
}
long end2 = System.currentTimeMillis();
long elapsed = end2 - start2;
double countExecTime = truncate((float) elapsed / 1000);
System.out.println("Count(*) Execution Time: " + countExecTime + " seconds");
return count;
}
public float totalExecTime;
private static double truncate(float x) {
int y = (int) (x * 100);
return (double) y / 100;
}
}
如果在仍然执行查询(cancel()
)时(从另一个线程)调用ResultSet result = p_stmt.executeQuery();
,接下来执行哪一行代码? result.next();
被打吗?还是被跳过?代码一旦取消,是否会退出try
块?如果p_stmt被取消,此方法的返回值是什么?
我不确定答案是否取决于平台,但我的数据库是Oracle。