In a Jakarta EE 8 environment: Is it possible to define a ("portable JNDI") data source at EAR level [1] in application.xml
and use this data source as JTA data source in the persistence.xml
inside a library/JAR module?
Purpose: Creating a common JAR module that defines JPA entities together with corresponding "repositories", so that this JAR module can be used by multiple WAR modules (e. g. a RESTful API and a UI module) and package this modules as an EAR that is deployable to multiple application servers.
With the following attempt/method (for a full example I created a simple git repo [2]), the deployment of such an EAR fails (at least with Payara and WildFly).
Attempt/Method
Let's say there's an application consisting of 2 WAR modules and both WAR modules uses a shared JAR module, so that there is a application structure something like this:
ear/
├── shared-lib-jar
| ├── ...
| └── META-INF
| ├── ...
| └── persistence.xml
├── api-war/
| └── ...
├── ui-war/
| └── ...
├── application.xml
├── ...
In the application.xml
of the EAR the data source is defined like this:
<application>
<!-- ... -->
<data-source>
<name>java:app/appDS</name>
<!-- ... -->
</data-source>
</application>
In the persistence.xml
the JNDI name defined in the application.xml
is used as the JTA data-source:
<persistence>
<persistence-unit name="..." transaction-type="JTA">
<jta-data-source>java:app/appDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!-- ... -->
</persistence-unit>
</persistence>
unexpected/faulty behavior/situation with different (2) application servers
Setup: org.h2.jdbcx.JdbcDataSource
as class-name
and a "file-based" data base
Payara (5.2020.2)
The data base file will not be created and the server log says:
[2020-07-07T22:56:32.731+0200] [Payara 5.2020] [SEVERE] [AS-DEPLOYMENT-00026] [javax.enterprise.system.tools.deployment.dol] [tid: _ThreadID=168 _ThreadName=admin-thread-pool::admin-listener(11)] [timeMillis: 1594155392731] [levelValue: 1000] [[
JNDI lookup failed for the resource: Name: foo-core, Lookup: java:app/appDS, Type: javax.sql.DataSource.]]
[2020-07-07T22:56:32.731+0200] [Payara 5.2020] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=168 _ThreadName=admin-thread-pool::admin-listener(11)] [timeMillis: 1594155392731] [levelValue: 1000] [[
JNDI lookup failed for the resource: Name: [foo-core], Lookup: [java:app/appDS], Type: [javax.sql.DataSource]]]
WildFly (1.4.11.Final)
The data base file is created but the server log says:
{"WFLYCTL0062: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"foo-ear-0.0.1-SNAPSHOT.ear\".WeldStartService" => "Failed to start service
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'foo-core' in deployment foo-ear-0.0.1-SNAPSHOT.ear for injection point protected javax.persistence.EntityManager com.acme.BookRepository.entityManager"}}}}
1: https://jakarta.ee/specifications/platform/8/platform-spec-8.html#a1688