Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityManagerBeanDefinitionRegistrarPostProcessor does not ensure unique SharedEntityManagerCreator beans #2699

Closed
imperatorx opened this issue Nov 11, 2022 · 5 comments
Assignees
Labels
type: bug A general bug

Comments

@imperatorx
Copy link

3.0.0-RC2 (and 1 too)

Given a trivial config:

<dependencies>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
	<groupId>org.postgresql</groupId>
	<artifactId>postgresql</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
</dependencies>

And datasource configured:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password=postgres

And a @Component @Autowire-ing an EntityManager, the application fails to start when compiled with mvnw -Pnative native:compile, but starts normally when started in java mode from the IDE:

No qualifying bean of type 'jakarta.persistence.EntityManager' available: more than one 'primary' bean found among candidates: [org.springframework.orm.jpa.SharedEntityManagerCreator#0, org.springframework.orm.jpa.SharedEntityManagerCreator#1]
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 11, 2022
@snicoll
Copy link
Member

snicoll commented Nov 11, 2022

@imperatorx thanks for the report but can you please share a small sample we can run ourselves? We do have smoke test for such trivial config and they are green.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Nov 11, 2022
@wilkinsona
Copy link
Member

It's the injection of an EntityManager that's key and our smoke tests don't do that. The problem can be reproduced by making this change to the data:data-jpa smoke test:

diff --git a/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java b/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java
index 8b8ac80..612a374 100644
--- a/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java
+++ b/data/data-jpa/src/main/java/com/example/data/jpa/CLR.java
@@ -5,6 +5,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
+import jakarta.persistence.EntityManager;
 import com.example.data.jpa.model.Author;
 import com.example.data.jpa.model.Book;
 
@@ -19,7 +20,7 @@ class CLR implements CommandLineRunner {
 
        private final BookRepository bookRepository;
 
-       CLR(AuthorRepository authorRepository, BookRepository bookRepository) {
+       CLR(AuthorRepository authorRepository, BookRepository bookRepository, EntityManager entityManager) {
                this.authorRepository = authorRepository;
                this.bookRepository = bookRepository;
        }

@wilkinsona wilkinsona removed the status: waiting-for-feedback We need additional information before we can continue label Nov 11, 2022
@imperatorx
Copy link
Author

@imperatorx thanks for the report but can you please share a small sample we can run ourselves? We do have smoke test for such trivial config and they are green.

Sure here it is: https://github.com/imperatorx/reproducer-entitymanager

@snicoll snicoll transferred this issue from spring-projects/spring-boot Nov 11, 2022
@snicoll snicoll added type: bug A general bug theme: aot and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 11, 2022
@snicoll snicoll self-assigned this Nov 11, 2022
@snicoll snicoll changed the title AOT: Dual EntityManager creation causes startup failure in trivial app AOT-process contexts expose two EntityManager beans Nov 11, 2022
@snicoll snicoll changed the title AOT-process contexts expose two EntityManager beans AOT-processed contexts expose two EntityManager beans Nov 11, 2022
@snicoll snicoll transferred this issue from spring-projects/spring-framework Nov 14, 2022
@snicoll snicoll removed their assignment Nov 14, 2022
@snicoll
Copy link
Member

snicoll commented Nov 14, 2022

It looks like SharedEntityManagerCreator is created at build-time and taken into account by AOT, and SD JPA is creating it again at runtime. It should rather check if it's there and skip it.

@mp911de mp911de self-assigned this Nov 14, 2022
@mp911de mp911de changed the title AOT-processed contexts expose two EntityManager beans EntityManagerBeanDefinitionRegistrarPostProcessor does not ensure unique SharedEntityManagerCreator beans Nov 14, 2022
@mp911de
Copy link
Member

mp911de commented Nov 14, 2022

The underlying problem is that multiple runs of EntityManagerBeanDefinitionRegistrarPostProcessor result in registering multiple auto-wire candidates of EntityManager via SharedEntityManagerCreator. We should consider unique bean names to ensure that we end up with a single EntityManager for each EntityManagerFactory bean even when running EntityManagerBeanDefinitionRegistrarPostProcessor multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants