0

我正在尝试通过以下代码模拟 MySql CommandTimeout 异常。我有一个 3 秒的 CommandTimeout,我的查询大约需要 30 秒才能执行。为什么这段代码不起作用?我有什么问题吗?

我的连接字符串中没有命令超时。

connectionString="server=localhost;logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;允许用户变量=false;允许零日期时间=真的”

请帮忙。

使用 (MySqlCommand cmd = new MySqlCommand("select * from order_line", new MySqlConnection("myConnectionString"))) {

    cmd.CommandTimeout = 3; // 默认 30 秒
    尝试 {
        日期时间开始 = 日期时间。现在;
        cmd.Connection.Open();
        使用 (MySqlDataReader reader = cmd.ExecuteReader()){
            while (reader.Read()){

            }
            DBFactory.CloseReader(阅读器);
        }
        cmd.Connection.Close();
        日期时间结束 = 日期时间。现在;

        时间跨度 ts = 结束 - 开始;

        Response.Write(ts.Seconds + "." + ts.Milliseconds);

    } 捕捉(异常前){
        Response.Write(ex.Message);
    } 最后 {
        DBFactory.CloseConnection(cmd);
    }
}
4

1 回答 1

1

Can you please inform me how many records into you order_line table so we can figure out what appends ? Normally CommandTimeout has no effect when the command is executed against a context connection (a SqlConnection opened with "context connection=true" in the connection string).CommandTimeout is the cumulative time-out (for all network packets that are read during the invocation of a method) for all network reads during command execution or processing of the results. A time-out can still occur after the first row is returned, and does not include user processing time, only network read time.

For example, with a 10 second time out, if Read requires two network packets, then it has 10 seconds to read both network packets. If you call Read again, it will have another 10 seconds to read any data that it requires. (Copy from msdn). You need more explanation, bro ?

于 2016-06-13T16:11:09.243 回答