简体   繁体   中英

how to correctly configure jndi data source in tomcat 8

I try to configure a jsbc data source for my app within

-tomcat_home-\\conf\\Catalina\\localhost

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>

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>

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

in Java i try to get an connection like this:

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

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

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
...

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

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?

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.

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).

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM