Quarkus: Named mongo client injection trying to use default mongo client

Created on 20 Aug 2020  路  11Comments  路  Source: quarkusio/quarkus

Describe the bug
When using the named mongo client injection, quarkus keeps trying to instantiate both the named client and the default mongo client. As a result, the default mongo client is getting the default connection string from quarkus.mongodb.connection-string which is always localhost:27017. Setting this property should not be needed, as it should not be used. Only the named mongo client connection string should be set in the application.properties file (e.g. quarkus.mongodb.fruits.connection-string)

Expected behavior
Setting only named mongo clients should only need the appropriate quarkus.mongodb.XXX.connection-string properties and quarkus.mongodb.connection-string must not be set at all. Moreover, when using the named mongo clients, the correct named connection must be used and NOT the default one (quarkus.mongodb.connection-string).

Actual behavior
When using only named mongo clients, quarkus is trying to instantiate the named mongo clients (as expected) AND the default one (not expected). Moreover, when for example an index is created upon @Startup with an injected named mongo client, quarkus picks the default client and not the named client as expected.

To Reproduce
Steps to reproduce the behavior:

  1. clone https://github.com/agelbess/quarkus-mongodb-quickstart.git (contains the mongodb quickstart with a few changes)
  2. fire up mongodb on 27017
  3. change the properties of the application.properties file. Try to change the ports to valid and invali ones and see what happens in the logs
# default connection is always instantiated, why?
quarkus.mongodb.connection-string = mongodb://localhost:27017
# in FruitService, when creating the index, the default connection is used
quarkus.mongodb.fruits.connection-string = mongodb://localhost:27018

Configuration

# Configuration file
# default connection is always instantiated, why?
quarkus.mongodb.connection-string = mongodb://localhost:27017
# in FruitService, when creating the index, the default connection is used
quarkus.mongodb.fruits.connection-string = mongodb://localhost:27018
quarkus.mongodb.reactivefruits.connection-string = mongodb://localhost:27017
quarkus.mongodb.codecfruits.connection-string = mongodb://localhost:27017

Screenshots
(If applicable, add screenshots to help explain your problem.)

Environment (please complete the following information):

  • Output of uname -a or ver:
  • Output of java -version: 11
  • GraalVM version (if different from Java):
  • Quarkus version or git rev: 1.7
  • Build tool (ie. output of mvnw --version or gradlew --version): mvn

Additional context
This behaviour was not happening on quarkus v.1.4.

arearc aremongodb kinbug

All 11 comments

/cc @mkouba, @manovotn
/cc @loicmathieu, @evanchooly

/cc @geoand maybe the latests changes on the way MongoDB clients are created cause this issue ?

I'll take a look sometime this week - I just got back from PTO and I have a ton of catching up to do :)

https://github.com/quarkusio/quarkus/pull/11568 addressed part of the issue, but we will need @mkouba for the other part.

Martin, it seems like this code (from https://github.com/agelbess/quarkus-mongodb-quickstart/blob/master/src/main/java/org/acme/mongodb/FruitService.java):

@ApplicationScoped
@Startup
public class FruitService {

    private static final Logger log = LoggerFactory.getLogger(FruitService.class);

    @Inject
    @MongoClientName("fruits")
    MongoClient mongoClient;

    @PostConstruct
    void init() {
        log.info("creating index");
        MongoCollection<Document> collection = mongoClient.getDatabase("fruits").getCollection("fruit");
        collection.createIndex(new Document("t", 1));
        log.info("created index");
    }

    public List<Fruit> list() {
...        
    }

    public void add(Fruit fruit) {
...
    }

    private MongoCollection getCollection() {
...
    }
}

is not injecting the proper MongoClient. It should inject the qualified (@MongoClientName("fruits")) MongoClient, but it's injecting the unqualified MongoClient.

@geoand It seems that @MongoClientName is not properly registered as a qualifier. The MongoClientProcessor only registers it as a bean defining annotation which is a different concept and should not be necessary in this use case. A qualifier that is not included in the bean archive index should be registered via AdditionalBeanBuildItem.

Note that io.quarkus.mongodb.DefaultAndNamedMongoClientConfigTest does not seem to test the injected clients properly.

@geoand It seems that @MongoClientName is not properly registered as a qualifier. The MongoClientProcessor only registers it as a bean defining annotation which is a different concept and should not be necessary in this use case. A qualifier that is not included in the bean archive index should be registered via AdditionalBeanBuildItem.

Ah, great catch! I'll fix it.

Note that io.quarkus.mongodb.DefaultAndNamedMongoClientConfigTest does not seem to test the injected clients properly.

WDYM?

WDYM

@geoand This merely tests the database is there but not if it's the "correct" instance: https://github.com/quarkusio/quarkus/blob/master/extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/DefaultAndNamedMongoClientConfigTest.java#L47-L48

In other words, the test does not fail even if both injected clients are referencing the same object ;-).

Right, I'll take care of it.

Thanks for your help!

@mkouba fixed! Mind taking a super quick look (although I am pretty certain it's OK now)?

@geoand it should be OK now ;-)

:+1:

Was this page helpful?
0 / 5 - 0 ratings