【发布时间】:2020-10-28 05:52:08
【问题描述】:
在 Jakarta EE 8 环境中:是否可以在 application.xml 中定义 EAR 级别 [1] 的(“可移植 JNDI”)数据源,并将此数据源用作 persistence.xml 中的 JTA 数据源在库/JAR 模块中?
目的:创建一个通用的 JAR 模块,定义 JPA 实体以及相应的“存储库”,以便这个 JAR 模块可以被多个 WAR 模块(例如一个 RESTful API 和一个 UI 模块)使用,并将这个模块打包为一个 EAR可部署到多个应用服务器。
使用以下尝试/方法(对于一个完整的示例,我创建了一个简单的 git 存储库 [2]),这种 EAR 的部署失败(至少对于 Payara 和 WildFly)。
尝试/方法
假设有一个由 2 个 WAR 模块组成的应用程序,并且两个 WAR 模块都使用一个共享的 JAR 模块,因此应用程序结构类似于:
ear/
├── shared-lib-jar
| ├── ...
| └── META-INF
| ├── ...
| └── persistence.xml
├── api-war/
| └── ...
├── ui-war/
| └── ...
├── application.xml
├── ...
在 EAR 的 application.xml 中,数据源的定义如下:
<application>
<!-- ... -->
<data-source>
<name>java:app/appDS</name>
<!-- ... -->
</data-source>
</application>
在persistence.xml 中,application.xml 中定义的 JNDI 名称用作 JTA 数据源:
<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>
不同 (2) 应用服务器的意外/错误行为/情况
设置:org.h2.jdbcx.JdbcDataSource 为 class-name 和“基于文件”的数据库
Payara (5.2020.2)
不会创建数据库文件,服务器日志显示:
[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)
数据库文件已创建,但服务器日志显示:
{"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
【问题讨论】:
标签: jpa jakarta-ee wildfly ear payara