0

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);
    }
}
3
  • what is exactly in your connection string? Commented Sep 25, 2013 at 8:42
  • connectionString="server=localhost; logging=true;user id=*****;pwd=****database=shopdb;port=3306;persist security info=true;allow user variables=false;allow zero datetime=true" Commented Sep 25, 2013 at 8:45
  • 1
    i suggest you read this carefully : dev.mysql.com/doc/refman/5.0/es/… Commented Sep 25, 2013 at 8:52

1 Answer 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 ?

Sign up to request clarification or add additional context in comments.

3 Comments

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
I am trying to help him brother @Flicker. why you told me this thing brother?
I think this is a misunderstanding. No worries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.