如何在tomcat 8中正确配置jndi数据源 [英] how to correctly configure jndi data source in tomcat 8

查看:423
本文介绍了如何在tomcat 8中正确配置jndi数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在

-tomcat_home- \ conf \ Catalina \ localhost

-tomcat_home-\conf\Catalina\localhost

我的应用程序是"reportExport" .war,所以我创建了具有以下内容的reportExport.xml:

My app is "reportExport".war so i created reportExport.xml with this content:

<Context>
    <Resource name="jdbc/mssql" auth="Container" type="javax.sql.DataSource"
    username="user"
    password="pass"
    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    url="jdbc:sqlserver://localhost:52015;databaseName=rewe;integratedSecurity=false;"
    maxActive="20"
    maxIdle="10"
    validationQuery="select 1" />
</Context>

我在web.xml中添加了以下内容:

I added to web.xml this:

<resource-ref>
    <description>
        This app requires a ms sql connection.
    </description>
    <res-ref-name>
        jdbc/mssql
    </res-ref-name>
    <res-type>
        javax.sql.DataSource
    </res-type>
    <res-auth>
        Container
    </res-auth>
</resource-ref>

可能我可以省略我已经在reportExport.xml中提供的字段吗?!

Probably i can omit the fields i already gave in reportExport.xml?!

在Java中,我尝试获得这样的连接:

in Java i try to get an connection like this:

((DataSource) (new InitialContext()).lookup("java:comp/env/jdbc/mssql")).getConnection()

该连接在Java中正常工作,但是有2个问题.

The connection is working fin in java but there are 2 problems.

第一个问题是:如果我在部署应用程序tomcat之前将reportExport.xml放入正确的路径,则会抛出异常:

The first problem is: If i put reportExport.xml into the correct path before deploying the app tomcat throws exception:

 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].Sta
ndardContext[/reportExport]]
...
Caused by: java.lang.IllegalArgumentException: The main resource set specified [C:\Users\moritz\entwicklung\apache-tomca
t-8.0.18\webapps\reportExport] is not valid
...

11-Feb-2015 14:15:38.303 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Error de
ploying configuration descriptor C:\Users\moritz\entwicklung\apache-tomcat-8.0.18\conf\Catalina\localhost\reportExport.x
ml
...

部署Web应用程序后,开始就可以了. 第二个问题是,在取消部署Web应用程序时会删除reportExport.xml.

After deploying the webapp the start is fine. The second problem is the reportExport.xml is being deleted on undeploying the web app.

所以我的设置有问题:我希望系统管理员为我的应用程序设置特定的数据源.我喜欢每个Web应用程序使用单独文件的想法,但是第一次安装时的例外情况以及在取消部署时的删除情况都是不好的. 另一方面,最好在server.xml中设置数据源,但是如何在其中配置"context"元素以使数据源仅可用于特定的应用程序?

So something is wrong with my setup: i want the system administrator to setup the data source specific for my application. I like the idea with a separate file per web app but the exception on first setup and the deletion on undeploying are bad. On the other hand it is probably good to setup data source in server.xml, but how to configure "context" element there to make datasource only available for specific application?

推荐答案

好的, 最简单的方法: -在server.xml中使用与reportExport.xml文件相同的语法声明全局资源(几乎在顶部). 如果您为每个数据源命名不同,例如jdbc/reportExport 而不是每个Web应用程序都可以使用不同的连接,您只需更新代码即可调用自定义名称". 因此,在MET-INF/context.xml文件中的.war内部(在Web/META_INF/context.xml中构建战争之前),您需要引用全局资源:

OK, the easiest way: - in the server.xml declare global resources (almost on the top) using the same syntaxt as in your reportExport.xml file. If you name each DataSource differenlty, for example jdbc/reportExport than each of your webapps can use different connection you just have to update the code to call the 'customized name'. So inside you .war in MET-INF/context.xml file (before you build the war in the web/META_INF/context.xml) you need to refrence the global resources:

<ResourceLink global="jdbc/reportExport" name="jdbc/reportExport" type="javax.sql.DataSource"/>

,然后从您的代码中正常访问它.

and then access it normally from you code.

如果需要,甚至可以在context.xml中将其重命名为jdbc/mssql,因此您的Java代码将像以前一样看到它,但是它将指向正确的数据源.但是我建议在每个应用程序中保留不同的名称,以便您直接看到正在访问的内容(个人喜好).

If you want you can even rename it in the context.xml to jdbc/mssql, so your java code will see it as before but it will point to the right data source. But I would recommend to keep different names in each app so you directly see what you are accessing (personal taste).

该解决方案的主要问题是,另一个Web应用程序可以在context.xml中引用任何全局资源并获得对它们的访问权.但是,如果您负责代码/服务器,那应该没什么大不了的.

The main problem with that solution is that another web app can reference any of the global resources in its context.xml and gets access to them. However if you are in charge of the code/server it should not be a big deal.

如果是,则无需手动将.xml文件放在webapps文件夹中,而是将其内容添加到默认主机元素(某些位于server.xml末尾)下的上下文描述中. 但是tomcat总是会在那寻找被拒绝的应用程序(因此,当您取消部署应用程序时,您会收到错误消息),而且如果您向元素中添加新资源(变量而不是全局数据源),则必须更新服务器端上下文描述再次.

If it is, you don't manually put .xml file in webapps folder but you add its content to the context description under default host element (some at the end of the server.xml). But tomcat will always look for applications refrenced there (so you will get error messages when you undeploy application), also if you add new resource to your element (a variable not a global data source) your server side context description would have to be updated again.

因此,从安全角度来看,如果这不是至关重要的,那么GlobalResources加上在Web应用程序的context.xml中重新引用它们是最简单的方法.

So if it is not crucial from security point of view, then GlobalResources plus re-referencing them in the context.xml of your web-app is the easiest way.

这篇关于如何在tomcat 8中正确配置jndi数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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