java DBCP(Apache Commons 数据库连接池)是否仍然相关?

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

Is DBCP (Apache Commons Database Connection Pooling) still relevant?

javadatabasejdbcpoolingapache-commons-dbcp

提问by jdigital

The JDBC 3.0 spec talks about Connection (and Prepared Statement) pooling.

JDBC 3.0 规范谈到了连接(和准备好的语句)池。

We have several standalone Java programs (i.e. we are not using an application server) that have been using DBCP to provide connection pooling. Should we continue to use DBCP, or can we take advantage of the JDBC-provided pooling and get rid of DBCP?

我们有几个独立的 Java 程序(即我们没有使用应用服务器),它们一直在使用 DBCP 来提供连接池。我们应该继续使用 DBCP,还是可以利用 JDBC 提供的池来摆脱 DBCP?

We are using MySQL (Connector/J) and will eventually be adding SQL Server support (jTDS); it's unlikely that we'll support any other databases.

我们正在使用 MySQL (Connector/J),最终将添加 SQL Server 支持 (jTDS);我们不太可能支持任何其他数据库。

EDIT: See comment below about my attempt to eliminate the connection pooling library. It appears that DBCP is still relevant (note that some commenters recommended C3P0 over DBCP).

编辑:请参阅下面关于我试图消除连接池库的评论。似乎 DBCP 仍然相关(注意一些评论者推荐 C3P0 而不是 DBCP)。

采纳答案by jdigital

Based on the encouragement of other posters, I attempted to eliminate DBCP and use the MySQL JDBC driver directly (Connector/J 5.0.4). I was unable to do so.

在其他发帖者的鼓励下,我尝试取消 DBCP,直接使用 MySQL JDBC 驱动程序(Connector/J 5.0.4)。我无法这样做。

It appears that while the driver does provide a foundation for pooling, it does not provide the most important thing: an actual pool (the source code came in handy for this). It is left up to the application server to provide this part.

看起来虽然驱动程序确实提供了池化的基础,但它并没有提供最重要的东西:一个实际的池(源代码为此派上了用场)。这部分由应用服务器来提供。

I took another look at the JDBC 3.0 documentation (I have a printed copy of something labeled "Chapter 11 Connection Pooling", not sure exactly where it came from) and I can see that the MySQL driver is following the JDBC doc.

我又看了看 JDBC 3.0 文档(我有一份标有“第 11 章连接池”的打印副本,不确定它的确切来源),我可以看到 MySQL 驱动程序遵循 JDBC 文档。

When I look at DBCP, this decision starts to make sense. Good pool management provides many options. For example, when do you purge unused connection? which connections do you purge? is there a hard or soft limit on the max number of connections in the pool? should you test a connection for "liveness" before giving it to a caller? etc.

当我查看 DBCP 时,这个决定开始变得有意义了。良好的池管理提供了许多选择。例如,何时清除未使用的连接?你清除哪些连接?池中的最大连接数是否有硬或软限制?在将连接提供给呼叫者之前,您应该测试连接的“活跃度”吗?等等。

Summary: if you're doing a standalone Java application, you need to use a connection pooling library. Connection pooling libraries are still relevant.

总结:如果你在做一个独立的Java 应用程序,你需要使用一个连接池库。连接池库仍然相关。

回答by erickson

DBCP has serious flaws. I don't think it's appropriate for a production application, especially when so many drivers support pooling in their DataSourcenatively.

DBCP 有严重的缺陷。我认为它不适合生产应用程序,尤其是当有这么多驱动程序在其DataSource本机支持池时。

The straw that broke the camel's back, in my case, was when I found that the entire pool was locked the whole time a new connection attempt is made to the database. So, if something happens to your database that results in slow connections or timeouts, other threads are blocked when they try to return a connection to the pool—even though they are done using a database.

就我而言,压垮骆驼的最后一根稻草是当我发现整个池在尝试连接到数据库的整个过程中都被锁定时。因此,如果您的数据库发生某些事情导致连接缓慢或超时,则其他线程在尝试将连接返回到池时会被阻塞 — 即使它们是使用数据库完成的。

Pools are meant to improve performance, not degrade it. DBCP is naive, complicated, and outdated.

池旨在提高性能,而不是降低性能。DBCP 是幼稚、复杂和过时的。

回答by Yoni

I prefer using dbcp or c3p0 because they are vendor neutral. I found out, at least with mysql or oracle, that whenever I try to do something with the jdbc client that is not standard sql I have to introduce compile-time dependency on the vendor's classes. See, for example, a very annoying example here.

我更喜欢使用 dbcp 或 c3p0,因为它们是供应商中立的。我发现,至少对于 mysql 或 oracle,每当我尝试对 jdbc 客户端做一些不是标准 sql 的事情时,我都必须引入对供应商类的编译时依赖。例如,请参阅此处的一个非常烦人的示例。

I am not sure about mysql, but oracle uses their specific, non-standard classes for connection pooling.

我不确定 mysql,但 oracle 使用其特定的非标准类进行连接池。

回答by Uri

People still use DBCP, I think it even comes as a default with Hibernate.

人们仍然使用 DBCP,我认为它甚至是 Hibernate 的默认设置。

Is DBCP not meeting your current needs?

DBCP 不能满足您当前的需求吗?

I'm not a big believer in replacing infrastructure unless there's already a performance or functionality gap that it can't fill, even if there are newer or fancier alternatives around.

我不太相信更换基础设施,除非已经存在无法填补的性能或功能差距,即使周围有更新或更好的替代方案。