关于Web应用程序中SQL连接的问题
我对我们打开的用于在 asp.net 中的 Web 应用程序(网上商店)上下文中对数据库执行 CRUD 语句的 sql 数据库连接有疑问
建议
- 保持数据库连接打开什么(当用户登录到他的个人资料时) )并在应用程序关闭或会话过期时关闭
,或者
- 在需要执行操作时打开数据库连接,并在执行命令/存储过程后立即将其关闭。
我读过一些内容,其中每次打开和关闭数据库连接都会消耗大量资源。
我需要你的建议
I have a doubt regarding the sql database connection that we open for performing CRUD statements on the database in the context of web application (a web store) in asp.net
What is advisable
- to keep the database connection open (when user logs into his profile) and close when the application is closed or session is expired
or
- To open a database connection when required perform operations and as soon as the command/stored procedure is executed close it.
I have read some where that a lot of resources are consumed to open and close the database connection every time.
I need your advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,MS SQL Server 使用 连接池,因此连接(使用完全相同的连接字符串)被分配给服务器保存在池中的实时连接。
你应该打开和关闭你的连接,以保持你身边的一切整洁。
By default, MS SQL server uses connection pooling, so connections (using the exact same connection string) get assigned to a live connection kept in the pool by the server.
You should open and close your connections to keep things tidy on your side.
我总是建议在使用后关闭连接。您的站点可以通过这种方式处理更多流量,因为连接不会闲置。打开连接的开销很小。
I would always recommend closing a connection after use. Your site can handle more traffic this way, since connections are not sitting idle. The overhead in opening a connection is minuscule.
仅在需要时打开,并在需要后立即关闭。
在经常使用的情况下,连接将被池化,并且实际上保持打开状态,但由 SQL Server 决定。这将为您带来代码透明的性能。
Open only when you need it, and close immediately after.
In case of often usage, connection will be pooled, and in fact stay be opened, but up to SQL Server decision. That will gain you performance being transparent for the code.