读者关闭并处置

发布于 2024-12-08 16:43:13 字数 412 浏览 5 评论 0原文

请看代码。我们有一个像这样的方法,它返回 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 技术交流群。

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

发布评论

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

评论(1

甜柠檬 2024-12-15 16:43:13

您可能应该使用 using 包装 SqlDataReader,以便它自动关闭并处置 IDisposable 对象。

就像下面的

using(IRecordsReader ResultReader = GetValues())
{
//do your stuff
}
//resultReader is closed and disposed from now

更多信息

一样,因为您执行了此操作

public IRecordsReader GetValues()
{
   IRecordsReader TestReader.....

   return TestReader;
}

,并且此

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

using(IRecordsReader ResultReader = GetValues())
{
//do your stuff
}
//resultReader is closed and disposed from now

More Info

since you do this

public IRecordsReader GetValues()
{
   IRecordsReader TestReader.....

   return TestReader;
}

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文