gpt4 book ai didi

c# - OracleDataAdapter,Fill 方法挂起,如何处理连接终止/中断/中断?

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:00 28 4
gpt4 key购买 nike

我有一个连接到远程服务器以查询数据的 C# 程序。数据很大,所以查询大约需要 2 分钟才能完成。在这 2 分钟的时间内,互联网中断了。这导致作业无法完成,程序卡在获取数据例程中。

它建立了连接,但在选择查询期间它被切断了。将命令超时设置为 30 秒无效。遇到此错误时,我需要查询失败,因为程序可以处理失败但无法处理卡住。谢谢!

更新:包含的代码

OracleConnection connection = new OracleConnection(connectionstring);
OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(new OracleCommand(query, connection));
oracleDataAdapter.SelectCommand.CommandTimeout = 30;

DataSet dataSet = new DataSet();
try
{
oracleDataAdapter.Fill(dataSet, table); //Hangs on this line when connection is lost
return dataSet;
}
catch
{
throw;
}
finally
{
dataSet.Dispose();
oracleDataAdapter.Dispose();
}

再次更新:

我需要做的是处理这种情况,因为我不想要悬空进程。

最简单的是,一旦连接丢失,程序将抛出错误。那就是我不知道该怎么做。我以为 commandtimeout 会修复它,但事实并非如此。

最佳答案

有一些重复报告此问题,例如:System being hang when the connection is lost while adapter is filling the datatables

我在 MSDN 上找到了一个很好的话题OP 回答的地方:

I have solved this problem a while back, sorry i forgot to come and let you all know. I worked out that the code stopped executing at that line because (for some reason) there was already an open connection to the database.

Since DA.Fill would open a connection itself if there wasnt one previously opened, it was having a hissy fit and bombing out.

I solved this by putting Connection.Close(); before and after any connection to the database is needed.

基于此,我们可以看到您没有明确地打开到数据库的连接。建议你做一个:

connection.Open();

同时按照 Steve Py 的回答using 确认您正在关闭连接并处理非托管资源。

关于c# - OracleDataAdapter,Fill 方法挂起,如何处理连接终止/中断/中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44916076/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com