0

为了使用 tomcat 作为服务器连接到 derby 数据库,我最初在conf/context.xmlTomcat 中添加了以下内容:

<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
 driverClassName="org.apache.derby.jdbc.ClientDriver"
 url="jdbc:derby://localhost:1527/poll database;create=true"
 username="suhail" password="suhail"
 maxActive="20" maxIdle="10" maxWait="-1" />

以及项目Resource-ref的 web.xml 中的标签WEB-INF/web.xml

<resource-ref>
<description>my connection</description>
<res-ref-name>jdbc/PollDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

但是当我运行一个必须连接到数据库的 servlet 时,语句:

connection = dataSource.getConnection();

导致异常:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

我还不明白这个异常的原因

在异常之后,我Resource从 Tomcat 中删除了标签conf/context.xml并将其放在META-INF/context.xml我的项目中。

当我再次尝试 servlet 时,它毫无例外地工作了!

Resource当我将标签放在全局 context.xml 文件中(即在 conf/context.xml 中)时出现异常的原因可能是什么,但是当我将标签放在特定于我的应用程序的 context.xml 中时却没有出现异常?. (即在 META-INF/context.xml 中)

4

1 回答 1

0

添加<Resource>conf/context.xml将使该资源的副本可用于您在服务器上部署的每个 web 应用程序 - 可能不是您想要做的。如果你想让 a<Resource>全局可用,把它放到conf/server.xml下更合适<GlobalNamiongResources>

我希望您遇到的错误是因为您没有在正确的位置安装 JDBC 驱动程序。定义<Resource>inconf/context.xml可能会导致使用不同的 ClassLoader 来加载您的<Resource>.

于 2012-07-21T18:28:55.800 回答