Skip to content

Refactor Database Initialization for early schema initialization#2762

Open
lbsekr wants to merge 5 commits into
JetBrains:mainfrom
lbsekr:main
Open

Refactor Database Initialization for early schema initialization#2762
lbsekr wants to merge 5 commits into
JetBrains:mainfrom
lbsekr:main

Conversation

@lbsekr
Copy link
Copy Markdown

@lbsekr lbsekr commented Mar 18, 2026

Description

Summary of the change: Migrate DatabaseInitializer from ApplicationRunner to a new initialization Point after the Database::connection is done to ensure database schema creation occurs before the application context refresh completes, preventing race conditions with database-dependent beans. Also allowing other non-spring application to apply ddl-auto generation.

Detailed description:

  • Why: The current DatabaseInitializer implementation extends ApplicationRunner, which executes after the Spring application context has fully refreshed. This creates a race condition where beans with database-dependent initialization logic (e.g. @PostConstruct methods, InitializingBean implementations, or repositories that validate schema on startup) attempt to access tables before the schema has been created. By moving initialization earlier in the lifecycle, immediately after Database-Connection instantiation but before it is injected into dependent it is ensured that the schema exists before any database access occurs
  • What:
    • Removed ApplicationRunner implementation
    • Implemented DDL-Initialization after database connection to ensure early execution in springs initialization process
    • Added DDL generation config to the DatabaseConfig ensuring non-spring application can access it as well
  • How:
    • Removed ApplicationRunner implementation: Eliminated the previous ApplicationRunner approach that executed after context
      refresh, replacing it with a mechanism that triggers during the early bean initialization phase
    • Implemented DDL-Initialization after database connection: The schema generation now executes immediately after the Database Connection is instantiated, ensuring early execution in Spring's initialization process before dependent beans (such as JdbcTemplate or repositories) can access the DB. This prevents race conditions where database-dependent components fail during their startup sequence due to missing tables.
    • Added DDL generation config to the DatabaseConfig: Centralized the DDL generation configuration in DatabaseConfig, ensuring that non-Spring applications (e.g., standalone utilities or test contexts) can access and utilize the same schema initialization logic without requiring the full Spring Boot context.

Type of Change

Please mark the relevant options with an "X":

  • Bug fix
  • New feature
  • Documentation update

Updates/remove existing public API methods:

  • Is breaking change

Affected databases:

  • MariaDB
  • Mysql5
  • Mysql8
  • Oracle
  • Postgres
  • SqlServer
  • H2
  • SQLite

Checklist

  • Unit tests are in place
  • The build is green (including the Detekt check)
  • All public methods affected by my PR has up to date API docs
  • Documentation for my change is up to date

Related Issues

Ref

lbsekr added 4 commits March 18, 2026 14:43
ensures database initializing is done before springboot context refresh

ref: EXPOSED-1004
ensures database initializing is done before springboot context refresh

ref: EXPOSED-1004
@bog-walk bog-walk self-assigned this Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants