postgresql - 使用 PostgreSQL JDBC 的连接池

标签 postgresql jdbc connection-pooling

最近我从 here 下载了 PostgreSQL 的 JDBC 驱动程序。 . 由于我使用的是 Java 1.7 JVM,它是这样写的:

If you are using the 1.6 or 1.7 JVM, then you should use the JDBC4 version.

我下载 JDBC4。问题是其中不存在 PoolingDataSource 的。 如果你得到 JDBC3,你可以使用 org.postgresql.jdbc3.Jdbc3PoolingDataSource 或 其他如所见here .

JDBC4 中是否有任何我不知道的池化DataSource,或者应该是什么 我改用?我在 JDBC4 中找到的唯一东西是 PGPoolingDataSource但我不是 确定我是否应该使用它,因为基于他们的 Java 文档消息:

Don't use this if your server/middleware vendor provides a connection pooling implementation which interfaces with the PostgreSQL ConnectionPoolDataSource implementation!

最佳答案

使用org.postgresql.ds.PPGoolingDataSource
这是一个例子:http://jdbc.postgresql.org/documentation/head/ds-ds.html
我已经使用 JDBC4 驱动程序检查了这个示例,它工作正常。

但是,在此链接的文档中,他们不鼓励使用 postgreSQL 池数据源,因为它有局限性:

The pooling data-source implementation provided here is not the most feature-rich in the world. Among other things, connections are never closed until the pool itself is closed; there is no way to shrink the pool. As well, connections requested for users other than the default configured user are not pooled. Its error handling sometimes cannot remove a broken connection from the pool. In general it is not recommended to use the PostgreSQL˘ provided connection pool. Check your application server or check out the excellent jakarta commons DBCP project.

他们推荐使用 DBCP 连接池:http://commons.apache.org/proper/commons-dbcp/ 检查它,它好多了 - 只需下载库文件,将它们放在类补丁中并导入到项目中,上面链接中的文档包含如何在代码中使用它的示例。

大多数(所有?)应用服务器都实现了自己的连接池,如果您正在使用应用服务器,这是最好的选择。
例如 Tomcat 7 有它自己的连接池实现,它甚至比 DBCP 更好,查看文档:http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

关于postgresql - 使用 PostgreSQL JDBC 的连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19203989/

相关文章:

sql - 仅当所有需求行都存在时才从表中选择徽章

php - 运行 Laravel 的 artisan session 时出错 :table with pgSQL

java - 将用于连接的对象实例变量设置为静态或非静态更好吗

java - JDBC 中缺少 dll

.net - NHibernate 和 ADO.NET 连接池

oracle - 与 Oracle 数据库的 tomcat jdbc 连接池

postgresql - 在 pg_restore 期间排除表

sql - 从没有年份表的范围中选择所有年份

Java 准备语句 nullPointerException

java - 使用 JedisPool 的 Redis 连接池变得过于冗长