Skip to content

AbstractRoutingDataSource should only use defaultTargetDataSource on key null and should fail-fast on a non-existing key [SPR-6809] #11475

@spring-projects-issues

Description

@spring-projects-issues

Geoffrey De Smet opened SPR-6809 and commented

We got 2 dataSources:

  • "productionDB" (also set as defaultTargetDataSource)
  • "testDB"

Here is what we expected to return if we ask determineTargetDataSource:

  • "productionDB" => the production DB
  • "testDB" => the test DB
  • null => the production DB
  • "testDb" => Exception, but instead we got the production DB
  • "nonsence" => Exception, but instead we got the production DB

I think this code:

protected DataSource determineTargetDataSource() {
     Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
     Object lookupKey = determineCurrentLookupKey();
     DataSource dataSource = (DataSource) this.resolvedDataSources.get(lookupKey);
     if (dataSource == null) {
          dataSource = this.resolvedDefaultDataSource;
     }
     if (dataSource == null) {
          throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
     }
     return dataSource;
}

should change into this code:

protected DataSource determineTargetDataSource() {
     Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
     Object lookupKey = determineCurrentLookupKey();
     DataSource dataSource;
     if (lookupKey == null) {
         dataSource = this.resolvedDefaultDataSource;
     } else {
         dataSource = (DataSource) this.resolvedDataSources.get(lookupKey);
         if (dataSource == null) {
             throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
         }
     }
     return dataSource;
}

Affects: 2.5.5

Referenced from: commits aafe8ef

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