可以停止正在运行的阅读器吗?
场景:我有一个包含 100000 个数据集的表
CREATE TABLE stock (
uid bigint NOT NULL,
name text,
quantity integer,
x bytea,
y bytea
);
和一个用于读取数据的控制台应用程序(.NET 4.0、Npgsql 2.0.11.0/2.0.11.92)
conn = new NpgsqlConnection("Server=localhost;Database=postgres;User id=postgres;password=postgres;Timeout=600;CommandTimeout=600;ConnectionLifeTime=600;");
using (new ConnectionOpen(conn))
using (var ta = conn.BeginTransaction(IsolationLevel.Snapshot))
{
IDbCommand command = conn.CreateCommand("SELECT * from stock;");
command.SetTransaction(ta);
IDataReader reader = command.ExecuteReader();
int n = 0;
while (!reader.IsClosed && reader.Read())
{
n++;
if (n > 5000)
{
if (reader != null)
{
((NpgsqlDataReader)reader).Close();
}
}
}
((NpgsqlDataReader)reader).Dispose();
reader = null;
}
我观察到数据阅读器无法真正停止。似乎数据读取器首先读取所有行,然后正常返回。
这个例子是一个更大的应用程序的摘要,其中用户将通过按下按钮来停止数据读取器,因为读取时间太长。