0
votes

I'm trying to simulate a MySql CommandTimeout Exception by the following code. I have a CommandTimeout for 3 seconds, and my query takes about 30 seconds to execute. Why does this code not working? Do I something wrong?

There is no commandtimeout in my connectionstring.

connectionString="server=localhost; logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;allow user variables=false;allow zero datetime=true"

Please help.

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

    cmd.CommandTimeout = 3; // default 30 seconds
    try {
        DateTime start = DateTime.Now;
        cmd.Connection.Open();
        using (MySqlDataReader reader = cmd.ExecuteReader()){
            while (reader.Read()){

            }
            DBFactory.CloseReader(reader);
        }
        cmd.Connection.Close();
        DateTime end = DateTime.Now;

        TimeSpan ts = end - start;

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

    } catch (Exception ex) {
        Response.Write(ex.Message);
    } finally {
        DBFactory.CloseConnection(cmd);
    }
}
1
what is exactly in your connection string? - user1455836
connectionString="server=localhost; logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;allow user variables=false;allow zero datetime=true" - user2783635
i suggest you read this carefully : dev.mysql.com/doc/refman/5.0/es/… - Stephan

1 Answers

1
votes

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 ?