读者关闭并处置
请看代码。我们有一个像这样的方法,它返回 reader。即
public IRecordsReader GetValues()
{
IRecordsReader TestReader.....
return TestReader;
}
,我们在另一个像这样的方法中调用该方法
IRecordsReader ResultReader = GetValues();
ResultReader.Close();
ResultReader.Dispose();
,即我们正在关闭并处置该读取器。
我的疑问是我们没有在 GetValues()
方法中正确关闭 TestReader。那么这是否会导致连接池问题呢?请提出您的建议。
Please look at the code. we have a method like this which returns reader. ie
public IRecordsReader GetValues()
{
IRecordsReader TestReader.....
return TestReader;
}
and we are calling that method inside another method like this
IRecordsReader ResultReader = GetValues();
ResultReader.Close();
ResultReader.Dispose();
ie we are closing and disposing that reader.
My doubt is that we are not properly closing the TestReader in the GetValues()
method. So whether it will cause any connection pool issues? Please give your suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该使用 using 包装 SqlDataReader,以便它自动关闭并处置
IDisposable
对象。就像下面的
更多信息
一样,因为您执行了此操作
,并且此
IRecordsReader ResultReader = GetValues()
这就是您实际上在执行的
IRecordsReader ResultReader = TestReader
所以您间接关闭/处置 < code>TestReader 实例。你明白了吗?把它放在代码中,尝试一下你就会知道:)
回答
OP询问-
那么它是否会导致任何连接池问题?请给出您的建议。
除非您的连接限制没有超出,否则不会有问题。但由于您正在关闭并处置资源,因此不会有任何问题:)
You should probably wrap your SqlDataReader with using so that it would automatically close and dispose the
IDisposable
object.Like below
More Info
since you do this
and this
IRecordsReader ResultReader = GetValues()
this is what you are actually doing
IRecordsReader ResultReader = TestReader
so you are indirectly closing/disposing the
TestReader
instance. You get it?. Put it in code and try you will know :)Answer
OP Asked -
So whether it will cause any connection pool issues? Please give your suggestions.
Unless your connection limit is not exceeded you won't have a issue. But since you are closing and disposing the resource you will not have any issues :)