System.Threading.ThreadAbortException错误

时间:2015-05-14 09:57:44

标签: c# multithreading web-services

我有一个会调用SaveEmployee()函数的web方法。

WebMethod功能如下:

[WebMethod]
public bool UpdateEmployee(DataSet dSet)  
{
  try
  {
     COMAccess.Employee.SaveEmployee(Registry.GetDatabaseConnection(),dSet)
  } 
  catch (Exception ex) 
  {
    Log(ex.InnerException.ToString()
  }
}   

SaveEmployee()功能如下:

public static bool SaveEmployee(string connection, DataSet dSet)
{
  bool rtnVal = false;
  SqlConnection  conn = new SqlConnection(connection);
  conn.Open();
  SqlTransaction tran = conn.BeginTransaction();
  SqlCommand command = conn.CreateCommand();
  command.Connection = conn;
  command.Transaction = tran;
  command.CommandTimeout = 320;
  command.CommandText = "SP_UPDATE_EMPLOYEE";
  command.CommandType = System.Data.CommandType.StoredProcedure;

  try 
  {
     foreach(System.Data.DataRow dRow in dSet.Tables["Employee"].Rows)
     {
        command.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = dRow["EmployeeID"];
        command.Parameters.Add("@EmployeeName ", SqlDbType.VarChar).Value = dRow["EmployeeName"];
        command.ExecuteNonQuery(); 
        command.Parameters.Clear();             
     }
     tran.Commit();
     myVal = true;    
  }
  catch (Exception ex)
  {
     tran.Rollback();
     myVal = false;
  }
     return myVal;
  }

无论如何,我收到了以下InnerException消息:

System.Threading.ThreadAbortException: Thread was being aborted.
at SNINativeMethodWrapper.SNIPacketGetConnection(IntPtr packet)
at System.Data.SqlClient.TdsParserStateObject.ProcessSniPacket(IntPtr packet, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByte()
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 COMAccess.Employee.SaveEmployee(String connection, DataSet dSet)

我不知道发生了什么。能有人请赐教吗?感谢

1 个答案:

答案 0 :(得分:0)

在您的代码中,您正在为320秒设置CommandTimeout属性:

command.CommandTimeout = 320;

但是,您的连接仍然具有等于30秒的默认超时,并且在该时间过去之后,该命令的内部线程将被取消为长时间运行。

您必须为连接设置ConnectionTimeout属性,如下所示:

connection.ConnectionTimeout = 320;