Java JDBC 连接池选项:DBCP 与 C3P0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/520585/
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-11 15:44:24  来源:igfitidea点击:

Connection pooling options with JDBC: DBCP vs C3P0

javajdbcconnection-poolingc3p0apache-commons-dbcp

提问by Dema

What is the best connection pooling library available for Java/JDBC?

可用于 Java/JDBC 的最佳连接池库是什么?

I'm considering the 2 main candidates (free / open-source):

我正在考虑 2 个主要候选人(免费/开源):

I've read a lot about them in blogs and other forums but could not reach a decision.

我在博客和其他论坛上阅读了很多关于它们的内容,但无法做出决定。

Are there any relevant alternatives to these two?

这两个有相关的替代方案吗?

采纳答案by j pimmel

DBCP is out of date and not production grade. Some time back we conducted an in-house analysis of the two, creating a test fixture which generated load and concurrency against the two to assess their suitability under real life conditions.

DBCP 已过时且不是生产级。前段时间,我们对两者进行了内部分析,创建了一个测试装置,它针对两者生成负载和并发性,以评估它们在现实生活条件下的适用性。

DBCP consistently generated exceptions into our test application and struggled to reach levels of performance which C3P0 was more than capable of handling without any exceptions.

DBCP 始终在我们的测试应用程序中生成异常,并努力达到 C3P0 能够在没有任何异常的情况下处理的性能水平。

C3P0 also robustly handled DB disconnects and transparent reconnects on resume whereas DBCP never recovered connections if the link was taken out from beneath it. Worse still DBCP was returning Connection objects to the application for which the underlying transport had broken.

C3P0 还稳健地处理了数据库断开连接和恢复时的透明重新连接,而如果从其下方取出链接,则 DBCP 永远不会恢复连接。更糟糕的是,DBCP 正在将 Connection 对象返回给底层传输已中断的应用程序。

Since then we have used C3P0 in 4 major heavy-load consumer web apps and have never looked back.

从那以后,我们在 4 个主要的重载消费类 Web 应用程序中使用了 C3P0,并且从未回头。

UPDATE:It turns out that after many years of sitting on a shelf, the Apache Commons folk have taken DBCP out of dormancyand it is now, once again, an actively developed project. Thus my original post may be out of date.

更新:事实证明,在搁置多年之后,Apache Commons 人们已经让DBCP 摆脱了休眠状态,现在它再次成为一个积极开发的项目。因此我原来的帖子可能已经过时了。

That being said, I haven't yet experienced this new upgraded library's performance, nor heard of it being de-facto in any recent app framework, yet.

话虽如此,我还没有体验过这个新升级的库的性能,也没有听说它在任何最近的应用程序框架中成为事实。

回答by toolkit

Another alternative, Proxool, is mentioned in this article.

这篇文章中提到了另一个替代方案 Proxool 。

You might be able to find out why Hibernate bundles c3p0 for its default connection pool implementation?

您可能会发现为什么 Hibernate 将 c3p0 捆绑为其默认连接池实现?

回答by Brandon Teo

For the auto-reconnect issue with DBCP, has any tried using the following 2 configuration parameters?

对于 DBCP 的自动重新连接问题,是否尝试过使用以下 2 个配置参数?

validationQuery="Some Query"

testOnBorrow=true

回答by Brandon Teo

Unfortunately they are all out of date. DBCP has been updated a bit recently, the other two are 2-3 years old, with many outstanding bugs.

不幸的是,它们都已经过时了。DBCP最近更新了一点,另外两个都是2-3年,有很多突出的bug。

回答by wwadge

I invite you to try out BoneCP-- it's free, open source, and faster than the available alternatives (see benchmark section).

我邀请您试用BoneCP—— 它是免费的、开源的,并且比可用的替代品更快(参见基准测试部分)。

Disclaimer: I'm the author so you could say I'm biased :-)

免责声明:我是作者,所以你可以说我有偏见:-)

UPDATE: As of March 2010, still around 35% faster than the new rewritten Apache DBCP ("tomcat jdbc") pool. See dynamic benchmark link in benchmark section.

更新:截至 2010 年 3 月,仍然比新重写的 Apache DBCP(“tomcat jdbc”)池快 35% 左右。请参阅基准测试部分中的动态基准测试链接。

Update #2: (Dec '13) After 4 years at the top, there's now a much faster competitor : https://github.com/brettwooldridge/HikariCP

更新 #2:(2013 年 12 月)在领先 4 年后,现在有一个更快的竞争对手:https: //github.com/brettwooldridge/HikariCP

Update #3: (Sep '14) Please consider BoneCP to be deprecatedat this point, recommend switching to HikariCP.

更新 #3:(2014 年9 月)请考虑此时 BoneCP 已被弃用,建议切换到HikariCP

Update #4: (April '15) -- I no longer own the domain jolbox.com

更新 #4:(2015 年 4 月)——我不再拥有 jolbox.com 域

回答by wwadge

Here are some articles that show that DBCP has significantly higher performance than C3P0 or Proxool. Also in my own experience c3p0 does have some nice features, like prepared statement pooling and is more configurable than DBCP, but DBCP is plainly faster in any environment I have used it in.

这里有一些文章表明 DBCP 的性能明显高于 C3P0 或 Proxool。同样根据我自己的经验,c3p0 确实有一些不错的功能,比如准备好的语句池,并且比 DBCP 更可配置,但 DBCP 在我使用过的任何环境中都明显更快。

Difference between dbcp and c3p0? Absolutely nothing! (A Sakai developers blog) http://blogs.nyu.edu/blogs/nrm216/sakaidelic/2007/12/difference_between_dbcp_and_c3.html

dbcp 和 c3p0 之间的区别?绝对没有!(酒井开发者博客) http://blogs.nyu.edu/blogs/nrm216/sakaidelic/2007/12/difference_between_dbcp_and_c3.html

See also the like to the JavaTech article "Connection Pool Showdown" in the comments on the blog post.

另请参阅博客文章评论中对 JavaTech 文章“Connection Pool Showdown”的类似内容。

回答by Larry H

Just got done wasting a day and a half with DBCP. Even though I'm using the latest DBCP release, I ran into exactly the same problems as j pimmeldid. I would not recommend DBCP at all, especially it's knack of throwing connections out of the pool when the DB goes away, its inability to reconnect when the DB comes back and its inability to dynamically add connection objects back into the pool (it hangs forever on a post JDBCconnect I/O socket read)

刚刚用 DBCP 浪费了一天半的时间。尽管我使用的是最新的 DBCP 版本,但我遇到了与j pimmel完全相同的问题。我根本不推荐 DBCP,尤其是当 DB 消失时它会从池中抛出连接的诀窍,它无法在 DB 回来时重新连接,并且它无法动态地将连接对象添加回池中(它永远挂在后 JDBCconnect I/O 套接字读取)

I'm switching over to C3P0 now. I've used that in previous projects and it worked and performed like a charm.

我现在正在切换到 C3P0。我在以前的项目中使用过它,它的工作和表现就像一个魅力。

回答by Soundlink

A good alternative which is easy to use is DBPool.

一个易于使用的好选择是DBPool

"A Java-based database connection pooling utility, supporting time-based expiry, statement caching, connection validation, and easy configuration using a pool manager."

“基于 Java 的数据库连接池实用程序,支持基于时间的到期、语句缓存、连接验证和使用池管理器的简单配置。”

http://www.snaq.net/java/DBPool/

http://www.snaq.net/java/DBPool/

回答by user542651

I was having trouble with DBCP when the connections times out so I trialled c3p0. I was going to release this to production but then started performance testing. I found that c3p0 performed terribly. I couldn't configure it to perform well at all. I found it twice as slow as DBCP.

当连接超时时,我遇到了 DBCP 的问题,所以我尝试了 c3p0。我打算将其发布到生产环境中,但随后开始进行性能测试。我发现 c3p0 的表现非常糟糕。我根本无法将它配置为表现良好。我发现它比 DBCP 慢两倍。

I then tried the Tomcat connection pooling.

然后我尝试了Tomcat 连接池

This was twice as fast as c3p0 and fixed other issues I was having with DBCP. I spent a lot of time investigating and testing the 3 pools. My advice if you are deploying to Tomcat is to use the new Tomcat JDBC pool.

这比 c3p0 快两倍,并修复了我在 DBCP 中遇到的其他问题。我花了很多时间调查和测试这 3 个池。如果您要部署到 Tomcat,我的建议是使用新的 Tomcat JDBC 池。

回答by UBIK LOAD PACK

Dbcp is production ready if configured properly.

如果配置正确,Dbcp 即可用于生产。

It is for example used on a commerce Website of 350000 visitors/ day and with pools of 200 connections.

例如,它用于每天有 350000 名访问者和 200 个连接池的商业网站。

It handles very well timeouts provided you configure it correctly.

如果您正确配置它,它可以很好地处理超时。

Version 2 is on progress and it has a background which makes it reliable since Many Production problems have been tackled.

第 2 版正在开发中,它的背景使它变得可靠,因为许多生产问题已经得到解决。

We use it for our batch server solution and it has been running hundreds of batches That work on millions of lines in database.

我们将它用于我们的批处理服务器解决方案,它已经运行了数百个批处理,在数据库中的数百万行上工作。

Performance tests run by tomcat jdbc pool show it has better performance than cp30.

由 tomcat jdbc pool 运行的性能测试表明它比 cp30 具有更好的性能。