大量打开的数据库连接导致应用程序崩溃
在我的网络服务中,我连接到 Sybase 数据库。
我有医生名单,每个医生都可以与多家医院相关联。我需要更新医生工作的每家医院的一些详细信息。
对于每个医生,都会打开与数据库的新连接。对于列表中的每个医生,我首先获取 DbReader 中的医院列表,然后对阅读器中的每个医院进行操作。处理完 DbReader 中的所有记录后,我将关闭读取器以及连接。 (这是因为我将 System.Data.CommandBehavior.CloseConnection
传递给 Sybase.Data.AseClient.AseCommand
类的 ExecuteReader()
方法)
现在,如果医生的数量超出某个限制,我会收到错误,因为打开的连接数量变得非常大。
在这种情况下我可以使用哪种不同的方法来消除错误?如何知道客户端打开的连接数?
我能做的一件事是 - 在前一个连接关闭之前不要打开新连接。但这种方法并不是那么好(我认为),因为虽然允许多个连接,但我一次只能有一个连接。
In my web service, i connect to Sybase database.
I have list of Doctors and each doctor can be associated with number of Hospitals. I need to update a few details on every Hospital with which a Doctor is working.
For every doctor a new connection to database is opened.For every Doctor in a list, I first get the list of Hospitals in the DbReader and i do operations on each Hospital in the reader. Once all the records in the DbReader are processed, I close the reader along with the connection. (This is because i pass System.Data.CommandBehavior.CloseConnection
to the ExecuteReader()
method of the Sybase.Data.AseClient.AseCommand
class)
Now, if the number of doctors are beyond some limit, I get errors since the number of connections which are open become very large.
Which different approach can i use in this scenario to get rid of the error? How to know the number of open connections from client side?
One of the thing i can do is - don't open a new connection until previous connection is closed. But this approach is not that good (I think) because, though multiple connections are allowed, i'll be having one connection at a time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用连接池?或者实现一个单例连接管理器,只要未达到限制,它就会提供新的连接。
此外,您确定在这种情况下需要多重连接吗?听起来您必须为每位医生运行相同的流程。因此,您可以循环执行此操作,猜测一个连接就足够了。
Why not using connection pooling? or implement a singleton connectionmanager which provides new connections as long as the limit is not reached.
Besides are you sure you need Multiconnection in this case? it sounds like you have to run the same process for each Doctor. So you can do it in a loop, guess one connection will be enough for that.