0

我有一个非常简单的服务,它一直在早期工作,但现在我将它升级到更新的 .NET 版本,它已经停止工作,出现如下错误消息:

超时已过。在从池中获取连接之前超时时间已过。这可能是因为所有池连接都在使用中并且达到了最大池大小。

我试图执行服务尝试运行的存储过程,并在 ms sql server management studio 中直接运行它需要不到 1 秒,所以我不明白可能是什么问题?使用相同数据库的客户端(虽然不是相同的存储过程)在同时使用它们时没有任何超时问题。

编辑所以你可以看到代码:

这在服务启动时调用:

Private Sub SetupGetBatch1Command()
    GetBatch1Command = New System.Data.SqlClient.SqlCommand("spgetTheData1")
    GetBatch1Command.CommandType = Data.CommandType.StoredProcedure
End Sub

这有时间间隔:

Private Function GetBatch1() As DataSet
    Dim dataset As New DataSet
    Dim adapter As Data.SqlClient.SqlDataAdapter
    Dim cn As New System.Data.SqlClient.SqlConnection(connectionstring())
    GetBatch1Command.Connection = cn
    adapter = New Data.SqlClient.SqlDataAdapter(GetBatch1Command)
    adapter.Fill(dataset)
    Return dataset
End Function

我没有这样做,也不太了解它,但我想我至少应该遵循以下建议并在某处添加 cn.dispose() ?

还有其他类似的数据库连接,都以与上面示例完全相同的方式完成。

4

1 回答 1

1

这几乎总是由于未能处置/关闭连接对象造成的。仔细检查您的代码并确保您的所有SqlConnection对象都已处理,最好使用以下using语句:

using (SqlConnection c = new SqlConnection(...))
{
    // use the connection...
    ...
} // c.Dispose() will be called automatically here, even if an exception is thrown.

我怀疑升级到新的 .NET 版本是一个红鲱鱼,或者只是间接导致问题:例如,您可能会遇到以前没有遇到的异常,并且您的代码可能无法处理/关闭抛出异常时正确连接。

于 2012-07-06T09:43:52.553 回答