22

Can someone explain what is Connection and Statement Pooling and what is the benefit over unpooled DataSources? I am trying to understand when it is a good idea to use a technology like c3p0 or proxool in a project. I need first to understand what they do and when it interesting to use them. Thank you very much.

4

5 回答 5

59

快乐的连接

每次创建新连接都非常容易。一行:仅此而已。没什么好想的。伟大的生活。

坚持,稍等。你吃盘子吗?

在此处输入图像描述

每次使用后你会扔掉你的盘子吗?

不,你把它洗干净放在盘子架上,这样你下一顿饭就可以再用了。每次都买新盘子是不可能的。如果你这样做了,你将浪费足够的钱在一年内购买一台新 iPad。

再次考虑连接池。

但是这一次,连接是你的盘子,连接池是你的盘子架。你的钱包和你的能量代表了系统资源(内存和带宽)。

洗还是花?

你更愿意做什么:

一个。洗碗

湾。还是每顿饭都跑到商场买新盘子?

虽然连接池涉及一些任务,但从长远来看,与每次创建新连接相比,它的工作量更少。关键是要知道您的家人(应用程序)在任何一天需要多少板(连接)。

池可用于数据库连接、线程、实体 bean 和其他工厂派生对象。

于 2013-04-19T15:34:49.680 回答
14

Creating a network connection to a database server is (relatively) expensive. Likewise asking the server to prepare a SQL statement is (relatively) expensive.

Using a connection/statement pool, you can reuse existing connections/prepared statements, avoiding the cost of initiating a connection, parsing SQL etc.

于 2009-01-19T11:23:28.803 回答
10

I am not familiar with c3p0, but the benefits of pooling connections and statements include:

  1. Performance. Connecting to the database is expensive and slow. Pooled connections can be left physically connected to the database, and shared amongst the various components that need database access. That way the connection cost is paid for once and amortized across all the consuming components.

  2. Diagnostics. If you have one sub-system responsible for connecting to the database, it becomes easier to diagnose and analyze database connection usage.

  3. Maintainability. Again, if you have one sub-system responsible for handing out database connections, your code will be easier to maintain than if each component connected to the database itself.

于 2009-01-19T11:24:01.453 回答
3

Connecting and disconnecting from a database is an expensive operation. By using pooling you can write your code to open and close connections but the pool decides when to actually do it, leaving a certain number of connections open for a certain time.

Statement pooling? Are you talking about statement caching?

于 2009-01-19T11:22:30.757 回答
1

引用JAVA Persistance with Hibernate一书

使用游泳池的三个原因:

  • 获取新连接的成本很高。一些数据库管理系统甚至为每个连接启动一个全新的服务器进程。

  • 维护许多空闲连接对于数据库管理系统来说是昂贵的,并且池可以优化空闲连接的使用(或者如果没有请求则断开连接)。

  • 对于某些驱动程序来说,创建准备好的语句也很昂贵,并且连接池可以缓存跨请求连接的语句。

于 2009-01-24T11:55:46.490 回答