java 在Tomcat 7中使用Tomcat JDBC Connection Pool的问题

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

Problems using Tomcat JDBC Connection Pool in Tomcat 7

javatomcatjdbcconnection-poolingtomcat7

提问by Patrick

In server.xml:

在 server.xml 中:

<GlobalNamingResources>
<Resource name="jdbc/ArchiveDB" 
    auth="Container" 
    type="javax.sql.DataSource" 
    driverClassName="oracle.jdbc.OracleDriver"
    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
    ...etc

In web.xml:

在 web.xml 中:

<resource-ref>
<description>Archive Database</description>
<res-ref-name>jdbc/ArchiveDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>  

My Code:

我的代码:

Context ic = new InitialContext();         
DataSource ds = (DataSource) ic.lookup( "java:/comp/env/jdbc/ArchiveDB" );             

I am getting the following exception:

我收到以下异常:

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource

Any idea what I have got wrong? It seems that the factory field in the resource is not being used, but I have no idea how to find out why. Any ideas how I can progress?

知道我出了什么问题吗?似乎资源中的工厂字段没有被使用,但我不知道如何找出原因。我有什么想法可以进步吗?

Update 1. Delving into source code, found the following in ResourceFactory.java

更新1.深入源码,发现ResourceFactory.java如下

if (ref.getClassName().equals("javax.sql.DataSource")) {
String javaxSqlDataSourceFactoryClassName =
    System.getProperty("javax.sql.DataSource.Factory",
    Constants.DBCP_DATASOURCE_FACTORY);

I guess I have to set that system property so it doesn't revert to default.

我想我必须设置该系统属性,以便它不会恢复为默认值。

Update 2. Have now set the following for startup:

更新 2. 现在已经为启动设置了以下内容:

-Djavax.sql.DataSource.Factory=org.apache.tomcat.jdbc.pool.DataSourceFactory

And getting different error:

并得到不同的错误:

09-Jun-2011 14:48:20 org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
java.sql.SQLException
at        org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:243)
Caused by: java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
    at     org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:236)
    ... 57 more
javax.naming.NamingException
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)

I guess it is not able to pick up the driver info I specified.

我想它无法获取我指定的驱动程序信息。

采纳答案by Patrick

Seems working now. I think this was a problem that I was debugging under Eclipse, so I wasn't using the server.xml that I thought I was using. Eclipse copies the one under the tomcat directory. Solution is to delete and the recreate under Eclipse to get changes in server.xml to be effective.

似乎现在工作。我认为这是我在 Eclipse 下调试的一个问题,所以我没有使用我认为正在使用的 server.xml。Eclipse 将其复制到 tomcat 目录下。解决方法是在Eclipse 下删除并重新创建,使server.xml 中的更改生效。