简体   繁体   中英

ODBC Connection to DB2 Hangs

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;
    }
}

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM