【问题标题】:What can a repeating stack trace mean?重复的堆栈跟踪意味着什么?
【发布时间】:2013-11-18 11:28:09
【问题描述】:

我有一个带有 web 方法的 web 服务,该方法在设定的时间间隔内异步调用,并且根据不同的因素随机调用(这基本上意味着它可以随时被多个事物调用)。此网络方法在其主体内多次调用数据库。我有时会收到超时、死锁和其他各种改进必要性的迹象。不过,这不是很相关。我想问的是这个堆栈跟踪:

System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at MyWebservice.PrivateMethod()
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at MyWebservice.PrivateMethod()
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at MyWebservice.PrivateMethod()
   at MyWebservice.WebMethod()

注意事项:

  1. PrivateMethod 在 WebMethod 中只被调用一次,在开始时
  2. 不涉及递归
  3. PrivateMethod 无非就是调用存储过程并返回结果。
  4. PrivateMethod 中只使用了一个 SqlConnection 对象,并且只有一个 SqlConnection.Open 调用。

此问题不仅限于 PrivateMethod,而且似乎与 SqlConnection.Open 有关。它也很少发生,并且只是我前面提到的超时/死锁问题的一小部分 - 所有其他情况都有正常的堆栈跟踪。

任何想法可能导致重复堆栈跟踪?正如我所说,那里的代码没有递归,也无法从 .NET 库内部调用我的 PrivateMethod。

编辑: PrivateMethod 看起来像这样:

using SqlConnection connection = new SqlConnection(connectionString)
{
    SqlCommand command = new SqlCommand("SP name here", connection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@paramName", _param);
    // several other parameters added the same way here

    SqlParameter result = new SqlParameter();
    result.ParameterName = "@result";
    result.DbType = DbType.Boolean;
    result.Direction = ParameterDirection.Output;
    command.Parameters.Add(result);

    connection.Open();
    command.ExecuteNonQuery();

    try { _spresult = (bool)result.Value; }
    catch (InvalidCastException) { return true; }   // this is by design, please don't pester me about it

    return _spresult;
}

如果 WebMethod 的参数之一设置为 true,则在 WebMethod 的最开始调用它,否则根本不调用它。

编辑 2:忘了说,它是 .NET 2.0。不知道这是否重要。

【问题讨论】:

  • 能贴出方法内容和调用方法吗?
  • 哪种方法? PrivateMethod在WebMethod一开始就被调用,无非是标准——使用SqlConnection,声明和定义SqlCommand,添加参数,打开连接,执行非查询,返回结果。真的有必要显示确切的代码吗?
  • 发布所谓的内容可能更重要。我想不出与多线程代码有什么关系会导致这样奇怪的堆栈跟踪,我想看看代码,看看是否有你可能丢失的微妙原因。
  • 你如何得到这个堆栈跟踪?这是您在调试器的异常详细信息中看到的内容,还是您从任何日志中读取的内容?
  • @AdamHouldsworth 什么叫 WebMethod?据我所知,它是一个简单的控制台应用程序,它被各种数据集调用,并为一组中的每个项目调用 WebMethod。不过,我不知道该应用程序到底是如何使用的。

标签: c# .net-2.0 stack-trace sqlexception


【解决方案1】:

这应该可以解决您的线程问题 在类中添加静态 lockObj

var isIdle = true;

if (isIdle)
        {
            lock (padlock)
            {
                if (isIdle)
                {
                  isIdle = false;
using SqlConnection connection = new SqlConnection(connectionString)
{
SqlCommand command = new SqlCommand("SP name here", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@paramName", _param);
// several other parameters added the same way here

SqlParameter result = new SqlParameter();
result.ParameterName = "@result";
result.DbType = DbType.Boolean;
result.Direction = ParameterDirection.Output;
command.Parameters.Add(result);

connection.Open();
command.ExecuteNonQuery();

try { _spresult = (bool)result.Value; }
catch (InvalidCastException) { return true; }   // this is by design, please don't pester me about it

}
                }
            }
        }
        return _spresult;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多