我有一个webservice,其中有一个webmethod被异步调用,它会根据不同的因素在固定时间间隔和随机时间内被调用(这基本上意味着它可以被多个事物随时调用)。这个webmethod在其主体内部多次调用数据库。有时候会出现超时、死锁和其他需要各种改进的迹象。不过这并不是很重要。我想问的是这个堆栈跟踪:
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()
需要注意的事项:
- PrivateMethod仅在WebMethod中的开头被调用一次
- 没有涉及到递归
- PrivateMethod只是调用存储过程并返回结果。
- 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。不知道这是否重要。
WebMethod
中的catch
块代码吗? - Dennis