【发布时间】:2015-02-11 13:25:03
【问题描述】:
我尝试在
中为我的应用程序配置 jsbc 数据源-tomcat_home-\conf\Catalina\localhost
我的应用是“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>
我在 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>
也许我可以省略我已经在 reportExport.xml 中给出的字段?!
在 Java 中,我尝试获得这样的连接:
((DataSource) (new InitialContext()).lookup("java:comp/env/jdbc/mssql")).getConnection()
在 java 中连接正常,但有 2 个问题。
第一个问题是:如果我在部署应用程序之前将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
...
部署 webapp 后,启动就很好了。 第二个问题是reportExport.xml在取消部署网络应用程序时被删除。
所以我的设置有问题:我希望系统管理员为我的应用程序设置特定的数据源。我喜欢每个 web 应用程序有一个单独的文件的想法,但是第一次设置的异常和取消部署的删除是不好的。 另一方面,在 server.xml 中设置数据源可能很好,但是如何在其中配置“上下文”元素以使数据源仅适用于特定应用程序?
【问题讨论】: