EntityManagerFactory startup failure in deferred mode is only available on first use
See original GitHub issueHello everyone! Recently I observed a strange behavior while starting a simple project with Spring-boot + Hibernate that confuse me a lot. I wrote a simple JPA entity and tried to start the project, but no entity wrote to a database. After long time searching why I accidentally had seen that the app after around 1-2 minute of correct working shut down with exit code 0 and the answer of my question appeared in a console: nested exception is org.hibernate.MappingException: Could not instantiate id generator [entity-name=null] But why the app keeps silent before that moment and doesn`t show me any information about entity creation process? I think this is not trivial and confusing.
My application.properties for reproduction
spring.session.store-type=jdbc
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.datasource.url= jdbc:postgresql://localhost:5432/***
spring.datasource.username=***
spring.datasource.password=***
spring.jpa.properties.hibernate.default_schema=demo
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
And a simple JPA entity with error:
@Entity
public class Item {
@Id
public Long id;
@ElementCollection
@CollectionTable(name = "IMAGE")
@Column(name = "FILENAME")
@CollectionId(
columns = @Column(name = "IMAGE_ID"),
type = @Type(type = "long"),
generator = "SEQUENCE" <<<<< NOT CORRECT
)
protected List<String> images = new ArrayList<>();
}
Now if you try to start a project then you will not see any error message for almost a one or two minutes and you doesn`t get what the problem. When you fix the error the entity writes to DB without problems
Srping-boot version: v2.3.5.RELEASE
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
@DredderGun this sample app doesn’t interact with the
EntityManagerFactory
and that doesn’t sound very realistic. On first use you’d get the exception as I’ve indicated in my previous comment.That said, there is a case to log this sooner. I am now going to transfer this issue to the Spring Framework issue tracker as there’s nothing we can do in Spring Boot about this. Thanks for the report.
So this isn’t a deadlock as I suspected initially. If you’re not using deferred mode for JPA, the JPA instructor is initialised in the main thread and leads to the following:
It seems that when using deferred mode, the bean creating fails in the background and is not reported to the main thread. @jhoeller, does that ring a bell.
It is possible to make this application fail by injecting the
EntityManager
anywhere, for instance: