如何判断一个SqlConnection有一个附加的SqlDataReader的? [英] How to tell if a SqlConnection has an attached SqlDataReader?

查看:253
本文介绍了如何判断一个SqlConnection有一个附加的SqlDataReader的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一些现在比较好奇大于实际用途。如果你有一个的SqlConnection 打开并附加 SqlDataReader的来,然后尝试使用相同的运行另一个查询的SqlConnection 那么就会抛出一个错误。我的问题是如何在的SqlConnection 知道读者是连接到它。没有一个公共财产或任何的 HasDataReader ,那么如何的SqlConnection 类知道吗?


原题:(即不再适用)

我设置了一个小东西,连接池和对我们有出现(它总是一个容易解决的比较常见的错误,但我们只是不记得 reader.Close ()!)这是当我们所使用的大量的类/方法,一个方法打开一个数据读取器和忘记关闭它的连接。这是不是真的不好的原因很多次了所有你所要做的就是进入调试器并进入上一层,看到的功能,然后才并检查它是否有一个未关闭数据读取器。

现在,这里是更大的问题。在这个连接池,如果一个DataReader是开放的,那么它不知道,直到线程得到的连接,并尝试使用它,这原本打开的数据读取器不再,甚至可能是活的东西。

所以很简单,你怎么能检测一个数据读取器是连接上打开,是有办法关闭阅读器而不关闭连接?

解决方案
  

如何关闭SqlConnection知道,   读取器被连接到它

据我所看到的,到SQLConnection知道它有附加给它的读者,因为它保持对它的引用内部。

一个明智地使用反射显示,SQLConnection对象有一个类型DBConnectionInternal,这是充满了许多这个抽象类的具体实现的一个私人领域。当您尝试到第二现场的读者加入到方法ValidateConnectionForExecute被称为内部连接的连接,而这种痕迹,通过一个内部ReferenceCollection的检查。当这揭示了一个现有的活的读者,则抛出异常。

我想,如果你愿意,你可以挖掘出这一切自己在运行时与反思。

This is something now more of curiosity than actual purpose. If you have a SqlConnection opened and attach a SqlDataReader to it, and then try to run another query using the same SqlConnection then it will throw an error. My question is how does the SqlConnection know that a reader is attached to it. There is not a public property or anything for HasDataReader, so how does the SqlConnection class know?


Original Question: (which is no longer relevant)

Hi, I'm setting up a little thing for connection pooling and on of the more common bugs that we have occur(its always an easy fix, but we just can't remember reader.Close()!) it is when we have a connection that is used by a lot of classes/methods and one method opens a data reader and forgets to close it. This isn't really bad cause a lot of times all you have to do is go into the debugger and go up one level and see the function before it was and check if it had an unclosed data reader.

Now, here is the bigger problem. In this connection pool, if a datareader is open, then it's not known until a thread gets a connection and tries to use it and the thing that originally opened the data reader may no longer even be alive.

So quite simply, how can you detect if a data reader is open on a connection and is there a way to close the reader without closing the connection?

解决方案

how does the SqlConnection know that a reader is attached to it

As far as I can see, the SQLConnection knows that it has a reader attached to it because it maintains a reference to it internally.

A judicious use of Reflector shows that the SQLConnection object has a private field of type DBConnectionInternal, which is filled with one of a number of concrete implementations of this abstract class. When you try to add a second live reader to the connection the method 'ValidateConnectionForExecute' is called on the internal connection, and this traces through to an examination of an internal 'ReferenceCollection'. When this reveals an existing live reader, an exception is thrown.

I guess, if you wanted, you could dig all this out yourself at runtime with reflection.

这篇关于如何判断一个SqlConnection有一个附加的SqlDataReader的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆