-
Notifications
You must be signed in to change notification settings - Fork 74
Description
13.12.2. Injecting an Entity Manager Factory using CDI
A Jakarta EE container must feature built-in integration of Jakarta Persistence with the CDI bean manager, allowing injection of a container-managed entity manager using the annotation jakarta.inject.Inject.
For each persistence unit, the container must make available a bean with:
- bean type EntityManagerFactory, - the qualifiers specified by qualifier XML elements in persistence.xml, or jakarta.enterprise.inject.Default, if no qualifiers are explicitly specified, - scope jakarta.enterprise.context.ApplicationScoped, - bean name given by the name of the persistence unit, - no interceptor bindings, - a bean implementation which satisfies the requirements of the Persistence specification for a container-managed entity manager factory.
Is your feature request related to a problem? Please describe.
EE implementations may create a single CDI BeanManager instance per application deployment to be used globally. EE implementations may also use other mappings such as an CDI BeanManager instance per deployment module which wouldn't get a duplicate bean error when there are duplicate persistence unit definitions.
For reference https://issues.redhat.com/browse/WFLY-19554 contains some background information about problems that can happen with existing application deployments that contain duplicate persistence unit definitions which are currently allowed by the Jakarta EE 11 specification.
In summary, think about an EAR deployment containing two subdeployments that each contain persistence definition:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="mainPu">
<description>Persistence Unit.</description>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
I think that some application servers may try to create two EntityManagerFactory CDI beans named mainPu (once per subdeployment that contains ^ persistence.xml). As is mentioned in https://issues.redhat.com/browse/WFLY-19554, I'm seeing a deployment failure [WELD-001414](https://issues.redhat.com/browse/WELD-1414): Bean name is ambiguous. Name mainPu resolves to beans: [Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default], Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default]]"}}}}
Describe the solution you'd like
Clarify that the EntityManagerFactory bean may not be named when there are duplicate persistence unit definitions in an application.
Describe alternatives you've considered
Specify a persistence unit hint that when used will ensure that the EntityManagerFactory CDI Bean name includes the containing module archive file name (e.g something like CDIPersistenceSchemaManagerTestCase.jar.pu2).
Additional context
Current test failures seen with WildFly:
[INFO] Running org.jboss.as.test.integration.jpa.packaging.PersistenceUnitWarPackagingTestCase
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.483 s <<< FAILURE! – in org.jboss.as.test.integration.jpa.packaging.PersistenceUnitWarPackagingTestCase
[ERROR] org.jboss.as.test.integration.jpa.packaging.PersistenceUnitWarPackagingTestCase – Time elapsed: 5.481 s <<< ERROR!
org.jboss.arquillian.container.spi.client.container.DeploymentException:
Cannot deploy scopedToEar.ear: {"WFLYCTL0062: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-1" => {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"scopedToEar.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: [WELD-001414](https://issues.redhat.com/browse/WELD-1414): Bean name is ambiguous. Name mainPu resolves to beans: [Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default], Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default]]"}}}}
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.623 s <<< FAILURE! – in org.jboss.as.test.integration.jpa.packaging.PersistenceUnitPackagingTestCase
Caused by: org.jboss.weld.exceptions.DeploymentException: [WELD-001414](https://issues.redhat.com/browse/WELD-1414): Bean name is ambiguous. Name mainPu resolves to beans: [Configurator Bean [interface jakarta.persistence.EntityManagerFactory, types: Object, EntityManagerFactory, AutoCloseable, qualifiers: @Any @Default], Configurator Bean [interface