0

Similar to What to put into jta-data-source of persistence.xml? and How to map jpa datasources in WildFly?

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.

Community
  • 1
  • 1
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

2 Answers2

0

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:
Configuring a default data source

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

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
0

If you want to use the same JNDI name that works on all servers, it's best to use resource references, as explained in What is resource-ref in web.xml used for?

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 configure a datasource without any additional mapping if you choose a name without a prefix.

Community
  • 1
  • 1
OndroMih
  • 7,280
  • 1
  • 26
  • 44
  • yup that's how I got it to work with EJBs, I had to put in a dummy ` @Resource(name = "java:comp/env/jdbc/jee") private DataSource ds;` to make it refer to it. There's no resource-ref on the EJB-JAR.XML top level like you do in WEB.XML – Archimedes Trajano May 05 '17 at 23:02
  • WebSphere requires the `java:comp/env` version though otherwise it will take it as the final JNDI location. Without it it will not allow you to set the reference though the administrative console. – Archimedes Trajano May 05 '17 at 23:03
  • the `java:comp/env` prefix ought to be valid on any Java EE app server. I believe it's mandated per spec – Andy Guibert May 06 '17 at 16:36
  • Both Payara and Wildfly support the DefaultDataSource using java:comp/DefaultDatasource. In other application servers, you must configure the default datasource. – martosfre Sep 23 '20 at 16:44