下面的简单方法是抛出超时错误,我不知道为什么会这样。这可能会连续多次执行,我想知道这是否是原因?
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;
}
}