简体   繁体   English

大量打开的数据库连接使应用程序崩溃

[英]Large number of open connections to database crashes the application

In my web service, i connect to Sybase database. 在我的Web服务中,我连接到Sybase数据库。

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. 对于每个医生,都将打开与数据库的新连接。对于列表中的每个医生,我首先在DbReader中获得医院列表,然后在读取器中对每个医院进行操作。 Once all the records in the DbReader are processed, I close the reader along with the connection. 处理完DbReader中的所有记录后,我将关闭读取器以及连接。 (This is because i pass System.Data.CommandBehavior.CloseConnection to the ExecuteReader() method of the Sybase.Data.AseClient.AseCommand class) (这是因为我将System.Data.CommandBehavior.CloseConnection传递给Sybase.Data.AseClient.AseCommand类的ExecuteReader()方法)

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. 但是这种方法不是很好(我认为),因为尽管允许多个连接,但我一次只能连接一个。

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. 听起来您必须为每个Doctor运行相同的过程。 So you can do it in a loop, guess one connection will be enough for that. 因此,您可以循环进行,猜测一个连接就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM