维护数据库连接或在需要时连接?
现在,我的项目网站在加载网站时开始与数据库连接。我的网站上有一些页面不需要连接到数据库。
在整个会话期间保持与数据库的连接是否明智,或者在发生实际需要连接的事件时不断重新建立连接? (比如注册账号)
Right now my project website starts a connection with the database as the website is loaded. I have pages on the website where a connection to a database isn't required.
Is it resourceful to maintain a connection to the database for the entire duration of a session, or keep remaking connections when an event occurs that actually requires a connection? (Such as registering an account)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我认为最好仅在需要使用数据库时才打开与数据库的连接。即使您必须在单个会话期间多次打开数据库,您的应用程序也不会无缘无故地随意打开连接。围绕以下理念构建应用程序:每个打开的数据库连接都是有目的的,可确保您明智地使用连接,而不是无用地使用连接。此外,打开一个新的数据库连接并不是非常消耗资源,而且为了简单的功能打开一些额外的连接也没有什么害处。如果有的话,您应该结合数据库功能
Personally, I think it is best to open up a connection to the database only when you need to utilize the database. Even if you have to open the database multiple times during a single session, your application will not be opening connections at will for no particular reason. Building your application around the idea that every database connection that opens is opened with a purpose ensures that you use your connectivity wisely rather than uselessly. Besides, opening a new database connection is not very resource-intensive, plus it's not harmful to open a few extra connections for simple functionality. If anything, you should combine database functionality