应用程序服务器JDBC资源的DataSource或ConnectionPoolDataSource [英] DataSource or ConnectionPoolDataSource for Application Server JDBC resources

查看:243
本文介绍了应用程序服务器JDBC资源的DataSource或ConnectionPoolDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序服务器中创建JNDI JDBC连接池时,我始终将类型指定为 javax.sql.ConnectionPoolDataSource 。我从来没有真正地给它太多的想法,因为它总是似乎更喜欢池的连接超过非池。



但是,在看一些例子(专门为Tomcat )我注意到他们指定 javax .sql.DataSource 。此外,似乎有 maxIdle maxWait 的设置,给人的印象是这些连接也被合并。




  • javax.sql.DataSource

    code>在应用程序服务器(或servlet容器)中合并?

  • 如果有任何优点,请选择 javax.sql.ConnectionPoolDataSource over javax.sql.DataSource (或反之亦然)?


解决方案

是的,Tomcat默认对定义为JNDI上下文资源的DataSources使用Apache DBCP池。



b $ b http://tomcat.apache.org/tomcat-7.0-doc /jndi-resources-howto.html#JDBC_Data_Sources


注意 - 默认数据源支持Tomcat中的
基于DBCP
连接池从Commons
项目。然而,有可能
使用任何其他连接池
实现javax.sql.DataSource,通过
编写您自己的自定义资源
工厂,如下所述。


挖掘Tomcat 6源显示他们以这种方式获取连接工厂(如果你没有使用Context的factory属性指定自己的工厂)

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

和实现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?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?format = ok < a>



奇怪的是,此类不实现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



所以我假设当你配置你的DataSources as javax.sql.ConnectionPoolDataSource你也使用了一些自定义工厂(这只是一个猜测,但我想,否则你会有类转换异常在Tomcat,因为他们的池不真正提供javax.sql的实例。因此,要回答有关特定情况的优点或缺点的问题,您应该在您的DataSource工厂中将Apache DBCP与池机制进行比较,无论您使用哪一个。


When creating JNDI JDBC connection pools in an application server, I always specified the type as 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. Further, it seems there are settings for maxIdle and maxWait giving the impression that these connections are pooled as well. Glassfish also allows these parameters regardless of the type of data source selected.

  • Are javax.sql.DataSource pooled in an application server (or servlet container)?
  • What (if any) advantages are there for choosing javax.sql.ConnectionPoolDataSource over javax.sql.DataSource (or vice versa)?

解决方案

Yes, Tomcat does use Apache DBCP pooling by default for DataSources defined as JNDI Context resources.

From documentation at 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. 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.

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):

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

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

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

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).

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.

这篇关于应用程序服务器JDBC资源的DataSource或ConnectionPoolDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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