简体   繁体   English

Web 应用程序中的数据库连接

[英]Database Connection in Web Application

The following are the different techniques we can use for database connection in a web application envrioment.以下是我们可以在 Web 应用程序环境中用于数据库连接的不同技术。

  1. Application Context Database Connection: only one database connection shared among all requests. Application Context Database Connection:所有请求只共享一个数据库连接。
  2. Database Pooling: open a fixed number of database connection and share the connection among all requests. Database Pooling:打开固定数量的数据库连接,并在所有请求之间共享连接。
  3. New Database Connection per request: Open a connection for each request. New Database Connection per request:为每个请求打开一个连接。

What are the advantage and disadvantage of these techniques?这些技术的优点和缺点是什么? Which one should a developer use?开发人员应该使用哪一个?

Option 1. Sharing a single database connection among all web users is bound to fail for one reason or another. 选项1.在所有Web用户之间共享一个数据库连接必然因某种原因而失败。 One long running query and your entire server will grind to a halt. 一个长时间运行的查询和整个服务器将停止运行。 That's an hard and fast "no" for 99.9% of all modern applications, even non-web based applications. 对于所有现代应用程序(甚至非基于Web的应用程序)中的99.9%而言,这是一成不变的“拒绝”。

Option 2. Connection pooling. 选项2.连接池。 Probably the 2nd most commonly used technique for a web app to connect to a DB. 可能是Web应用程序连接到数据库的第二种最常用的技术。 First, the benefits are extremely limited if the DB and the Pool/web app are on the same machine. 首先,如果数据库和池/ Web应用程序在同一台计算机上,则收益将非常有限。 The pool and the web app, however can easily exist on the same hardware. 池和Web应用程序可以很容易地存在于同一硬件上。 The benefit here is that a database connections are expensive to open AND to a lesser extent, expensive to keep open. 这样做的好处是,数据库连接的打开开销很大,而保持打开的开销较小。 Opening a connection requires CPU usage and memory allocation. 打开连接需要CPU使用率和内存分配。 Pooling connections can keep a few dozen connections available to be "attached to" nearly instantly. 池连接可以使几十个连接几乎可以立即“连接”。 The memory will already be allocated and most of the setup work done already, so connecting and disconnecting from the pool is relatively cheep. 内存将已经分配,​​并且大多数设置工作已经完成,因此与池连接和断开连接比较便宜。 Pools usually keep a certain number of connections open at all times and grow as traffic increases. 池通常始终保持一定数量的连接处于打开状态,并随着流量的增加而增长。 In times of excessive traffic, pools typically queue connection requests so that the DB is not overloaded.. users at the back of the queue experience delays, but the system churns away and generally is less likely to grind to a halt due to lack of memory and disk swapping. 在流量过大的情况下,池通常会将连接请求放入队列中,以使DB不会过载。.位于队列后面的用户会遇到延迟,但是系统崩溃了,并且由于内存不足,通常不太可能停止运行和磁盘交换。

Option 3. New Database Connection per request. 选项3.每个请求的新数据库连接。 On a light to moderately utilized system, it's not a terrible option, and it's usually easy to upgrade to a pooler at a later date. 在轻到中等使用的系统上,这不是一个糟糕的选择,通常以后可以很容易地升级到Pooler。 Keep in mind, however, that each database connection (every page load) will require opening and closing a DB connection, which involved CPU and Memory Allocation. 但是请记住,每个数据库连接(每个页面加载)都需要打开和关闭一个数据库连接,这涉及CPU和内存分配。 In practice, if your pages load quickly, your userbase is small and consistent, and your traffic is fairly consistent, it can work just fine. 实际上,如果页面快速加载,用户群较小且一致,并且流量相当一致,则可以正常工作。 Many DEV, CAT and QA environments are connected directly without a pooler. 许多DEV,CAT和QA环境直接连接而没有池。 The disadvantage is #1, there is absolutely no way to control connections to the DB. 缺点是#1,绝对没有办法控制与数据库的连接。 If queries hang, you can have hundreds of connections suddenly killing your DB and sometimes a reboot or restart of the DB is required to rectify the situation. 如果查询挂起,则可能有数百个连接突然杀死数据库,有时需要重新启动或重新启动数据库才能纠正这种情况。

For example: You write 1 bad query that's on the homepage of your site, that causes it to take 3 seconds to run instead of .3 seconds. 例如:您在网站的首页上写了1条错误的查询,这导致它需要3​​秒钟而不是0.3秒钟才能运行。 Eventually, your site that had 1-2 pages running at any one moment, now may bump up to 10-20. 最终,您的网站在任何时刻运行1-2页,现在可能会增加到10-20。 Now those 10-20 pages = 10-20 DB connections opening and closing constantly, with 10-20 being open on average. 现在,那些10-20页= 10-20 DB连接不断打开和关闭,平均10-20页处于打开状态。 This problem will creep up, using more and more memory until you reach the connection limit (or worse, use up all memory and everythings now swapping). 此问题将逐渐蔓延,使用越来越多的内存,直到达到连接限制为止(或更糟的是,耗尽所有内存,现在所有内容都在交换)。 At this point, everything's ground to a halt. 此时,一切都停止了。

Keep in mind that connections take up resources both on the DB and the app server/pooler. 请记住,连接占用了数据库和应用程序服务器/池上的资源。 When your DB is paging to disk, most of the time, all hope is lost for a graceful recovery without resetting something-- Obviously it can happen, but without a code fix, you usually reboot to give yourself some more time until the problem inevitibly happens again, and hopefully by that time, you've found the bad query, or bad config and fixed it. 在大多数情况下,当数据库分页到磁盘时,如果不进行任何重置就无法进行正常恢复,所有希望都将落空-显然这是可能发生的,但是如果没有代码修复,您通常会重新启动以给自己更多的时间,直到不可避免地出现问题为止再次发生,希望到那时,您已经找到了错误的查询或错误的配置并进行了修复。

Option 2 gives you the most options. 选项2为您提供最多的选择。 It's usually not a management headache, but if you're running it all on 1 machine, the benefits are limited. 通常这不是管理上的麻烦,但是如果您全部在一台机器上运行,则收益是有限的。 If you have at least 2 machines (app server and DB server), it's an easy solution that usually prevents many system overloads. 如果您至少有两台计算机(应用服务器和数据库服务器),这是一个简单的解决方案,通常可以防止许多系统过载。

What about a corporate web application, where users stay connected all day, could I maintain one connection per user?对于用户整天保持连接的企业 Web 应用程序,我是否可以为每个用户维护一个连接? That would be option four:那将是选项四:

  1. New DB connection per connected user: Open a New connection with the given user after a successfull login and stick it to the web session.每个已连接用户的新数据库连接:成功登录后与给定用户打开新连接并将其粘贴到网络会话。 Must be closed when the session gets invalidated (logout or timeout).当会话失效(注销或超时)时必须关闭。

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

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