使用 PostgreSQL JDBC 的连接池

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

Connection Pooling with PostgreSQL JDBC

postgresqljdbcconnection-pooling

提问by insumity

Recently I downloaded the JDBC driver for PostgreSQL from here. Since I'm using Java 1.7 JVM and it's written:

最近我从这里下载了 PostgreSQL 的 JDBC 驱动程序。因为我使用的是 Java 1.7 JVM 并且它是这样写的:

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

如果您使用的是 1.6 或 1.7 JVM,那么您应该使用 JDBC4 版本。

I download JDBC4. The problem is there exist no PoolingDataSource'sin it. If you get JDBC3 you can use org.postgresql.jdbc3.Jdbc3PoolingDataSourceor others as is seen here.

我下载了JDBC4。问题是它不存在PoolingDataSource's。如果你JDBC3你可以使用org.postgresql.jdbc3.Jdbc3PoolingDataSource或他人如看到这里

Is there any pooling DataSourcein JDBC4 that I don't know about, or what should I use instead? The only thing I found in JDBC4 is PGPoolingDataSourcebut I'm not sure if I'm supposed to use this because based on their Java doc message:

DataSourceJDBC4 中是否有任何我不知道的池,或者我应该使用什么?我在 JDBC4 中发现的唯一一件事是,PGPoolingDataSource但我不确定是否应该使用它,因为基于他们的 Java 文档消息:

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

如果您的服务器/中间件供应商提供了与 PostgreSQL ConnectionPoolDataSource 实现接口的连接池实现,请不要使用它!

回答by krokodilko

Use org.postgresql.ds.PGPoolingDataSource
Here is an example: http://jdbc.postgresql.org/documentation/head/ds-ds.html
I've checked this example using JDBC4 driver and it worked fine.

However in documentation from this link they discourage from using postgreSQL pooling data source because of it's limitations:

使用org.postgresql.ds.PGPoolingDataSource
这是一个例子: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.

这里提供的池化数据源实现并不是世界上功能最丰富的。除其他外,在池本身关闭之前,连接永远不会关闭;没有办法缩小池。同样,为默认配置用户以外的用户请求的连接不被合并。它的错误处理有时无法从池中删除断开的连接。一般不推荐使用PostgreSQL吗?提供的连接池。检查您的应用程序服务器或检查优秀的 jakarta commons DBCP 项目。

They recommend to use DBCP connection pool: http://commons.apache.org/proper/commons-dbcp/check it, it is much better - just download library files, place them in a classpatch and import to the project, documentation from the above link contains examples how to use it in code.

Most (all?) application servers implements their own connection pools, if you are using the application server, it's the best option.
For Example Tomcat 7 has it's own implementation of the connection pool, it's even better than DBCP, check documentation: http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

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

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