本文介绍了应用程序服务器JDBC资源的DataSource或ConnectionPoolDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在应用程序服务器中创建JNDI JDBC连接池时,我始终将类型指定为 javax.sql.ConnectionPoolDataSource 。我从来没有真正地给它太多的想法,因为它总是似乎自然喜欢池的连接非池化。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.但是,在看一些例子(具体为Tomcat )我注意到他们指定 javax.sql.DataSource 。此外,似乎有 maxIdle 和 maxWait 的设置给人的印象是这些连接也被合并。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. 是 javax.sql.DataSourceAre 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)?推荐答案是的,Tomcat默认对定义为JNDI上下文资源的DataSources使用Apache DBCP池。Yes, Tomcat does use Apache DBCP pooling by default for DataSources defined as JNDI Context resources. b $ b http://tomcat.apache.org/tomcat-7.0-doc /jndi-resources-howto.html#JDBC_Data_Sources挖掘Tomcat 6来源显示,他们以这种方式获取连接工厂(如果您没有使用上下文的工厂属性指定自己的) :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();和实现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 = okAnd 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我看到他们创建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 = okI 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奇怪的是,这个类不实现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 = okOddly enough, this class doesn't implement ConnectionPoolDataSource itself, neither does org.apache.tomcat.dbcp.dbcp.PoolingDataSource, that's returned internally by BasicDataSourcehttp://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的实例。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).因此,要回答有关特定情况的优点或缺点的问题,您应该在您的DataSource工厂中将Apache DBCP与池机制进行比较,无论您使用哪一个。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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 12:17