Skip to content

DatabasePopulatorConfigUtils should only set a populator if matching scripts are defined #23405

@MateuszStefek

Description

@MateuszStefek

In my project, we define database initialization scripts via jdbc:initialize-database element.

        <jdbc:initialize-database data-source="dataSource">
            <jdbc:script location="classpath:db/schema.mysql.sql"/>
            <jdbc:script location="classpath:db/data.sql"/>
        </jdbc:initialize-database>

There are no cleanup scripts (no entries with execution="DESTROY"), yet the DataSourceInitializer bean tries to open a database connection when the context is shutting down. This slows down the shutdown process, especially when the connection times out.

The source code of DataSourceInitializer contains a null check on this.databaseCleaner:

	@Override
	public void destroy() {
		execute(this.databaseCleaner);
	}

	private void execute(DatabasePopulator populator) {
		Assert.state(this.dataSource != null, "DataSource must be set");
		if (this.enabled && populator != null) {
			DatabasePopulatorUtils.execute(populator, this.dataSource);
		}
	}

However, DatabasePopulatorConfigUtils.setDatabasePopulator always creates a non-null value for databaseCleaner, even if the object is effectively a no-op.

The problem happens in spring-jdbc 4.3.14 and 5.1.7

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions