与 DB2 的 ODBC 连接挂起
我有一个函数,用于通过 ODBC 向 DB2 发送写入查询,如果我失去与 DB2 的连接,该函数会不时地挂起。我向该函数发送 60 作为超时,但它永远不会超时。它只是无限期地挂起我的线程,而且我不确定强制该函数放弃的好方法。
public int WriteQuery(string query, string dbConnStr, int timeout)
{
int rowsAffected = -1;
OdbcConnection conn = new OdbcConnection(dbConnStr);
try
{
conn.Open();
OdbcCommand command = new OdbcCommand(query, conn);
command.CommandTimeout = timeout;
OdbcTransaction trans = conn.BeginTransaction();
command.Transaction = trans;
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
adapter.UpdateCommand = command;
rowsAffected = command.ExecuteNonQuery();
trans.Commit();
}
catch(Exception)
{
throw;
}
finally
{
conn.Close();
conn.Dispose();
}
return rowsAffected;
}
}
I have a function I use to send write queries to DB2 through ODBC, and it gets hung up in this function from time to time if I lose my connection to DB2. I am sending 60 as my timeout to the function, but it never times out. It just hangs up my thread indefinitely, and I'm not sure of a good way to force this function to give up.
public int WriteQuery(string query, string dbConnStr, int timeout)
{
int rowsAffected = -1;
OdbcConnection conn = new OdbcConnection(dbConnStr);
try
{
conn.Open();
OdbcCommand command = new OdbcCommand(query, conn);
command.CommandTimeout = timeout;
OdbcTransaction trans = conn.BeginTransaction();
command.Transaction = trans;
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
adapter.UpdateCommand = command;
rowsAffected = command.ExecuteNonQuery();
trans.Commit();
}
catch(Exception)
{
throw;
}
finally
{
conn.Close();
conn.Dispose();
}
return rowsAffected;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论如何,您都以相同的方法打开和关闭连接。
尝试使用:OdbcConnection.ConnectionTimeout=60;
希望这有帮助!
Anyways you are opening and closing the connection in same method.
Try using : OdbcConnection.ConnectionTimeout=60;
Hope this helps!