Migrating at startup
The server migrates its database before it builds the SessionFactory. If anything is wrong, it does not start.
The default: a Hibernate setting
With schema-integration on the class path, Hibernate settings switch the migration on. While Hibernate builds the SessionFactory, and before its own validation, the integrator reads the entities and migrates through one of Hibernate's connections.
When the server owns the bootstrap
With JTA or multi-tenancy the integrator refuses to run. Call the migration yourself between building the metadata and building the SessionFactory, with a DataSource whose connections are not enlisted in JTA.
migrate runs synchronously. Any MigrationStatus lets the server continue; a MigrationException must abort the startup. Settings in the registry refine the options you pass.
No migration scripts, no snapshots
Every migration stores its target model as JSON in __hibernate_ddl.schema_history. The next start plans against that model. Because IDs are stable across all versions, a server may skip releases. Without history the previous model is empty, and all tables are created.
- Applied
- Changes were planned and executed; a new revision was written.
- AlreadyApplied
- The database already matches the target; only checked.
- Adopted
- A database without history matched the target and became revision 1.
- ManuallyMigrated
- A target migrated by hand was verified and recorded.
What happens under the lock
Under a transactional advisory lock the executor checks the history, plans the changes, compares the database with the stored model, executes the DDL, checks the target and writes the new revision, all in one transaction.
Hand-made changes stop the start
A database that differs from the stored model is not migrated further.
An older server cannot run
After a newer migration, an older model would rename or drop things back. It is refused.
Other nodes keep working
A start without changes only checks under a shared lock, so running nodes keep reading and writing.
A migration with changes locks its tables exclusively until it commits. Plan large changes for a maintenance window.