与 DB2 的 ODBC 连接挂起

发布于 2024-09-11 14:43:44 字数 950 浏览 3 评论 0原文

我有一个函数,用于通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-09-18 14:43:44

无论如何,您都以相同的方法打开和关闭连接。
尝试使用:OdbcConnection.ConnectionTimeout=60;

希望这有帮助!

Anyways you are opening and closing the connection in same method.
Try using : OdbcConnection.ConnectionTimeout=60;

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文