保持 SQL 连接打开有什么缺点?
这似乎是一个简单的问题,但我想知道不调用“close()”函数的缺点。
This seems to be a simple question, but I wonder the disadvantages of not calling the "close()" function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
迟早,您会遇到“已达到最大连接限制”错误。我认为这是一个主要缺点。
Sooner or later, you'll run into the "The Maximum Connection Limit Has Been Reached" error. I'd call that a major disadvantage.
除了耗尽连接池(到目前为止大多数答案都是如此)之外,您还面临锁定数据的危险。
如果您正在读取或写入表,某些锁定语义将导致某些行被锁定到其他连接。如果连接上有任何未完成的事务,则尤其如此。
然后,读取和写入可能会失败,并且应用程序将到处抛出异常。
简而言之,始终关闭连接。
Apart from exhausting the connection pool (as most answers so far have been), you are in danger of locking data.
If you are reading or writing to a table, some locking semantics will cause certain rows to be locked to other connections. This is especially true if you have any open transaction on the connection.
Reads and writes can then fail and the application will throw exceptions all over the place.
In short, always close the connection.
连接池将填满,任何新连接将超时等待池中的新连接。
The connection pool will fill up and any new connections will time out waiting for a new connection from the pool.
每个与 SQL Server 的连接都需要分配内存。
因此,打开的连接越多,正在使用和保留的内存就越多,这可能会得到更好的利用。
如果您想了解 SQL Server 中的连接使用了多少内存,请查看以下参考资料。
SQL Server 对象使用的内存
Each and every connection to SQL Server requires memory allocation.
So the more connections you have open the more memory that is being used and held, that could potentially be put to better use.
If you want to know just how much memory is used by connections in SQL Server, take a look at the following reference.
Memory Used by SQL Server Objects
与数据库服务器的连接已打开。假设你有数百个程序访问同一服务器......
你也应该处理它
a connection to the database server is open. suppose you have 100s of programs hitting the same server...
you should also dispose it
快速简单的答案是,越早关闭,连接池就能越早重新使用连接。
Quick simple answer is that the sooner you close, the sooner the connection can be re-used by the connection pool.