SqlDataReader:在这种情况下,读取器会被关闭吗?
我正在清理我继承的旧 .NET 1.1 项目中的 DataReader。
以前的开发人员以大多数 DAL 方法返回 SqlDataReaders 的方式对数据访问层进行编码(因此由调用者正确调用 .Close() 或 .Dispose() 方法)。
不过,我遇到过一种情况,调用者没有捕获返回的 SqlDataReader (因此没有正确处理它)。 请参阅下面的代码:
数据访问方法:
Public Shared Function UpdateData() As SqlDataReader
...
drSQL = cmdSQL.ExecuteReader(CommandBehavior.CloseConnection)
Return drSQL
End Function
调用代码:
...
DataAccessLayer.UpdateData()
...
正如您所看到的,调用方法不会接收/捕获返回的 SqlDataReader。 那么会发生什么呢? SqlDataReader 仍然存在并打开吗? 或者它会自动收集垃圾,因为没有任何东西可以解决它?
我想不出一种方法来调试和测试它。 如果有人有任何想法或建议,那就太好了。
I am cleaning up the DataReaders in an old .NET 1.1 project that I inherited.
The previous developer coded the data-access-layer in such a way that most of the DAL methods returned SqlDataReaders (thus leaving it up to the caller to properly call the .Close() or .Dispose() methods).
I have come across a situation, though, where a caller is not catching the returned SqlDataReader (and therefore is not disposing of it properly). See the code below:
Data Access Method:
Public Shared Function UpdateData() As SqlDataReader
...
drSQL = cmdSQL.ExecuteReader(CommandBehavior.CloseConnection)
Return drSQL
End Function
Calling code:
...
DataAccessLayer.UpdateData()
...
As you can see, the calling method does not receive/catch the returned SqlDataReader. So what happens? Is that SqlDataReader still out there and open? Or does it automatically get garbage collected since nothing is addressing it?
I couldn't think of a way to debug and test this. If anybody has any ideas or suggestions that would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信它会被关闭,但直到垃圾收集器开始处理它,这可能不会持续很长时间......
i believe that it will get closed but not until the garbage-collector gets 'round to it, which may not be for a very long time...