我正在尝试在应用程序中使用 Oracle 的托管数据访问客户端(版本 4.121.1.0)。我对数据库进行小查询没有问题,但我遇到了返回大结果的查询问题。
我从一个包含大约 137,000 条记录的表中选择两列(所有行)。一列是一个数字 (id),另一列是一大串文本。我正在使用数据读取器将所有带有 id 的 clob 数据读取到对象列表中。所有这一切都很好,大约需要 10 分钟才能完成(这些团 block 可能很大)。
填充数组后,我在连接上调用 Close() 方法并等待、等待、等待……连接关闭大约需要 1 小时 25 分钟。一旦连接关闭,应用程序将继续正常运行。为什么关闭连接需要这么长时间?
这是我当前代码的一个示例,它展示了这个问题。
List<StudentData> studentData = new List<StudentData>();
using (OracleConnection connection = new OracleConnection(this.ConnectionString))
{
try
{
// Get all the clobs
OracleCommand cmdGetClobs = new OracleCommand("SELECT STUDENT_NUMBER, CUSTOM FROM PS.STUDENTS", connection);
connection.Open();
var rdr = cmdGetClobs.ExecuteReader();
while (rdr.Read())
{
System.Char[] clobData = new System.Char[rdr.GetOracleClob(1).Length];
rdr.GetChars(1, 0, clobData, 0, (int)rdr.GetOracleClob(1).Length);
string studentNumber = rdr["STUDENT_NUMBER"].ToString().Trim();
StudentData data = new StudentData()
{
Student_Number = studentNumber,
ClobValue = clobData
};
studentData.Add(data);
}
// I've tried with, and without calling the dispose methods.
rdr.Dispose();
cmdGetClobs.Dispose();
connection.Close(); // <--- App will hang here for about 1.5 hours
connection.Dispose();
}
finally
{
if (connection.State != System.Data.ConnectionState.Closed)
{
connection.Close();
}
}
}
最佳答案
OracleClob 自己实现了 IDisposable。根据您拥有的学生数量,将有很多 Clob 需要清理。
你能做这样的事情看看是否有帮助吗?
while (rdr.Read())
{
using (OracleClob clobData = rdr.GetOracleClob(1))
using (StreamReader reader = new StreamReader(clobData))
{
string studentNumber = rdr["STUDENT_NUMBER"].ToString().Trim();
StudentData data = new StudentData()
{
Student_Number = studentNumber,
ClobValue = reader.ReadToEnd()
};
studentData.Add(data);
}
}
关于.net - Oracle ManagedDataAccess Client 和关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22486255/