-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Asynchronous EntityManagerFactory bootstrapping to complete on context refresh completion [SPR-17334] #21868
Comments
Juergen Hoeller commented This is meant to be by design: The application and its web endpoints can be up and listening already while the persistence provider is still bootstrapping, only blocking once a request comes in that actually needs to access the persistence provider. That said, I can see the issue with post-bootstrapping logic here. I guess we could try to attach a callback to the async bootstrap thread there, or we could have a mode of bootstrapping where we effectively wait and block at the end of the refresh phase. |
Andy Wilkinson commented Ah, I see. And that will of course work fine if you only access the database via the entity manager. It remains problematic for applications that aren't purely using JPA and are using The area of Boot that led me to investigate this was As things stand, even with our (ab)use of the |
Revisiting this ticket in the backlog, I'm wondering what the current state of affairs is in Boot there. Would a dedicated completion callback for EntityManagerFactory bootstrapping still be useful? Should we consider introducing an option to wait for EntityManagerFactory completion at the end of context refresh, e.g. through joining the underlying Future on In any case, I'd like to close this ticket for good in the 6.1 timeframe. |
Following up on our background initialization option for the core container (#13410) with its strict rules, it seems sensible to enforce completion of asynchronous EMF initialization before context refresh completion (just like we do for Unless there is a compelling case made, I do not intend to let users opt out of this in order to enforce the predictability for JPA/Hibernate initialization specifically. So the behavior will simply be stricter in 6.2, always completing in time for context refresh completion. This enables us to rely on |
Andy Wilkinson opened SPR-17334 and commented
When a
LocalEntityManagerFactory
bean is configured with a bootstrap executor, there's a race between application context refresh and native entity manager factory bootstrapping. This makes things awkward for any logic that wants to run once bootstrapping has completed, for example something that relies on Hibernate DDL processing.I've attached a small example that hopefully illustrates the problem. When imported into your IDE and run,
EmfBootstrappingRaceApplication
should thrown an exception when executing theSELECT
query usingJdbcTemplate
. The failure will not occur iffactory.setBootstrapExecutor(executor);
is commented out. The failure will occur intermittently if the artificial delay introduced by the async executor is removed.Affects: 4.3.19, 5.0.9, 5.1 GA
Reference URL: spring-projects/spring-boot#14658
Attachments:
The text was updated successfully, but these errors were encountered: