Eclipse:覆盖Tomcat中的JNDI资源 [英] Eclipse: Overriding JNDI Resource in Tomcat

查看:131
本文介绍了Eclipse:覆盖Tomcat中的JNDI资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse中使用Java Web应用程序,并部署到由Eclipse运行的Tomcat实例中。我试图让这个应用程序通过JNDI 资源元素与其他主机上的数据库进行交谈。



应用程序中包含的context.xml文件尝试连接到在localhost上运行的MySQL服务器,如下所示:

 <? xml version =1.0encoding =UTF-8?> 

<上下文>
< Environment name =log4j.configuration
value =/ path / to / installed / log4j.properties
type =java.lang.String/>

<资源名称=jdbc / configDBauth =容器type =javax.sql.DataSource
username =dbuserpassword =dbpassworddriverClassName =com .mysql.jdbc.Driver
url =jdbc:mysql:// localhost:3306 / dbnamemaxActive =8
validationQuery =SELECT 1testOnBorrow =true/>

<环境名称=mykeyvalue =已安装type =java.lang.String/>

< / Context>

当我将它安装在实际主机上时,此配置工作正常。也就是说,它正确读取环境值并连接到数据库。



我也可以将它安装在单独的主机上,并编辑 / etc / tomcat6 / conf / Catalina / localhost / myaplication.xml 更改JDBC URL,如下所示:

  ... 
<资源名称=jdbc / configDBauth =容器type =javax.sql.DataSource
username =dbuserpassword =dbpassworddriverClassName =com .mysql.jdbc.Driver
url =jdbc:mysql:// otherhost:3306 / dbnamemaxActive =8
validationQuery =SELECT 1testOnBorrow =true/>
...

此配置也可以正常工作。换句话说,我非常有信心我的应用程序正确使用JNDI。



所以,现在我想在Eclipse的桌面上部署到Tomcat。我以标准的方式将项目添加到Tomcat中,并且我已经编辑了服务器的context.xml文件(即Eclipse中在服务器项目中的Tomcat服务器上显示的)文件,如下所示:

 <?xml version =1.0encoding =UTF-8?> 

<上下文>
<环境名称=log4j.configuration
value = C:\workspace\myapplication\src\main\resources\conf\log_to_tomcat_console_log4j.properties
type =java.lang.String/>

<资源名称=jdbc / configDBauth =容器type =javax.sql.DataSource
username = dbuserpassword =dbpassworddriverClassName =com.mysql.jdbc.Driver
url =jdbc:mysql:// otherhost:3306 / dbnamemaxActive =8
validationQuery = SELECT 1testOnBorrow =true/>

<环境名称=mykeyvalue =desktoptype =java.lang.String/>

< / Context>

当我在Eclipse中重新启动Tomcat时,日志向我显示路径到Log4J文件正在正确读取(因为它开始登录到Tomcat控制台),我甚至看到它吐出正确的mykey值,但是当尝试从 configDB中读取时,初始化失败



起初,我以为这是一个连接问题,但在裁定之后,我在调试器中重新启动了Tomcat,并检查了从JNDI读取的DataSource;它的URL指向localhost,这意味着它在应用程序的context.xml文件中使用,而不是Tomcat的!



我已经尝试了几种方法来使其工作,包括放这个信息在Tomcat的server.xml文件中,但似乎没有任何工作。或者,更准确地说,它是一种工作,因为它正在阅读环境值,而不是资源。



我在这里做错了什么?如何让Eclipse的Tomcat服务器覆盖此资源?

解决方案

这里的基本问题是, context.xml 您在eclipse中编辑对应于 conf context.xml > tomcat目录。然后应用程序上下文文件中的首选项优先。正如您已经在tomcat的正常部署生命周期中所描述的,这是非常好的,因为您只需编辑应用程序设置。



您可以对由eclipse创建的上下文文件执行相同操作(位于 .metadata / .plugins / org.eclipse.wst.server.core / tmp0 / conf / Catalina / localhost 之间),但会尽快覆盖当您更改原始上下文文件( META-INF / context.xml )中的某些内容时。
所以实际上你需要在那里进行更改,但是这通常是没有问题的,因为这个文件必须在没有自定义更改的情况下部署。



但是这里是解决方法:通过滥用部署程序集,可以使eclipse另外使用另一个 META-INF / context.xml 。在其他地方创建一个新的 META-INF 目录,并在其中设置一个 context.xml 文件。然后在您的eclipse项目的项目属性部署程序集中添加新的路径/到/ META-INF META-INF

那么eclipse将覆盖原来的 context.xml 您的自定义和tomcat应该采取您的设置。


I'm working on a Java web application in Eclipse and deploying to an instance of Tomcat that is run by Eclipse. I'm trying to get this application to talk to a database on another host via a JNDI Resource element.

The context.xml file included in the application attempts to connect to a MySQL server running on localhost, like so:

<?xml version="1.0" encoding="UTF-8"?>

<Context>
    <Environment name="log4j.configuration"
    value="/path/to/installed/log4j.properties"
    type="java.lang.String" />

    <Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
    username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/dbname" maxActive="8"
    validationQuery="SELECT 1" testOnBorrow="true" />

    <Environment name="mykey" value="installed" type="java.lang.String" />

</Context>

This configuration works when I install it on the actual host. That is, it reads the environment values correctly and connects to the database.

I can also install this on a separate host and edit /etc/tomcat6/conf/Catalina/localhost/myaplication.xml to change the JDBC URL, like so:

...
    <Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
    username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
    validationQuery="SELECT 1" testOnBorrow="true" />
...

This configuration also works. In other words, I'm pretty confident that my application is using the JNDI correctly.

So, now I want to deploy this to Tomcat on my desktop in Eclipse. I've added the project to Tomcat in the standard way, and I've edited the context.xml file for the server (i.e., the one that Eclipse shows under my Tomcat server in the "Servers" project) like so:

<?xml version="1.0" encoding="UTF-8"?>

<Context>
    <Environment name="log4j.configuration"
    value=C:\workspace\myapplication\src\main\resources\conf\log_to_tomcat_console_log4j.properties"
    type="java.lang.String" />

    <Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
    username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
    validationQuery="SELECT 1" testOnBorrow="true" />

    <Environment name="mykey" value="desktop" type="java.lang.String" />

</Context>

When I then restart Tomcat in Eclipse, log shows me that the path to the Log4J file is being read correctly (because it starts logging to the Tomcat console) and I even see it spit out the correct "mykey" value. However, initialisation fails when it tries to read from the configDB.

At first, I thought this was a connectivity issue, but after ruling that out, I restarted Tomcat in the debugger and inspected the DataSource read from JNDI; its URL points to localhost, meaning it's using the one in the application's context.xml file, not Tomcat's!

I've tried several ways to get this working, including putting this information in Tomcat's server.xml file, but nothing seems to work. Or, more accurately, it sort of kind of works because it's reading the Environment values but not the Resource.

What am I doing wrong here? How do I get Eclipse's Tomcat server to override this Resource?

解决方案

The basic problem here is, that the context.xml you edit in eclipse corresponds to the main context.xml in the conf directory of tomcat. And then the preferences in the application context file takes precedence. As you already described within normal deployment lifecycle of tomcat this is perfectly fine, because you just edit the application settings.

You could do the same with the context file created by eclipse (which is located somewhere at .metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/Catalina/localhost) but it will be overwritten as soon as you change something in the original context file (META-INF/context.xml). So actually you would need to make the changes there, but this is normally out of question because this file must be deployed without custom changes.

But here is a workaround: You can make eclipse use another META-INF/context.xml by abusing the deployment assembly. Make a new META-INF directory somewhere else and place a context.xml file with your setting there. Then in the project properties of your eclipse project at Deployment Assembly add an additional mapping from your new path/to/META-INF to META-INF.
Then eclipse will overwrite the original context.xml with your custom one and tomcat should take your settings.

这篇关于Eclipse:覆盖Tomcat中的JNDI资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆