Skip to content

Shut down embedded database when cleaner fails - #37523

Open
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/embedded-database-destroy-shutdown
Open

junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/embedded-database-destroy-shutdown

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

EmbeddedDatabaseFactoryBean#destroy() runs the configured database cleaner and then shuts the embedded database down. If the cleaner throws, the shutdown is skipped and the in-memory database stays alive for the rest of the JVM. This PR makes sure the database is shut down even if the cleaner fails, while still propagating the cleaner exception.

Problem

The cleaner is typically configured through <jdbc:script execution="DESTROY"/> in <jdbc:embedded-database>. ResourceDatabasePopulator fails on the first error by default (continueOnError and ignoreFailedDrops are false), so a DESTROY script that drops a table that no longer exists, or a script that cannot be read, makes destroy() throw before reaching shutdownDatabase():

public void destroy() {
	DatabasePopulator cleaner = this.databaseCleaner;
	if (cleaner != null && getDataSource() != null) {
		DatabasePopulatorUtils.execute(cleaner, getDataSource());  // throws
	}
	shutdownDatabase();  // skipped
}

DisposableBeanAdapter only logs the exception, so the context closes normally. The in-memory database is not dropped though: the default HSQL database lives until SHUTDOWN is executed, and H2 databases are created with DB_CLOSE_DELAY=-1. Since the database name defaults to testdb, a subsequent context in the same JVM connects to the stale database and sees the previous schema and data, e.g. its own init script fails with "table already exists".

EmbeddedDatabaseFactory#initDatabase() already handles the equivalent case on startup by shutting the database down when the populator fails.

Fix

destroy() now calls shutdownDatabase() in a finally block. The cleaner exception is not caught, so it still propagates to the container as before, and the behavior is unchanged when the cleaner succeeds.

A new test configures a cleaner that drops a missing table, verifies that destroy() throws ScriptStatementFailedException, and then opens a new database with the same name and asserts that the table created by the populator is gone. It fails before the change and passes after it. The full spring-jdbc test suite passes.

EmbeddedDatabaseFactoryBean#destroy() runs the configured database
cleaner and then shuts the database down. If the cleaner throws, for
example because a DESTROY script drops a table that no longer exists,
the shutdown is skipped. The container only logs the exception, and the
in-memory database survives for the lifetime of the JVM: the default
HSQL database lives until SHUTDOWN is executed, and H2 databases are
created with DB_CLOSE_DELAY=-1. A subsequent context using the same
database name then connects to the stale database.

This commit makes sure the database is shut down even if the cleaner
fails, consistent with initDatabase() which shuts the database down
when the populator fails. The cleaner exception is still propagated.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants