1

下面的简单方法是抛出超时错误,我不知道为什么会这样。这可能会连续多次执行,我想知道这是否是原因?

public static Boolean UpdateMessageState(int messageId, int stateId, string message)
{
    var repo = new MailItemRepository();
    try
    {
        var objTask = repo.GetMailByMailId(messageId);

        objTask.State = stateId;
        objTask.Result = message;
        repo.Save();

        return true;
    }
    catch (Exception ex)
    {
        logger.Info(ex.Message);
        logger.Info(ex.InnerException);
        return false;
    }
}

错误跟踪:

2012-07-02 15:26:38.1002|INFO|EF.Methods.MailMethods.UpdateMessageState|System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
The statement has been terminated.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)

编辑:

我已经从等式中删除了 LINQ,现在将其作为 UpdateMessageState

public static Boolean UpdateMessageState(int messageId, int stateId, string message)
    {

        var success = true;

        var conn =
            new SqlConnection(
                "Data Source=...");

        try
        {

            //logger.Info(String.Format("S1:messageId={0},stateId={1},message={2}", messageId, stateId, message));

            var cmdPers = new SqlCommand("procMailQueueStatusUpdate", conn)
                              {
                                  CommandType = CommandType.StoredProcedure
                              };

            cmdPers.Parameters.Add("@MessageId", SqlDbType.Int);
            cmdPers.Parameters.Add("@StateId", SqlDbType.Int);
            cmdPers.Parameters.Add("@Message", SqlDbType.VarChar, -1);

            cmdPers.Parameters["@MessageId"].Value = messageId;
            cmdPers.Parameters["@StateId"].Value = stateId;
            cmdPers.Parameters["@Message"].Value = message;

            conn.Open();
            cmdPers.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            logger.Info(ex.Message);
            success = false;
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
            }
        }
        return success;
    }
}
4

1 回答 1

5

我的猜测是 sql sever 的响应速度比您预期的要慢一些。这可能是由多种原因引起的(内存压力、磁盘 IO、资源等待、网络延迟等)。

只需增加连接的超时时间,您就应该是金色的。您可以进行此更改,将“连接超时”关键字添加到当前连接字符串中

http://www.connectionstrings.com/Articles/Show/all-sql-server-connection-string-keywords

IE

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;Connect Timeout=60

如果失败,请尝试增加命令超时。使用 ADO 时,您可以执行以下操作。

using (SqlConnection con = new SqlConnection())
using(SqlCommand cmd = new SqlCommand())
{            
    cmd.CommandTimeout = 60;
    cmd.Connection = con;
}

要在 linq 中将此属性设置为实体,请使用以下方法

using (MyEntities db = new MyEntities())
{
  db.CommandTimeout = 60;
}
于 2012-07-02T14:43:51.917 回答