什么是数据库池?[英] What is database pooling?

本文是小编为大家收集整理的关于什么是数据库池?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我只是想知道数据库连接池的概念及其如何实现.

推荐答案

数据库 Connection boming是一种用于保持数据库连接打开的方法,以便其他人可以重复使用.

通常,打开数据库连接是一个昂贵的操作,尤其是在数据库远程的情况下.您必须打开网络会话,身份验证,检查授权等等.合并使连接保持活动状态,以便在稍后请求连接时,其中一个活动被用来优先创建另一个连接.

请参阅下几段以下图:

  +---------+
  |         |
  | Clients |
+---------+ |
|         |-+  (1) +------+ (3) +----------+
| Clients | ==#==> | Open | ==> | RealOpen |
|         |   |    +------+     +----------+
+---------+   |       ^
              |       |(2)
              |    /------\     +---------+ (6) +-----------+
              |    | Pool | --> | Cleaner | ==> | RealClose |
              |    \------/     +---------+     +-----------+
          (4) |       ^
              |       |(5)
              |    +-------+
              #==> | Close |
                   +-------+

在最简单的形式中,它只是Open api调用的API调用(1),类似于"真实"一个,RealOpen.首先检查池是否合适的连接(2),如果有可用的话,则将其提供给客户.否则将创建一个新的(3)并给予客户端.

"合适的连接"只是已经使用正确信息(例如数据库实例,凭据以及可能的其他事项)对数据库访问的连接.

类似地,有一个Close API调用(4)实际上并未调用 real RealClose,而是将连接放回池中(5)供以后使用.在某个时候,池中的连接可能是实际上封闭的(6).这可以通过连续监视池并调用RealClose的线程来完成,如果它们已经足够老或满足其他条件.

这是一个非常简单的解释.实际实现可能更复杂,例如上述:

  • 处理与多个服务器的连接并使用多个用户帐户;
  • 使用任意规则来决定何时应该真正关闭连接,诸如其年龄,有多少个类似连接等等.

数据库连接池是更一般情况下的特定情况,即维护可能启动昂贵的缓存物品.如您所见,从也适用于线程池(或备用磁带池,通信设备,激光切割器或其他数十个不同的东西).

在所有情况下,最好是"昂贵"在线上另一个资源而不是使用以前在线的资源.

链接答案还显示了一种可能启动或(完全)关闭资源的可能算法.

其他推荐答案

图像会说一千个单词(Paxdiablo给出了很棒的描述):

 alt text

source

其他推荐答案

顾名思义.如果一些人想游泳,他们可以在同一游泳池里游泳,每次有人添加时,建造一个新的游泳池真的很有意义吗?时间和成本是优先事项.

本文地址:https://www.itbaoku.cn/post/597413.html

问题描述

I just wanted to know the concept of database connection pooling and how it is achieved.

推荐答案

Database connection pooling is a method used to keep database connections open so they can be reused by others.

Typically, opening a database connection is an expensive operation, especially if the database is remote. You have to open up network sessions, authenticate, have authorisation checked, and so on. Pooling keeps the connections active so that, when a connection is later requested, one of the active ones is used in preference to having to create another one.

Refer to the following diagram for the next few paragraphs:

  +---------+
  |         |
  | Clients |
+---------+ |
|         |-+  (1) +------+ (3) +----------+
| Clients | ==#==> | Open | ==> | RealOpen |
|         |   |    +------+     +----------+
+---------+   |       ^
              |       |(2)
              |    /------\     +---------+ (6) +-----------+
              |    | Pool | --> | Cleaner | ==> | RealClose |
              |    \------/     +---------+     +-----------+
          (4) |       ^
              |       |(5)
              |    +-------+
              #==> | Close |
                   +-------+

In it's simplest form, it's just an API call (1) to an Open API call which is similar to the "real" one, RealOpen. This first checks the pool for a suitable connection (2) and, if one is available, that's given to the client. Otherwise a new one is created (3) and given to the client.

A "suitable connection" is just one that already has access to the database using the correct information (such as database instance, credentials, and possibly other things).

Similarly, there's a Close API call (4) which doesn't actually call the real RealClose, rather it puts the connection back into the pool (5) for later use. At some point, connections in the pool may be actually closed (6). This could be done by a thread that continuously monitors the pool and calls RealClose if they are old enough or certain other conditions are met.

That's a pretty simplistic explanation. Real implementations may be arbitrarily more complex such as the previously mentioned:

  • handling connections to multiple servers and using multiple user accounts;
  • using arbitrary rules to decide when connections should be really shut down, things like its age, how many similar connections there are, and so on.

Database connection pooling is a specific case for a more general one, that of maintaining cached things where they may be expensive to start. As you see from a similar answer of mine, it can apply to thread pools as well (or pools of backup tapes, communication devices, laser cutters, or dozens of other different things).

In all cases, it's best if it's "expensive" to bring another resource online rather than use one that had previously been online.

That linked answer also shows one possible algorithm for deciding when to start up or (fully) shut down a resource.

其他推荐答案

Images speak a thousand words (paxdiablo gave an awesome description):

alt text

Source

其他推荐答案

As the name suggests. If a few people wants to swim, they can swim in the same swimming-pool, does it really make sense to construct a new swimming-pool each time someone adds in ? Time and cost is a priority.