java BasicDatasource 和 PoolingDatasource 的区别

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

Difference between BasicDatasource and PoolingDatasource

javaconnection-poolingapache-commons-dbcp

提问by sam

What is the difference between org.apache.commons.dbcp BasicDatasource and PoolingDataSoure? Do both support pooling of connections? When to use each of them ?

org.apache.commons.dbcp BasicDatasource 和 PoolingDataSoure 有什么区别?两者都支持连接池吗?什么时候使用它们?

回答by helios

BasicDataSourceis, as the javadocsays, a one-stop shopping for basic needs. It has all the necessary. It creates internally a PoolableDataSource and an ObjectPool.

正如javadoc所说,BasicDataSource是满足基本需求的一站式服务。它拥有所有必要的东西。它在内部创建了一个 PoolableDataSource 和一个 ObjectPool。

PoolingDataSourceimplements the DataSource interface using a provided ObjectPool. PoolingDatasource take cares of whatever has to do with connections (casting, checking validity, setting properties, etc) and ObjectPool take cares of holding and counting this whatever-type-it-is object.

PoolingDataSource使用提供的 ObjectPool 实现 DataSource 接口。PoolingDatasource 负责处理与连接有关的任何事情(转换、检查有效性、设置属性等),而 ObjectPool 负责持有和计算这个任何类型的对象。

So I should use BasicDataSource. If you need something special maybe can use PoolingDatasource with another implementation of ObjectPool. But I think it would be a rare case.

所以我应该使用 BasicDataSource。如果您需要一些特殊的东西,可以将 PoolingDatasource 与 ObjectPool 的另一个实现一起使用。但我认为这将是一种罕见的情况。