Asp.Net - (Sql) 连接未关闭

Asp.Net - (Sql) The connection was not closed

我的网站经常遇到一个问题。无论我尝试过什么,我都无法摆脱它。

在我的 asp.net 项目中,我使用的是 microfost azure 服务器和数据库。我一次又一次地检查了我所有的查询。之后无论我在哪里打开连接,我都会关闭它。

此外,在我打开 sql 连接之前,几乎每个功能我都会检查连接状态,例如:

if (conn.State != System.Data.ConnectionState.Closed) { conn.Close(); }

如果我多次刷新我的主页,我得到了错误,大多数时候没有任何多次刷新我得到了相同的结果。

**The connection was not closed. The connection's current state is open.**

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The connection was not closed. The connection's current state is open.

任何人都可以帮助我了解在这种情况下我该怎么办?

异常可能表明当您尝试打开连接时连接已经打开。您是否共享由不同线程上的多个进程使用的全局连接?这会导致它,也可能解释为什么问题是间歇性的。

SqlConnection.Open() 的文档中提到了 InvalidOperationException

最好在每次需要时显式创建一个新连接,然后在完成后调用 Dispose()。您可以调用 Close(),但更简单的模式是 using,例如

using(var connection = new SqlConnection("yourConnectionString"))
{
    connection.Open() // open it right before you execute something
}

using 语句确保总是调用 Dispose()Dispose() 呼叫 Close()。因此,如果您按上述方式创建连接,您的连接将始终关闭。

在您 Close()Dispose() 的幕后,框架并未 真正地 关闭连接。至少不是马上。它保留一个打开的连接池并重新使用它们,因为它比打开和关闭连接更有效。因此,如果您打开一个连接,关闭它,打开另一个具有相同连接字符串的连接,然后关闭它,等等,很可能在幕后它实际上是完全相同的连接。但是框架正在幕后为您处理。如果未使用连接,则连接 really 将关闭。参见 connection pooling

因此,虽然尝试重新使用相同的 SqlConnection 似乎很有效,但根据需要创建和 Dispose() 并让连接池管理连接的生命周期真的没问题幕后连接。

您应该在关闭连接时仔细检查异常。

try{
    stmt.close();
    connection.close();
    connection = null;
}catch(Exception e){
    try{
        connection.close();
        connection = null;
    }catch(Exception e){
    }
}