[英]Persistence unit not found in wildfly module
My scenario is as follows: I have a bunch of different applications (WARs) running on a WildFly server. 我的情况如下:我在WildFly服务器上运行着许多不同的应用程序(WAR)。 One common functionality of each of them is, that they should look up something in a database and behave different, if a specific flag is set (kind of an advanced "kill switch" with some finer-grained configurability).
它们每个的一个共同功能是,如果设置了一个特定的标志(一种高级的“ kill switch”,具有一些更细的可配置性),则它们应该在数据库中查找某些东西并且表现不同。
The whole thing is realized as a filter, called on every request (with some caching to avoid querying the database too often). 整个过程都实现为一个过滤器,该过滤器在每个请求上都进行调用(使用一些缓存以避免过多地查询数据库)。 This is intended and works like it should, if I integrate the filter logic (let's call it
killswitch.jar
) in every single war application (and configure the web.xml
accordingly). 如果我在每个战争应用程序中集成了过滤器逻辑(我们称其为
killswitch.jar
)(并相应地配置了web.xml
),则这是预期的,并且应按预期进行。
From a perspective of maintainability and redundancy avoidance, I find this a little dissatisfactory. 从可维护性和避免冗余的角度来看,我对此不太满意。 Therefore I'd like to put the whole thing into a WildFly module in order to avoid bundling the jar in every war.
因此,我想将整个内容放入WildFly模块中,以避免在每次战争中都将jar捆绑在一起。 I put the jar and the
module.xml
(as follows) in the right place and the module gets loaded. 我将jar和
module.xml
(如下所示)放在正确的位置,并加载了模块。
module.xml
: module.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="my.company.domain.killswitch">
<resources>
<resource-root path="killswitch.jar"/>
</resources>
<dependencies>
<module name="org.jboss.logging"/>
<module name="javax.api"/>
<module name="com.microsoft.sqlserver"/>
<module name="org.slf4j.jcl-over-slf4j"/>
<module name="org.jboss.as.web" />
<module name="javax.servlet.api" />
<module name="javax.persistence.api" />
<module name="javax.api"/>
<module name="javax.inject.api"/>
<module name="org.hibernate" services="import"/>
</dependencies>
</module>
(In fact, this is not the whole truth: This module is loaded in hierarchy of modules. Module A depends on module B and module B depends on this one. But as I see it, this works and all three of them get loaded, so IMHO this shouldn't matter.) (实际上,这不是全部的事实:该模块按模块的层次结构加载。模块A依赖于模块B,模块B依赖于此模块。但是正如我所看到的,此方法有效,并且全部三个都被加载,所以恕我直言,这没关系。)
The problem is, that the persistence unit defined in the module is not found on server startup: 问题是,在服务器启动时找不到模块中定义的持久性单元:
Can't find a persistence unit named 'Killswitch' in deployment MyAwesomeApplication.war
But, as you can imagine, the persistence unit is there. 但是,您可以想象,持久性单元在那里。 The
persistence.xml
also, of course, and it is well-formed and valid. 当然,
persistence.xml
也是格式正确且有效的。 I can definitely confirm this, as it works without any changes, if used not as a module. 我绝对可以确认这一点,因为如果不用作模块,它可以进行任何更改。
I've read a couple of threads about similar issues. 我已经阅读了一些有关类似问题的主题。 Some state, it isn't possible to achieve, what I try to.
有些状态,我无法实现。 Others talk about
META-INF
, ClassLoaders or the order of the modules being loaded. 其他人谈论
META-INF
,ClassLoaders或要加载的模块的顺序。 I tried some of the solutions, but with no success. 我尝试了一些解决方案,但没有成功。 As I'm not a wildfly or JAP professional, I'm at a loss now and asking for your kind help:
由于我不是一名野外专家或JAP专业人员,因此我茫然不知所措,要求您的帮助:
killswitch.jar
in every war? killswitch.jar
吗? Many thanks in advance! 提前谢谢了!
Although my scenario is different from your, i had the same issue with Wildfly not finding the persistence unit, even it was there. 尽管我的情况与您的情况有所不同,但我也遇到了同样的问题,即使它在那里,Wildfly也找不到持久性单元。
After hours of struggling and searching, i've solved my issue simply putting my persistence.xml in the META-INF folder of one of the JARs that my WAR depends on. 经过数小时的努力和搜索,我已经解决了我的问题,只需将我的persistence.xml放入WAR依赖的一个JAR的META-INF文件夹中即可。
You can also build a single lightweight jar that contains solely the META-INF with the persistence.xml file, or even better add it in one of the JARs that your WAR depends on. 您还可以构建一个仅包含带有persistence.xml文件的META-INF的轻量级jar,或者甚至更好地将其添加到WAR依赖的一个JAR中。
The official documentation of Wildfly states this: Wildfly的官方文档指出:
The persistence.xml contains the persistence unit configuration (eg datasource name) and as described in the JPA 2.0 spec (section 8.2), the jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.
persistence.xml包含持久性单元配置(例如,数据源名称),并且如JPA 2.0规范(第8.2节)中所述,其META-INF目录包含persistence.xml文件的jar文件或目录称为持久性根目录。单元。 In Java EE environments, the root of a persistence unit must be one of the following (quoted directly from the JPA 2.0 specification):
在Java EE环境中,持久性单元的根必须是以下之一(直接从JPA 2.0规范中引用):
"
“
- an EJB-JAR file
EJB-JAR文件
- the WEB-INF/classes directory of a WAR file
WAR文件的WEB-INF / classes目录
- a jar file in the WEB-INF/lib directory of a WAR file
WAR文件的WEB-INF / lib目录中的jar文件
- a jar file in the EAR library directory
EAR库目录中的jar文件
- an application client jar file
应用程序客户端jar文件
"
“
Note that, in many answers that i've found, the second option seems to be not working. 请注意,在我找到的许多答案中,第二个选项似乎不起作用。
Hope it can help you and others with the same issue 希望它可以帮助您和其他人解决同样的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.