“System.ArgumentException:无效操作。连接已关闭”。我不断收到此错误
我在服务器上收到此错误。我无法在我的开发机器上复制这个。
当我调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
OpenConnection
方法有什么作用?我假设它在完全独立的 OracleCommand 对象上打开数据库连接。我建议这里最简单的解决方案是将 ByRef 中的 OracleCommand 对象传递给 OpenConnection 方法,允许您关联连接并在该方法中打开它。
显然,这将要求您更改 OpenConnection 方法以采用 ConnectionString 参数以及引用的 OracleCommand 对象,签名为:
这将允许您在两个方法中使用 OracleCommand 对象,并且它们都引用相同的对象 - 因此允许您在连接上调用 .Open() ,并在两种方法中打开连接。
重新阅读您的代码...
您似乎有一个名为
OrclConn
的对象,您将其分配给OrclCmd.Connection
。我的心理调试告诉我这是一个在此函数之外声明的静态对象。如果是这样,那就有问题了 - 当多个用户访问此代码时,
OrclConn
对象可以在执行命令时让另一个用户关闭其连接。共享对象上的典型竞争条件。解决方案是使用函数本地的连接对象:
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:
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 toOrclCmd.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: