PostgreSQL JDBC的连接池 [英] Connection Pooling with PostgreSQL JDBC

查看:1266
本文介绍了PostgreSQL JDBC的连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我从此处下载了PostgreSQL的JDBC驱动程序. 由于我使用的是Java 1.7 JVM,因此它写为:

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

如果使用的是1.6或1.7 JVM,则应使用JDBC4 版本.

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

我下载JDBC4.问题是其中不存在PoolingDataSource's. 如果您获得JDBC3,则可以使用org.postgresql.jdbc3.Jdbc3PoolingDataSource此处

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

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

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

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

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

推荐答案

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

但是,在此链接的文档中,由于其局限性,他们不鼓励使用postgreSQL池数据源:

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:

此处提供的池化数据源实现并不是世界上功能最丰富的.除其他事项外,在池本身关闭之前,连接永远不会关闭.没有办法缩小池.同样,为默认配置用户以外的用户请求的连接也不会被合并.其错误处理有时无法从池中删除断开的连接.通常,不建议使用PostgreSQL提供的连接池.检查您的应用程序服务器或查看出色的jakarta commons DBCP项目.

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/ 检查它,它会更好-只需下载库文件,将它们放在classpatch中,然后导入到项目中,上面链接中的文档包含示例如何在代码中使用它的示例.

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

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

这篇关于PostgreSQL JDBC的连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆