-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
Clarify the scope of DataSourceInitializer
#9528
Comments
DataSourceInitializer
on the auto-configured DataSource
DataSourceInitializer
only on the auto-configured DataSource
I did a bit of hacking on this which makes me think that the original statement has to be revisited a bit. Public APIThe current code is highly dependant of This should be mitigated with the fact that our doc invites users to create their custom Also, the invocation of the migration is purely programmatic so any advanced feature where the intialization should happen only once Hibernate has started will have to be managed by the user somehow. EventThe OrderingRight now, the initializer does two separate things:
While our implementation is considered a simple solution (compared to our Liquibase/Flyway support), it's actually much harder to implement. The first link can be easily done by adding a ref to the entity manager (The Liquibase and Flyway auto-configs already do that).The second link is harder though since the same bean already required the opposite order to be able to create the schema. Technicall we should wait for Flyway, Liquibase or Hibernate DDL auto to complete to initialize the schema. And that's where the event we have may have more sense. I am wondering if |
#1115 seems to be in contradiction with my initial ordering analysis. So it looks like everything should happen as soon as the |
More investigation on the code (this one is definitely tricky!) The following cases are supported:
This allowed me to discover a weird case: if you don't have JPA and only an init script (no schema) because something custom is going to create the schema, the initializer doesn't run. It would probably be easier if we looked at things a bit differently:
Perhaps a |
We now have better test coverage for those cases that I've discovered which should facilitate the refactoring. Having discussed with @jhoeller we won't be able to get rid of the BPP dance. But we can get rid of the event and make the intent more clear. Also, for users that do not require things to be created super early rolling their own version should be pretty easy. |
The BPP dance is such that we're considering running the initializer if one To make that intend more clear, we need to move the initialization-related properties out of |
DataSourceInitializer
only on the auto-configured DataSource
DataSourceInitializer
Unfortunately it is not that easy, we do have support for custom username/password for both the creation of the schema and its initialization. When that feature is used, it is assume that we're going to create the exact same I confirm we need to configure a single |
This commit separates the lifecycle of the datasource initialization from DataSourceInitializer itself. It also makes sure that a @primary data source is no longer required. See spring-projectsgh-9528 This leads to cycle in slice tests
This commit separates the lifecycle of the datasource initialization from DataSourceInitializer itself. It also makes sure that a @primary data source is no longer required. See spring-projectsgh-9528
Let's make sure to validate #9394 works now. There is a sample attached to the issue that still breaks and the only way to make it work is to stop with the BPP dance (which isn't going to happen so we may want to go the documentation route for that case). |
I updated the test repo for #9394 with minor changes for spring-boot 2: https://github.com/zachmarshall/spring-datasource-init-bug/tree/spring-boot-2. As you said, it still fails. Please let me know if there's something you'd like for me to validate as working. |
Please, see my comment #9394 (comment) Am I correct that now there is no way to fix it? |
Right now
DataSourceInitializer
runs regardless of the fact that we've auto-configured aDataSource
or a custom one is available. If more than one is available, this currently throws a low-level exception because we expect a primary.We have plans to change the last bit in 2.0 but I think it is confusing that we apply something datasource related on something that we haven't auto-configured. After all things defined in
spring.datasource
(inDataSourceProperties
) should only be taken into account if we configure theDataSource
.The side effect is that if we do this explicitly, we have the guarantee the
DataSource
is initialized and ready to be used once we expose it in the context. Right now we use a BPP for that and it causes hard to track issues such as #9394The purpose of this issue is to:
DataSource
. It should be super easy to grab an instance of such object based onDataSourceProperties
DataSourceConfiguration#createDataSource
that has all that we need to share that logic. We'd also need to updateEmbeddedDataSourceConfiguration
so it would be nice if we could easily migrate aDataSource
based on aDataSource
and aDataSourceProperties
instancesIf someone is willing to initialize a custom
DataSource
then they'll have to do the same thing but at least it will be explicit.The text was updated successfully, but these errors were encountered: