Java 什么是数据库池?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4041114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:51:10  来源:igfitidea点击:

What is database pooling?

javadatabasedatabase-connectionconnection-pooling

提问by sagar27

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

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

采纳答案by paxdiablo

Database connectionpooling 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)
               |     /------\
               |     | Pool |
               |     \------/
           (4) |         ^
               |         | (5)
               |     +-------+   (6)   +-----------+
               #===> | Close | ======> | RealClose |
                     +-------+         +-----------+

In it's simplest form, it's just a similar API call (1) to an open-connection API call which is similar to the "real" one. 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).

在最简单的形式中,它只是与开放连接 API 调用类似的 API 调用 (1),类似于“真实”调用。这首先检查池中是否有合适的连接 (2),如果可用,则将其提供给客户端。否则会创建一个新的 (3)。

A "suitable connection" is just one that already hasaccess 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 realclose-connection, rather it puts the connection into the pool (5) for later use. At some point, connections in the pool may be actuallyclosed (6).

类似地,有一个关闭 API 调用 (4),它实际上并没有调用真正的关闭连接,而是将连接放入池 (5) 以供以后使用。在某些时候,池中的连接可能实际上已关闭 (6)。

That's a pretty simplistic explanation. Real implementations may be able to handle connections to multiple servers and multiple user accounts, they may pre-allocate some baseline of connections so some are ready immediately, and they may actually close old connections when the usage pattern quietens down.

这是一个非常简单的解释。真正的实现可能能够处理到多个服务器和多个用户帐户的连接,它们可能会预先分配一些连接基线,以便一些连接立即准备就绪,并且当使用模式安静下来时,它们实际上可能会关闭旧连接。

回答by zengr

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

图像说一千个字(paxdiablo 给出了一个很棒的描述):

alt text

替代文字

Source

来源

回答by madhurtanwani

You can use the apache commons library for connection pooling implementation transparently : http://commons.apache.org/dbcp/

您可以透明地使用 apache 公共库进行连接池实现:http: //commons.apache.org/dbcp/

DBCP is also a supported Hibernate pool : http://www.informit.com/articles/article.aspx?p=353736&seqNum=4

DBCP 也是受支持的休眠池:http: //www.informit.com/articles/article.aspx?p=353736&seqNum =4

回答by Sandhya Saini

Connection Pooling concept not only in Java but across many programming languages. Creating a new connection object is costly so a fixed number of connections are made and maintained in lifecycle creating a virtual pool Java Just ( http://javajust.com/javaques.html) see question 14 on this page

连接池概念不仅在 Java 中,而且在许多编程语言中。创建一个新的连接对象的成本很高,因此在创建虚拟池的生命周期中建立和维护固定数量的连接 Java Just ( http://javajust.com/javaques.html) 请参阅此页面上的问题 14

回答by vdegenne

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.

顾名思义。如果几个人想游泳,他们可以在同一个游泳池里游泳,每次有人加入时建造一个新游泳池真的有意义吗?时间和成本是重中之重。

回答by Peter

Database connection pooling is simply caching connections to databases so that they can be reused next time to reduce the cost of establishing a new connection each time we want to connect to a database.

数据库连接池只是缓存到数据库的连接,以便下次可以重用它们,以减少我们每次想要连接到数据库时建立新连接的成本。