Skip to content

RoutingDataSourceHealthContributor should heed Maps with null values in keys #27694

@timmalich

Description

@timmalich

I just upgraded our project from springboot 2.4.1 to 2.5.3. I am super exited, that there is support for AbstractRoutingDataSource now. Thank' and kudos for that!
Due to the update the health check now run's into an NPE which kills the whole application.
It happens because the entryset 'routingDataSource.getResolvedDataSources().entrySet()' contains null as a key.

We create our datasource like this:

@Bean
@Primary
public DataSource dataSource() {
  final RoutingDataSource routingDataSource = new RoutingDataSource();

  final DataSource primaryDataSource = buildDataSource("PrimaryHikariPool", primaryDataSourceProperties());
  final DataSource replicaDataSource = buildDataSource("ReplicaHikariPool", replicaDataSourceProperties());

  final Map<Object, Object> targetDataSources = new HashMap<>();
  // HERE WE PUT IN OUR EVIL null KEY
  targetDataSources.put(null, primaryDataSource);
  targetDataSources.put(RoutingDataSource.Route.REPLICA, replicaDataSource);

  routingDataSource.setTargetDataSources(targetDataSources);
  routingDataSource.setDefaultTargetDataSource(primaryDataSource);

  return routingDataSource;
}

It might be strange or even stupid to put 'null' as a key into a map, but the RoutingDataSourceHealthContributor shouldn't crash because of that anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: supersededAn issue that has been superseded by another

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions