Tomcat 5.5 在启动时无法识别数据源(空组件 Catalina:type=DataSource)

发布于 2024-10-03 14:43:26 字数 2279 浏览 1 评论 0原文

我在本地 PC 上设置了 Postgres 9.0.1 SQL 服务器和 Tomcat 5.5.28。两者都运行良好。但我无法配置 Tomcat 以通过 JNDI 提供数据源。我想我已经按照 Tomcat 页面中的 JNDI HOW-TO 进行操作(并且也尝试了其他页面中的其他一些操作方法)。但每次 Tomcat 启动时,他都会显示错误消息:

SEVERE: Null component Catalina:type=DataSource,path=/MyApp,host=localhost,class=javax.sql.DataSource,name="jdbc/postgres"

Tomcat 确实启动了,但我的数据库代码将无法工作,因为我的 InitialContext.lookup 最终什么也没有。

postgres 驱动程序“postgresql-8.4-701.jdbc4.jar”被复制到以下文件夹中:

I:\Apache Software Foundation\Tomcat 5.5\common\lib

I:\Apache Software Foundation\Tomcat 5.5\webapps\MyApp\WEB- INF\lib

这是我的 MyApp/META-INF/context.xml:

<Context path="/MyApp" docBase="MyApp" crossContext="true" reloadable="true" debug="1">
    <Resource name="jdbc/postgres" 
        auth="Container"
            type="javax.sql.DataSource" 
            username="postgres" 
            password="xxx" 
            driverClassName="org.postgresql.Driver"
            url="jdbc:postgresql://localhost/MyDatabase"
            maxWait="1000"
            maxActive="20" 
            maxIdle="10">
    </Resource>
</Context>

这是我的 MyApp/WEB-INF/web.xml 的摘录:

<web-app>
…
<resource-ref>
    <description>
        Postgres resource reference to a factory
    </description>
    <res-ref-name>
        jdbc/postgres
    </res-ref-name>
    <res-type>
        javax.sql.DataSource
    </res-type>
    <res-auth>
        Container
    </res-auth>
  </resource-ref>
</web-app>

我的项​​目由 Eclipse 部署为 WAR,Tomcat 在启动时将 WAR 分解为文件夹。现在奇怪的是:如果我不手动启动 Tomcat,而是通过 Eclipse 启动,一切都会正常! Tomcat 没有出现上述错误消息。 我发现 Eclipse 在启动 Tomcat 本身时使用它自己的 server.xml、context.xml 等,但即使在将这些文件与 Tomcat 的常规配置文件进行比较之后,我到目前为止发现的唯一区别是 Eclipse 服务器中的以下附加行。在关闭“host”标记之前添加 xml:

<Context docBase="MyApp" path="/MyApp" reloadable="true" source="org.eclipse.jst.j2ee.server:MyApp"/>

但是即使我将这一行复制到 Tomcat 自己的 server.xml(没有“source”属性)中,如果没有 Eclipse,它也无法工作。因此 Eclipse 必须做一些“额外”的事情,使得找到数据源成为可能,而“正常”的 Tomcat 配置会失败。由于我必须在不同的服务器(没有 Eclipse)上部署 WAR,因此我需要帮助才能在没有 Eclipse 的情况下运行它。我认为我的代码没问题,因为它与 Eclipse 的 Tomcat 一起工作得很好,但我的 Tomcat 5.5 配置一定有问题。有什么想法吗?

I have set up a Postgres 9.0.1 SQL server and a Tomcat 5.5.28 both at my local PC. Both are running fine. But I have not been able to configure Tomcat to make the DataSource available via JNDI. I think I have followed the JNDI HOW-TO from the Tomcat pages to the letter (as well as having tried some other how-to's from other pages, too). But everytime Tomcat starts he displays the error message:

SEVERE: Null component Catalina:type=DataSource,path=/MyApp,host=localhost,class=javax.sql.DataSource,name="jdbc/postgres"

Tomcat does start nonetheless, but my database code won't work, because my InitialContext.lookup ends up with nothing.

The postgres driver "postgresql-8.4-701.jdbc4.jar" is copied into the following folders:

I:\Apache Software Foundation\Tomcat 5.5\common\lib

I:\Apache Software Foundation\Tomcat 5.5\webapps\MyApp\WEB-INF\lib

Here is my MyApp/META-INF/context.xml:

<Context path="/MyApp" docBase="MyApp" crossContext="true" reloadable="true" debug="1">
    <Resource name="jdbc/postgres" 
        auth="Container"
            type="javax.sql.DataSource" 
            username="postgres" 
            password="xxx" 
            driverClassName="org.postgresql.Driver"
            url="jdbc:postgresql://localhost/MyDatabase"
            maxWait="1000"
            maxActive="20" 
            maxIdle="10">
    </Resource>
</Context>

Here is an excerpt from my MyApp/WEB-INF/web.xml:

<web-app>
…
<resource-ref>
    <description>
        Postgres resource reference to a factory
    </description>
    <res-ref-name>
        jdbc/postgres
    </res-ref-name>
    <res-type>
        javax.sql.DataSource
    </res-type>
    <res-auth>
        Container
    </res-auth>
  </resource-ref>
</web-app>

My project is deployed as a WAR by Eclipse and Tomcat explodes the WAR at startup as a folder. Now the strange thing: If I doesn't start Tomcat by hand, but by Eclipse, everything works!!! Tomcat doesn't come up with the error message above.
I have discovered Eclipse uses its own server.xml, context.xml etc. when it starts Tomcat itself, but even after comparing those files with Tomcat's regular config files the only difference I have spotted so far is the following additional line in Eclipse's server.xml before the closing "host" tag:

<Context docBase="MyApp" path="/MyApp" reloadable="true" source="org.eclipse.jst.j2ee.server:MyApp"/>

But even when I copy this line into Tomcat's own server.xml (without the "source"-attribute), it doesn't work without Eclipse. So Eclipse must do something "extra" which makes finding the DataSource possible, while the "normal" Tomcat configuration fails. Since I have to deploy the WAR on a different server (without Eclipse) I need help to make this run without Eclipse. I think my code is ok, since it works fine with Eclipse's Tomcat, but my configuration of Tomcat 5.5 must be faulty. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2024-10-10 14:43:26

在花了更多时间解决这个问题之后,我找到了一种避免 Tomcat 错误消息的方法。看来我的 Eclipse 配置有问题,因为它被配置为将 Tomcat 自己的一些 jar 添加到文件夹“I:\Apache Software Foundation\Tomcat 5.5\webapps\MyApp\WEB-INF\lib”中。在我将 Eclipse 的设置更改为不包含它们,而是将这些库用作引用库后,它们不再包含在 WAR 文件中,并且 Tomcat 启动时没有错误。更准确地说,必须删除我的 web 应用程序库中重复的以下三个 jar 以避免出现消息:“naming-factory.jar”、“naming-factory-dbcp.jar”和“naming-resources.jar” ”。

有趣的是,虽然 Tomcat 现在启动正常,但结果仍然相同:如果从 Eclipse 内部启动,我的 Web 应用程序将成功通过 JNDI 查找数据源,但是当我在不使用 Eclipse 的情况下启动 Tomcat 时,Web 应用程序在尝试执行以下操作时仍然会失败:访问数据库时出现以下错误消息:

Cannot create JDBC driver of class '' for connect URL 'null'

我也花了一些时间才使其正常工作。我不得不放弃定义特定于应用程序的数据源的首选方法,而是定义了一个全局数据源。我仍然不知道为什么 Tomcat 拒绝理解我的应用程序特定设置,但至少可以解决这个问题。以下是解决我所有问题并允许我进行 JNDI 查找的步骤:

  1. 修改 %CATALINA_HOME%/conf/server.xml 中“GlobalNamingResources”标签内的 server.xml 文件:这里我添加了完整的“资源” 修改 %CATALINA_HOME%/conf/server.xml中

  2. 修改%CATALINA_HOME%/conf/context.xml中“Context”标签内的context.xml文件:这里我添加了一行:

我留下了特定于 webapp 的 web.xml 和 context.xml不变(如上所示)。现在一切正常。我发布了我发现的问题的解决方案,希望其他有类似问题的人可以受益。

After spending some more hours on the problem I have found a way to avoid Tomcat's error message. It seems my Eclipse configuration was faulty, because it was configured to add some of Tomcat's own jars into the folder "I:\Apache Software Foundation\Tomcat 5.5\webapps\MyApp\WEB-INF\lib". After I changed Eclipse's setting to not include them but to use those libraries as referenced ones, they weren't included in the WAR file anymore and Tomcat started without error. To be more precise, the following three jars being duplicated in the lib of my webapp had to be removed to avoid the message: "naming-factory.jar", "naming-factory-dbcp.jar" and "naming-resources.jar".

Interestingly, although Tomcat was starting fine now, the result remained the same: If started from within Eclipse, my webapp would succeed with the JNDI lookup for the datasource, but when I started Tomcat without using Eclipse, the webapp still would fail when trying to access the database with the error message below:

Cannot create JDBC driver of class '' for connect URL 'null'

It took me some more time to get this working, too. I had to give up on the preferred method of defining an app-specific datasource, but defined a global one instead. I still don't know WHY Tomcat refuses to understand my app-specific settings, but at least one can circumvent the problem. Following are the steps that solved all my problems and allowed me the JNDI lookup:

  1. Modifying the server.xml file in %CATALINA_HOME%/conf/server.xml inside the tag "GlobalNamingResources": Here I added the complete "Resource" tag section that was already in my context.xml (see above).

  2. Modifying the context.xml file in %CATALINA_HOME%/conf/context.xml inside the "Context" tag: Here I added the line: <ResourceLink global="jdbc/postgres" name="jdbc/postgres" type="javax.sql.DataSource"/>

I left the webapp-specific web.xml and context.xml unchanged (as seen above). Everything is working now. I post the solution to my problem I found myself in the hope others with a similar problem may benefit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文