0

Im using MySql.Data on C# and a INSERT query is throwing a exception:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.    at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
       at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
       at MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(Exception ex)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
       at WarehouseMgr.SyncServiceLib.RequestCreator.CreateRequest(RequestPackage package, MySqlConnection db, ILog log, ConfigFile config, String log_prefix) in z:\WORKSPACE\Web\W_WarehouseMgr\WebApp\SyncService_Library\RequestCreator.cs:line 229
       at WarehouseMgr.SyncServiceLib.InputWork.InputWorker.RunWorkOnce() in z:\WORKSPACE\Web\W_WarehouseMgr\WebApp\SyncService_Library\InputWork\InputWorker.cs:line 152

Any ideas?

Edit:

The code for the insert query:

var reqId = 0;


 var reqSql =
            "INSERT INTO `requests` (`uid`, `label_id`, `corridor_id`, `status`) VALUES (@p_uid, @p_label_id, @p_corridor_id, 'waiting');" +
            "SELECT `id` FROM `requests` WHERE uid = @p_uid LIMIT 1;";
        using (var cmd = new MySqlCommand(reqSql, db)) {
            cmd.Parameters.AddWithValue("@p_uid", package.RequestUid);
            cmd.Parameters.AddWithValue("@p_label_id", labelId);
            cmd.Parameters.AddWithValue("@p_corridor_id", corridorId);
            using (var reader = cmd.ExecuteReader()) {
                if (reader.Read()) {
                    reqId = reader.GetInt32("id");
                }
            }
        }

        if (reqId <= 0) {
            log.E(TAG, log_prefix + "Request creation failed");
            throw new Exception("Request creation failed");
        }
Carlos Coelho
  • 31
  • 2
  • 5
  • 2
    It's telling you exactly what the error is...for some reason your query is timing out. Perhaps something else has a lock on the table you're inserting into, or maybe there's something wrong with your insert statement. Hard to say without seeing your full code. – mituw16 Jul 27 '16 at 14:29
  • Possible duplicate of [MySql.Data.MySqlClient.MySqlException: Timeout expired](http://stackoverflow.com/questions/3475867/mysql-data-mysqlclient-mysqlexception-timeout-expired) – Gilad Green Jul 27 '16 at 14:31
  • Others query runs well? So the problem is related only to this INSERT statement? – Igor Damiani Jul 27 '16 at 14:32
  • The others query run well the problem is just on this insert statement – Carlos Coelho Jul 27 '16 at 14:34
  • Running your insert and then also immediately running a select to grab the newly created ID is not considered best practice. You really ought to use the `LastInsertedId` as deomonstrated here. http://stackoverflow.com/a/15057619/1729859 – mituw16 Jul 27 '16 at 14:40
  • True, and i can change that. But que query only fails sometimes – Carlos Coelho Jul 27 '16 at 14:41
  • If the query only fails sometimes, you could try increasing your timeout in the `cmd.CommandTimeout` property. – mituw16 Jul 27 '16 at 14:54

1 Answers1

0

When making a command set a command timeout. So just add

cmd.CommandTimeout = xxx;

It's in seconds so set as much as you need. 0 = unlimited.