Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce initialize() method in AbstractRoutingDataSource and AbstractRoutingConnectionFactory #31248

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ public void setDataSourceLookup(@Nullable DataSourceLookup dataSourceLookup) {

@Override
public void afterPropertiesSet() {
initialize();
}

/**
* Synchronizes targetDataSources to resolvedDataSources
* and defaultTargetDataSource to resolvedDefaultDataSource.
* @throws IllegalArgumentException in case of targetDataSources is null
*/
public void initialize() {
if (this.targetDataSources == null) {
throw new IllegalArgumentException("Property 'targetDataSources' is required");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ protected Object determineCurrentLookupKey() {
assertThat(routingDataSource.determineTargetDataSource()).isSameAs(ds);
}

@Test
void testInitialize_synchronizeTargetDataSourcesToResolvedDataSources() {
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
@Override
protected Object determineCurrentLookupKey() {
return null;
}
};

DataSource ds1 = new StubDataSource();
DataSource ds2 = new StubDataSource();

Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put("ds1", ds1);
targetDataSources.put("ds2", ds2);
routingDataSource.setTargetDataSources(targetDataSources);

routingDataSource.initialize();

Map<Object, DataSource> resolvedDataSources = routingDataSource.getResolvedDataSources();
assertThat(resolvedDataSources).hasSize(2);
assertThat(resolvedDataSources.get("ds1")).isSameAs(ds1);
assertThat(resolvedDataSources.get("ds2")).isSameAs(ds2);
}

@Test
public void notInitialized() {
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ public void setConnectionFactoryLookup(ConnectionFactoryLookup connectionFactory

@Override
public void afterPropertiesSet() {
initialize();
}

/**
* Synchronizes targetConnectionFactories to resolvedConnectionFactories
* and defaultTargetConnectionFactory to resolvedDefaultConnectionFactory.
*/
public void initialize() {
Assert.notNull(this.targetConnectionFactories, "Property 'targetConnectionFactories' must not be null");

this.resolvedConnectionFactories = CollectionUtils.newHashMap(this.targetConnectionFactories.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ public void shouldAllowModificationsAfterInitialization() {
.verifyComplete();
}

@Test
void testInitialize_shouldDetermineRoutedFactory() {
connectionFactory.setTargetConnectionFactories(
singletonMap("key", routedConnectionFactory));
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup());
connectionFactory.initialize();

connectionFactory.determineTargetConnectionFactory()
.contextWrite(Context.of(ROUTING_KEY, "key"))
.as(StepVerifier::create)
.expectNext(routedConnectionFactory)
.verifyComplete();
}

static class DummyRoutingConnectionFactory extends AbstractRoutingConnectionFactory {

Expand Down