我尝试本地主机如何正确配置JNDI数据源的tomcat 8
内
-tomcat_home- \的conf \卡塔利娜\配置江苏广电总台的数据源,我的应用我的应用程序“ 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 ...
部署web应用程序的启动是罚款后。 第二个问题是reportExport.xml在卸载Web应用程序时被删除。
因此,我的设置有些问题:我希望系统管理员为我的应用程序设置特定的数据源。我喜欢这个想法,每个网络应用都有一个单独的文件,但第一次安装时的例外情况和undeploying上的删除情况都很糟糕。 另一方面,在server.xml中设置数据源可能是好的,但是如何在那里配置“context”元素来使数据源仅适用于特定的应用程序?
目前,我的web.xml中有“resource-ref”,与resource.xml中的resource.xml相比,这有什么好处呢?由于server.xml中没有Context元素,我应该添加它还是应该只添加资源? – dermoritz 2015-02-12 09:05:39
据我所知你需要在你的上下文元素中的资源链接,web.xml中的资源链接用于文档目的(或者thye不被java:comp/env引用,因为它们不是环境)。但我可能是错的,因为我没有玩过很多东西。 从someting开始工作,然后你可以调整它。 a)在全局资源中声明DataSource,b)将context.xml添加到war文件中,c)在context.xml中描述resourcelink。如果它起作用,则将上下文描述移动到server.xml的主机元素,如果你不想在你的战争中使用上下文文件(但是为什么你不会在那里?) – Zielu 2015-02-12 14:50:28