
Question:
Similar to <a href="https://stackoverflow.com/questions/4051273/what-to-put-into-jta-data-source-of-persistence-xml" rel="nofollow">What to put into jta-data-source of persistence.xml?</a> and <a href="https://stackoverflow.com/questions/42434282/how-to-map-jpa-datasources-in-wildfly" rel="nofollow">How to map jpa datasources in WildFly?</a>
However, I am asking for something that would work on all vendors or at least WildFly, Glassfish/Payara, WebSphere Application Server classic, WebSphere Application Server Liberty, TomEE. I am not looking for something that works in a Java SE Unit test.
So far I found that java:comp/env/jdbc/xxx
works in WebSphere Application Server and TomEE. There's a mapping exercise (which is expected) to get it working but I cannot get the same to work on GlassFish/Payara and JBoss/WildFly.
More specifically I do not wish to use default data source because for my scenario I am actually working on two different data sources. E.g. for reference data and another for transactional.
Answer1:If all of the app servers you work with are Java EE 7 compliant, you can use the default data source, which is required per EE7 spec to be available at:
java:comp/DefaultDataSource
The app server you run on ought to let you customize the configuration of the DefaultDataSource.
Since I'm familiar with WebSphere Liberty, I can point you to this doc for default data sources on Liberty:<br /><a href="https://www.ibm.com/support/knowledgecenter/SSAW57_liberty/com.ibm.websphere.wlp.zseries.doc/ae/twlp_config_dds.html" rel="nofollow">Configuring a default data source</a>
If you are using WebSphere traditional, as of v9.0 it supports Java EE 7, and has a default data source available out of the box (under the spec mandated JNDI name).
Answer2:If you want to use the same JNDI name that works on all servers, it's best to use resource references, as explained in <a href="https://stackoverflow.com/questions/2887967/what-is-resource-ref-in-web-xml-used-for" rel="nofollow">What is resource-ref in web.xml used for?</a>
Basically, you would define an arbitrary JNDI name (ideally without any java:comp
prefix or similar, just something like "myDatasource") and then map it to the concrete JNDI name provided by the target server.You would need to define a server-specific descriptor for each server with the mapping the if the server cannot use the JNDI directly (e.g. glassfish-web.xml for GlassFish/Payara, jboss-web.xml for WildFly, ibm-web-bnd.xml for WebSphere Classic and Liberty). TomEE seems to support references without any prefix, so it should be able to <a href="https://tomee.apache.org/configuring-datasources.html" rel="nofollow">configure a datasource</a> without any additional mapping if you choose a name without a prefix.