-
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
Background initialization option for JPA EntityManagerFactory / Hibernate SessionFactory [SPR-13732] #18305
Comments
Juergen Hoeller commented Phil Webb, Stéphane Nicoll, Greg Turnquist, Oliver Drotbohm, this occured to me after reading that recent Twitter conversation about concurrent bean bootstrapping in Spring.next. Generalized concurrent bootstrapping of beans is an absolutely non-trivial problem which seems to bring more pain than gain; addressing our known bottlenecks through specific background initialization options might be a fine compromise. This doesn't seem too hard, actually, so I'm scheduling it for 4.3 already, where we have a focus on further performance optimizations anyway. Configuration-wise, that would mean a Juergen |
Juergen Hoeller commented I've got a working arrangement based on a This preserves the Juergen |
Oliver Drotbohm commented That sounds great. I'd love to do some A/B testing on startup times for some of the JPA samples we have flying around as soon as I can get a hand on a snapshot build. |
Juergen Hoeller commented Initial cut committed. Triggered by "bootstrapExecutor" property on the |
Oliver Drotbohm commented I've played a little with this and while it generally works, I didn't see too significant improvements in startup times. I guess the following reasons play into this:
So unless you don't have a lot of beans that are expensive to create and don't on some way or another rely on JPA, or you extensively use lazy initialization in the first place, the performance benefits of that change as is are probably not that significant. Is there a way we can identify LCEMFB and make sure they get instantiated as early as possible? Or find other means of a more coarse grained lazification of the beans that actually trigger the use of an I was thinking about a lazy initialization proxy for repositories that could be added to the factory creating the repository proxies, so that the bean creation still gets triggered (and thus the downstream EMF initialization) but basically acts like a transparent I guess we'd also have to make sure the initialization process is completed before the container throws the |
Juergen Hoeller commented As for 1., that's expected: Initializing the JPA provider in the background will only pay off if it did not eat most of a particular application's startup time to begin with, and/or if actual JPA access happens very late. This new option here is by no means a simple opt-in. Parallel bootstrapping unfortunately needs to be reflected in the application's architecture. As for 2., in a typical Spring JPA application, many beans indirectly depend on the JPA persistence layer, of course. However, that shouldn't be a problem for the initialization order, since the bean factory algorithm will very quickly end up at the LCEMFB when resolving any such bean's dependency graph anyway, without much other work having happened yet. A key difference is whether a receiving bean just stores the EMF proxy reference or immediately uses it... From my perspective, identifying the LCEMFB bean through some special mechanism won't provide much benefit. I'd rather recommend our existing means of ordering: defining the LCEMFB bean (or its containing configuration class) early, or ordering the corresponding configuration class through It is much more important that the dependent beans call the Finally, with respect to enforcing JPA initialization completion at |
Mirek commented Nice in our app where we have 4000 beans and 2000 entities so this feature is important for us because startup time is about 70 seconds. So yesterday spring 4.3 was released.. how to enable this feature, some prop ? |
Stéphane Nicoll commented If you have a Spring Boot app, see this issue. If you are configuring the entity manager yourself, there is a comment above that explains how to configure |
Andrei Ivanov commented Btw, I think it would be nice to mention this in the reference documentation :) |
Daniel Pinyol Laserna commented I agree with Andrei. I'm not sure how this can be enabled. thanks |
Oliver Drotbohm commented I've just merged the fix for DATAJPA-1397 that allows configuring I've also created an example to show the usage of the different modes. Spring Boot support to ease enabling this is on its way, too. |
Juergen Hoeller opened SPR-13732 and commented
Traditionally, the bottleneck in the startup performance of Spring applications is the initialization of expensive resources such as a JPA
EntityManagerFactory
or a HibernateSessionFactory
.As an alternative to generalized parallel initialization of beans, we might be able to achieve some significant gain through specific background initialization options in
LocalContainerEntityManagerFactoryBean
/LocalSessionFactoryBean
/LocalSessionFactoryBuilder
, internally passing the actual build call on to a background thread after configuration validation and in particular dependency resolution happened. The exposedEntityManagerFactory
/SessionFactory
proxy (which we have for other reasons already) could then simply delegate method invocations to aFuture
of the target resource until it is known to be resolved, which is likely not to come before the very end of the startup phase.Issue Links:
Referenced from: commits db1171d, 09cea6e
1 votes, 9 watchers
The text was updated successfully, but these errors were encountered: