microsoft / spring-data-cosmosdb

Access data with Azure Cosmos DB
MIT License
94 stars 64 forks source link

How can i keep multiple connections using different Database Name? #495

Closed cofstea closed 4 years ago

cofstea commented 4 years ago

In my project, on the same URI link,i have multiple cosmosDB names, and i need to create separate CosmosDBconfig for each, extended from AbstractCosmosConfiguration , and override the cosomosTemplate() method, for having an unique bean with specific database name. Problem is that the all executions are using only the Primary CosmosOperations bean and con not reach the others.

As an example: Primary connection:

@Configuration
@EnableCosmosRepositories(basePackageClasses = ARepository.class)
public class ADbConfig extends AbstractCosmosConfiguration {

    @Primary
    @Bean(name = "ACosmosDb")
    public CosmosDBConfig getACosmosDb(
            @Value("${cosmos.host}") String host,
            @Value("${cosmos.key}") String key,
            @Value("${a.cosmos.database}") String database) {
        return CosmosDBConfig.builder(host, key, database).build();
    }

    @Override
    @Primary
    @Bean(name = "ATemplate")
    public CosmosTemplate cosmosTemplate(CosmosDBConfig config) throws ClassNotFoundException {
        return super.cosmosTemplate(config);
    }
}

Second connection:

@Configuration
@EnableCosmosRepositories(basePackageClasses = BRepository.class)
public class BDbConfig extends AbstractCosmosConfiguration {

    @Bean(name = "BCosmosDb")
    public CosmosDBConfig getBCosmosDb(
            @Value("${cosmos.host}") String host,
            @Value("${cosmos.key}") String key,
            @Value("${b.cosmos.database}") String database) {
        return CosmosDBConfig.builder(host, key, database).build();
    }

    @Override
    @Bean(name = "BTemplate")
    public CosmosTemplate cosmosTemplate(@Qualifier("BCosmosDb") CosmosDBConfig config) throws ClassNotFoundException {
        return super.cosmosTemplate(config);
    }
}
kushagraThapar commented 4 years ago

@cofstea We have a backlog item for this that you can track. I have referenced your issue here: https://github.com/microsoft/spring-data-cosmosdb/issues/520

kushagraThapar commented 4 years ago

This is done in new version which will be out soon as azure-spring-data-2-3-cosmos