“System.ArgumentException:无效操作。连接已关闭”。我不断收到此错误

发布于 2024-11-07 23:03:45 字数 1015 浏览 1 评论 0原文

我在服务器上收到此错误。我无法在我的开发机器上复制这个。

当我调用 ExecuteReader、ExecuteScalar 或当我尝试填充数据集时,我会得到它。

我用的是oracle数据库。

我认为,当服务器负载增加时,这一点就会增加。我不知道。

我需要帮助解决这个问题。如果您需要更多详细信息,请告诉我。

ExecuteScalar 的代码如下

Public Function ExecuteScalar1(ByVal sExecuteString As String, ByVal sConnectString     As     String) As String
        Dim OrclCmd As New OracleCommand
        Try
            If OpenConnection(sConnectString) Then
                OrclCmd.CommandText = sExecuteString
                OrclCmd.CommandType = CommandType.Text
                OrclCmd.Connection = OrclConn
                ExecuteScalar_ = Convert.ToString(OrclCmd.ExecuteScalar())
                If ExecuteScalar_ Is System.DBNull.Value Then
                    ExecuteScalar_ = ""
                End If

            End If
        Catch ex As Exception
            Err.Raise(Err.Number, Err.Source, Err.Description)
        Finally
            Call CloseConnection()
        End Try
    End Function

I get this error on the server. I cannot replicate this on my development machine.

I get it when i call ExecuteReader, ExecuteScalar or when i try to fill a dataset.

I use an oracle database.

This, i think, increases when the load on the server increases. Im not sure.

i need help fixing this. Please let me know if you need anymore details.

The code for ExecuteScalar would be as follows

Public Function ExecuteScalar1(ByVal sExecuteString As String, ByVal sConnectString     As     String) As String
        Dim OrclCmd As New OracleCommand
        Try
            If OpenConnection(sConnectString) Then
                OrclCmd.CommandText = sExecuteString
                OrclCmd.CommandType = CommandType.Text
                OrclCmd.Connection = OrclConn
                ExecuteScalar_ = Convert.ToString(OrclCmd.ExecuteScalar())
                If ExecuteScalar_ Is System.DBNull.Value Then
                    ExecuteScalar_ = ""
                End If

            End If
        Catch ex As Exception
            Err.Raise(Err.Number, Err.Source, Err.Description)
        Finally
            Call CloseConnection()
        End Try
    End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2024-11-14 23:03:45

您的 OpenConnection 方法有什么作用?

我假设它在完全独立的 OracleCommand 对象上打开数据库连接。我建议这里最简单的解决方案是将 ByRef 中的 OracleCommand 对象传递给 OpenConnection 方法,允许您关联连接并在该方法中打开它。

显然,这将要求您更改 OpenConnection 方法以采用 ConnectionString 参数以及引用的 OracleCommand 对象,签名为:

Public Sub OpenConnection(ByVal sConnectionString As String, ByRef orclCommand As OracleCommand)

这将允许您在两个方法中使用 OracleCommand 对象,并且它们都引用相同的对象 - 因此允许您在连接上调用 .Open() ,并在两种方法中打开连接。

重新阅读您的代码...

您似乎有一个名为 OrclConn 的对象,您将其分配给 OrclCmd.Connection

我的心理调试告诉我这是一个在此函数之外声明的静态对象。如果是这样,那就有问题了 - 当多个用户访问此代码时,OrclConn 对象可以在执行命令时让另一个用户关闭其连接。共享对象上的典型竞争条件。

解决方案是使用函数本地的连接对象:

Public Function ExecuteScalar1(ByVal sExecuteString As String, ByVal sConnectString As String) As String
    Dim OrclCmd As New OracleCommand
    Dim OrclConn As New OracleConnection
    Try
        OrclConn.ConnectionString = sConnectString
        OrclConn.Open() 
        'Add any connection initialisation here

        OrclCmd.CommandText = sExecuteString
        OrclCmd.CommandType = CommandType.Text
        OrclCmd.Connection = OrclConn

        ExecuteScalar_ = Convert.ToString(OrclCmd.ExecuteScalar())

        If ExecuteScalar_ Is System.DBNull.Value Then
            ExecuteScalar_ = ""
        End If
    Catch ex As Exception
        Err.Raise(Err.Number, Err.Source, Err.Description)
    Finally
        If OrclConn.State <> ConnectionState.Closed Then ' Can't remember if this is correct
            OrclConn.Close()                             ' Just be sure to call this
        End If
    End Try
End Function

What does your OpenConnection method do?

I'm assuming it opens a database connection on a completely independant OracleCommand object. I would suggest the simplest solution here would be to pass your OracleCommand object in ByRef to the OpenConnection method, allowing you to associate a connection and open it within the method.

Obviously, this will require you to change the OpenConnection method to take both a ConnectionString parameter, as well as an OracleCommand object by reference, the signature would be:

Public Sub OpenConnection(ByVal sConnectionString As String, ByRef orclCommand As OracleCommand)

This will allow you to work with the OracleCommand object in both methods, with them both referencing the same object - therefore allowing you to call .Open() on the connection, and have the connection open in both methods.

Re-reading your code...

You appear to have an object called OrclConn, which you assign to OrclCmd.Connection.

My psychic debugging tells me this is a Static object declared outside of this Function. If so, there's your problem - when multiple users are accessing this code, the OrclConn object can have it's connection closed by another user by the time the command is executed. Typical race condition on a shared object.

The solution would be to use a connection object local to the function:

Public Function ExecuteScalar1(ByVal sExecuteString As String, ByVal sConnectString As String) As String
    Dim OrclCmd As New OracleCommand
    Dim OrclConn As New OracleConnection
    Try
        OrclConn.ConnectionString = sConnectString
        OrclConn.Open() 
        'Add any connection initialisation here

        OrclCmd.CommandText = sExecuteString
        OrclCmd.CommandType = CommandType.Text
        OrclCmd.Connection = OrclConn

        ExecuteScalar_ = Convert.ToString(OrclCmd.ExecuteScalar())

        If ExecuteScalar_ Is System.DBNull.Value Then
            ExecuteScalar_ = ""
        End If
    Catch ex As Exception
        Err.Raise(Err.Number, Err.Source, Err.Description)
    Finally
        If OrclConn.State <> ConnectionState.Closed Then ' Can't remember if this is correct
            OrclConn.Close()                             ' Just be sure to call this
        End If
    End Try
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文