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

Migrations performed by Flyway or Liquibase may not have completed before the database is accessed via jOOQ #25055

Closed
wilkinsona opened this issue Jan 29, 2021 · 7 comments
Labels
status: superseded An issue that has been superseded by another type: bug A general bug

Comments

@wilkinsona
Copy link
Member

If jOOQ is used to interact with a database during application context refresh, migrations performed by Flyway or Liquibase may not have completed. This can happen because there's no guarantee that the Flyway or Liquibase beans will have been created and initialised before any jOOQ beans are created and used. I think we need some AbstractDependsOnBeanFactoryPostProcessor beans so that any jOOQ DSLContext beans depend on the Flyway and Liquibase beans.

@wilkinsona wilkinsona added this to the 2.3.x milestone Jan 29, 2021
@wilkinsona wilkinsona added the type: bug A general bug label Jan 29, 2021
@lukaseder
Copy link
Contributor

Interesting, could this be a more general problem? Would a migration-aware DataSource (or connection pool) be interesting, with a mutex that prevents migrations and ordinary access from happening at the same time?

@wilkinsona
Copy link
Member Author

It is a general problem and we have a (fairly) general solution for it. There's a bug here as we've neglected to use that solution for jOOQ's DSLContext. I noticed this as part of a broader reworking of DataSource initialization for Boot 2.5. Part of that may result in a further generalisation of the current solution so that it's more extensible.

a mutex that prevents migrations and ordinary access from happening at the same time

Other than one scenario involved deferred bootstrapping of JPA, everything is single threaded. We need to ensure that things happen in the right order rather than preventing them from happening in parallel.

@lukaseder
Copy link
Contributor

I see, thanks for the explanation!

eddumelendez added a commit to eddumelendez/spring-boot that referenced this issue Feb 15, 2021
In order to make sure that `Jooq` is used when `flyway` and
`liquibase` beans are created and migrations are done,
`DslContextDependsOnPostProcessor` was added.

See spring-projectsgh-25055
@wilkinsona
Copy link
Member Author

Closing in favour of #25279.

@wilkinsona wilkinsona removed this from the 2.3.x milestone Feb 16, 2021
@wilkinsona wilkinsona added the status: superseded An issue that has been superseded by another label Feb 16, 2021
@neiser
Copy link
Contributor

neiser commented Sep 14, 2022

@wilkinsona I've found this issue while using Spring Boot 2.7.3 probably suffering exactly from the problem that Flyway might not have run during application startup but already using jOOQ's DSLContext within a @Scheduled method. It then fails with an error telling me a database table isn't present, which would have been there if Flyway had run. I'm also using spring.flyway.clean-on-validation-error=true but that shouldn't change much I think.

I've seen this fix here has been forward-ported to 2.4.x, but I can't tell how it's now solved from 2.5.x onwards. Searching org.springframework.boot.autoconfigure.jooq package for keyword flyway and org.springframework.boot.autoconfigure.flyway for keyword jooq makes me wonder how it's ensured that jOOQ's DSLContext bean is only available once Flyway's migration has happened. Can you enlighten me? Or is this a bug which has now re-appeared after the refactoring of DataSources in 2.5.x you've mentioned above?

@wilkinsona
Copy link
Member Author

org.springframework.boot.jooq.JooqDependsOnDatabaseInitializationDetector should ensure that any DSLContext beans depend upon any beans that are responsible for DataSource initialization. Flyway is one such type of bean as identified by org.springframework.boot.flyway.FlywayDatabaseInitializerDetector.

@neiser
Copy link
Contributor

neiser commented Sep 14, 2022

@wilkinsona Thanks a lot for this immediate answer. I'll see if I can reproduce it now that I believe jOOQ is indeed later available than Flyway 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants