简体   繁体   English

Application Server JDBC资源的DataSource或ConnectionPoolDataSource

[英]DataSource or ConnectionPoolDataSource for Application Server JDBC resources

When creating JNDI JDBC connection pools in an application server, I always specified the type as javax.sql.ConnectionPoolDataSource . 在应用程序服务器中创建JNDI JDBC连接池时,我总是将类型指定为javax.sql.ConnectionPoolDataSource I never really gave it too much thought as it always seemed natural to prefer pooled connections over non-pooled. 我从来没有真正给过太多考虑,因为在非汇集时更喜欢汇集连接似乎总是很自然。

However, in looking at some examples ( specifically for Tomcat ) I noticed that they specify javax.sql.DataSource . 但是,在查看一些示例( 特别是Tomcat )时,我注意到它们指定了javax.sql.DataSource Further, it seems there are settings for maxIdle and maxWait giving the impression that these connections are pooled as well. 此外,似乎有maxIdlemaxWait设置给人的印象是这些连接也是合并的。 Glassfish also allows these parameters regardless of the type of data source selected. 无论选择何种类型的数据源,Glassfish都允许使用这些参数。

  • Are javax.sql.DataSource pooled in an application server (or servlet container)? javax.sql.DataSource是否在应用程序服务器(或servlet容器)中合并?
  • What (if any) advantages are there for choosing javax.sql.ConnectionPoolDataSource over javax.sql.DataSource (or vice versa)? javax.sql.DataSource选择javax.sql.ConnectionPoolDataSource有什么优点(如果有的话)(反之亦然)?

Yes, Tomcat does use Apache DBCP pooling by default for DataSources defined as JNDI Context resources. 是的,默认情况下,Tomcat对定义为JNDI Context资源的DataSource使用Apache DBCP池。

From documentation at http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#JDBC_Data_Sources 来自http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#JDBC_Data_Sources上的文档

NOTE - The default data source support in Tomcat is based on the DBCP connection pool from the Commons project. 注 - Tomcat中的默认数据源支持基于Commons项目的DBCP连接池。 However, it is possible to use any other connection pool that implements javax.sql.DataSource, by writing your own custom resource factory, as described below. 但是,可以通过编写自己的自定义资源工厂来使用实现javax.sql.DataSource的任何其他连接池,如下所述。

Digging Tomcat 6 sources revealed that they obtain connection factory this way (in case when you don't specify your own using Context's "factory" attribute): 挖掘Tomcat 6源代码显示他们以这种方式获得连接工厂(如果您没有使用Context的“factory”属性指定自己的工厂):

ObjectFactory factory = (ObjectFactory)Class.forName(System.getProperty("javax.sql.DataSource.Factory", "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory")).newInstance();

And org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory that implements javax.naming.spi.ObjectFactory takes care of creating DataSource instances: http://www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.2/tomcat-dbcp-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/BasicDataSourceFactory.java?format=ok 实现javax.naming.spi.ObjectFactory的org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory负责创建DataSource实例: http//www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat- DBCP / 7.0.2 / Tomcat的DBCP-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/BasicDataSourceFactory.java?格式= OK

I see they create instances of org.apache.tomcat.dbcp.dbcp.BasicDataSource: http://www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.2/tomcat-dbcp-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/BasicDataSource.java?format=ok 我看到他们创建了org.apache.tomcat.dbcp.dbcp.BasicDataSource的实例: http//www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.2/tomcat-dbcp- 7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/BasicDataSource.java?格式= OK

Oddly enough, this class doesn't implement ConnectionPoolDataSource itself, neither does org.apache.tomcat.dbcp.dbcp.PoolingDataSource, that's returned internally by BasicDataSource http://www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.2/tomcat-dbcp-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/PoolingDataSource.java?format=ok 奇怪的是,这个类本身并没有实现ConnectionPoolDataSource,org.apache.tomcat.dbcp.dbcp.PoolingDataSource也没有实现,它是由BasicDataSource内部返回的http://www.jarvana.com/jarvana/view/org/apache/tomcat /tomcat-dbcp/7.0.2/tomcat-dbcp-7.0.2-sources.jar!/org/apache/tomcat/dbcp/dbcp/PoolingDataSource.java?format=ok

So I presume when you configured your DataSources as javax.sql.ConnectionPoolDataSource you also used some custom-defined factory (it's just a guess, but I suppose otherwise you'd have class cast exceptions in Tomcat, since their pooling doesn't really provide instances of javax.sql.ConnectionPoolDataSource, only javax.sql.DataSource). 所以我假设当你将你的DataSources配置为javax.sql.ConnectionPoolDataSource时你也使用了一些自定义工厂(这只是一个猜测,但我想如果你在Tomcat中有类别转换异常,因为它们的汇集并没有真正提供javax.sql.ConnectionPoolDataSource的实例,只有javax.sql.DataSource)。

Thus, to answer questions about advantages or disadvantages of particular case you should compare Apache DBCP against pooling mechanism in your DataSource factory, whichever one you used. 因此,要回答有关特定情况的优缺点的问题,您应该将Apache DBCP与DataSource工厂中的池化机制进行比较,无论您使用哪个。

My understanding is that only purpose of ConnectionPoolDataSource is to give access to PooledConnection which implements native pooling by JDBC driver. 我的理解是, ConnectionPoolDataSource唯一目的是提供对通过JDBC驱动程序实现本机池的PooledConnection访问。 In this case application server can implement connections pooling using this native interface. 在这种情况下,应用程序服务器可以使用此本机接口实现连接池。

When using simple DataSource , appserver uses its own pooling instead of native. 使用简单的DataSource ,appserver使用自己的池而不是本机池。

Can't say which approach is best. 不能说哪种方法最好。

As for the Java docs it contains this: 至于Java文档,它包含:

DataSource Java 7 API DataSource Java 7 API

The DataSource interface is implemented by a driver vendor. DataSource接口由驱动程序供应商实现。 There are three types of implementations: 有三种类型的实现:

Basic implementation -- produces a standard Connection object 基本实现 - 生成标准Connection对象

Connection pooling implementation -- produces a Connection object that will automatically participate in connection pooling. 连接池实现 - 生成一个将自动参与连接池的Connection对象。 This implementation works with a middle-tier connection pooling manager. 此实现适用于中间层连接池管理器。

Distributed transaction implementation -- produces a Connection object that may be used for distributed transactions and almost always participates in connection pooling. 分布式事务实现 - 生成可用于分布式事务的Connection对象,并且几乎总是参与连接池。 This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager. 此实现与中间层事务管理器一起使用,并且几乎总是与连接池管理器一起使用。

PooledConnection Java 7 API PooledConnection Java 7 API

An application programmer does not use the PooledConnection interface directly; 应用程序员不直接使用PooledConnection接口; rather, it is used by a middle tier infrastructure that manages the pooling of connections. 相反,它由管理连接池的中间层基础结构使用。

When an application calls the method DataSource.getConnection, it gets back a Connection object. 当应用程序调用DataSource.getConnection方法时,它会返回一个Connection对象。 If connection pooling is being done, that Connection object is actually a handle to a PooledConnection object , which is a physical connection. 如果正在进行连接池,则该Connection对象实际上是PooledConnection对象的句柄,该对象是物理连接。

The connection pool manager , typically the application server, maintains a pool of PooledConnection objects .... 连接池管理器 (通常是应用程序服务器) 维护一个PooledConnection对象 ....

So in the end you just use DataSource and Connection classes and never PooledConnection / ConnectionPoolDataSource , if you are a happy and normal programmer. 所以最后你只使用DataSourceConnection类,而不是PooledConnection / ConnectionPoolDataSource ,如果你是一个快乐和正常的程序员。

If are implementing an Application Server that's another story... 如果正在实现另一个故事的应用服务器......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM