Repository navigation
Shut down embedded database when cleaner fails - #37523
Open
junhyeong9812 wants to merge 1 commit into
Open
junhyeong9812 wants to merge 1 commit into
junhyeong9812 wants to merge 1 commit into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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>.ResourceDatabasePopulatorfails on the first error by default (continueOnErrorandignoreFailedDropsarefalse), so a DESTROY script that drops a table that no longer exists, or a script that cannot be read, makesdestroy()throw before reachingshutdownDatabase():DisposableBeanAdapteronly logs the exception, so the context closes normally. The in-memory database is not dropped though: the default HSQL database lives untilSHUTDOWNis executed, and H2 databases are created withDB_CLOSE_DELAY=-1. Since the database name defaults totestdb, 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 callsshutdownDatabase()in afinallyblock. 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()throwsScriptStatementFailedException, 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.