java 如何在tomcat 8中正确配置jndi数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28455532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to correctly configure jndi data source in tomcat 8
提问by dermoritz
I try to configure a jsbc data source for my app within
我尝试为我的应用程序配置一个 jsbc 数据源
-tomcat_home-\conf\Catalina\localhost
-tomcat_home-\conf\Catalina\localhost
My app is "reportExport".war so i created reportExport.xml with this content:
我的应用程序是“reportExport”.war 所以我用这个内容创建了 reportExport.xml:
<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>
I added to web.xml this:
我在 web.xml 中添加了以下内容:
<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>
Probably i can omit the fields i already gave in reportExport.xml?!
也许我可以省略我已经在 reportExport.xml 中给出的字段?!
in Java i try to get an connection like this:
在 Java 中,我尝试获得这样的连接:
((DataSource) (new InitialContext()).lookup("java:comp/env/jdbc/mssql")).getConnection()
The connection is working fin in java but there are 2 problems.
该连接在 Java 中工作正常,但存在 2 个问题。
The first problemis: If i put reportExport.xml into the correct path before deploying the app tomcat throws exception:
第一个问题是:如果我在部署应用程序之前将reportExport.xml放入正确的路径tomcat抛出异常:
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
...
After deploying the webapp the start is fine. The second problemis the reportExport.xml is being deleted on undeploying the web app.
部署 webapp 后,开始就好了。 第二个问题是在取消部署 Web 应用程序时删除了 reportExport.xml。
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?
所以我的设置有问题:我希望系统管理员为我的应用程序设置特定的数据源。我喜欢每个 Web 应用程序有一个单独文件的想法,但首次设置时的异常和取消部署时的删除很糟糕。另一方面,在 server.xml 中设置数据源可能很好,但是如何在那里配置“上下文”元素以使数据源仅可用于特定应用程序?
回答by Zielu
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:
好的,最简单的方法: - 在 server.xml 中使用与 reportExport.xml 文件中相同的语法声明全局资源(几乎在顶部)。如果您为每个数据源命名不同,例如 jdbc/reportExport,那么您的每个 web 应用程序都可以使用不同的连接,您只需更新代码以调用“自定义名称”。因此,在 MET-INF/context.xml 文件中的 .war 中(在 web/META_INF/context.xml 中构建War之前),您需要引用全局资源:
<ResourceLink global="jdbc/reportExport" name="jdbc/reportExport" type="javax.sql.DataSource"/>
and then access it normally from you code.
然后从您的代码中正常访问它。
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).
如果您愿意,您甚至可以在 context.xml 中将其重命名为 jdbc/mssql,这样您的 java 代码将像以前一样看到它,但它将指向正确的数据源。但我建议在每个应用程序中保留不同的名称,以便您直接看到您正在访问的内容(个人品味)。
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.
该解决方案的主要问题是另一个 Web 应用程序可以引用其 context.xml 中的任何全局资源并访问它们。但是,如果您负责代码/服务器,那应该没什么大不了的。
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.
如果是,您不会手动将 .xml 文件放在 webapps 文件夹中,而是将其内容添加到默认主机元素下的上下文描述中(一些在 server.xml 的末尾)。但是 tomcat 将始终在那里查找应用程序(因此在取消部署应用程序时您会收到错误消息),此外,如果您向元素添加新资源(变量不是全局数据源),则必须更新服务器端上下文描述再次。
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.
因此,如果从安全的角度来看这并不重要,那么 GlobalResources 加上在您的 web 应用程序的 context.xml 中重新引用它们是最简单的方法。