Search code examples
webspheredatasourcejndiwildfly-10

Which JNDI datasource name works both on Wildfly and Websphere?


I've a java/spring web app that needs to be deployed as war file both on Wildfly and Websphere

The app is using datasource with JNDI names:

The WebConfig.java contains:


    public DataSource dataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        DataSource dataSource = dsLookup.getDataSource("jdbc/myDS");

        return dataSource;
    }

... and run perfectly on Websphere where JNDI datasource name is jdbc/myDS.

but Wildfly JNDI name has to start with 'java:/' or 'java:jboss/'

Changing WebConfig.java does the work:

DataSource dataSource = dsLookup.getDataSource("java:/myDS");

Which JNDI datasource name works both on Wildfly and Websphere (and maybe on others application servers?)


Solution

  • If you use resource references to do the lookup they would be relative to java:comp/env in both Liberty and Wildfly.

    There are two ways to define a datasource. One is using the javax.annotation.Resource annotation. This can be used on a type, method, or field definition.

    You can also do it in the web.xml or ejb-jar.xml using a resource-ref element:

    <resource-ref>
        <description />
    
        <res-ref-name>myRef</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    

    If you are providing the userid/password in the application code then the res-auth element should contain Application