简体   繁体   English

在 Tomcat 7 中使用 Tomcat JDBC 连接池时出现问题

[英]Problems using Tomcat JDBC Connection Pool in Tomcat 7

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.我猜它无法获取我指定的驱动程序信息。

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 下调试的问题,所以我没有使用我认为我正在使用的 server.xml。 Eclipse copies the one under the tomcat directory. Eclipse 复制 tomcat 目录下的那个。 Solution is to delete and the recreate under Eclipse to get changes in server.xml to be effective.解决方法是删除并在Eclipse下重新创建以使server.xml中的更改生效。

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

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