{"record":{"id":"d470ffd636d1e142","repo":"apereo/cas","slug":"unable-to-build-a-mongodb-client-without-any-hosts","errorCode":null,"errorMessage":"Unable to build a MongoDb client without any hosts/servers defined","messagePattern":"Unable to build a MongoDb client without any hosts/servers defined","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"support/cas-server-support-mongo-core/src/main/java/org/apereo/cas/mongo/MongoDbConnectionFactory.java","lineNumber":208,"sourceCode":"    }\n\n\n    /**\n     * Build mongo db client.\n     *\n     * @param mongo the mongo\n     * @return the mongo client\n     */\n    public MongoClient buildMongoDbClient(final BaseMongoDbProperties mongo) {\n        val settingsBuilder = MongoClientSettings.builder();\n\n        if (StringUtils.isNotBlank(mongo.getClientUri())) {\n            LOGGER.debug(\"Using MongoDb client URI [{}] to connect to MongoDb instance\", mongo.getClientUri());\n            settingsBuilder.applyConnectionString(new ConnectionString(mongo.getClientUri()));\n        } else {\n            val serverAddresses = mongo.getHost().split(\",\");\n            if (serverAddresses.length == 0) {\n                throw new BeanCreationException(\"Unable to build a MongoDb client without any hosts/servers defined\");\n            }\n            val servers = new ArrayList<ServerAddress>();\n            if (serverAddresses.length > 1) {\n                LOGGER.debug(\"Multiple MongoDb server addresses are defined. Ignoring port [{}], \"\n                             + \"assuming ports are defined as part of the address\", mongo.getPort());\n                Arrays.stream(serverAddresses)\n                    .filter(StringUtils::isNotBlank)\n                    .map(ServerAddress::new)\n                    .forEach(servers::add);\n            } else {\n                val port = mongo.getPort() > 0 ? mongo.getPort() : DEFAULT_PORT;\n                LOGGER.debug(\"Found single MongoDb server address [{}] using port [{}]\", mongo.getHost(), port);\n                val addr = new ServerAddress(mongo.getHost(), port);\n                servers.add(addr);\n            }\n            settingsBuilder.applyToClusterSettings(builder -> {\n                builder.hosts(servers);\n                if (StringUtils.isNotBlank(mongo.getReplicaSet())) {","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/apereo/cas/blob/e7288fc434b4f4505b8452e1a57e8fb3111bb863/support/cas-server-support-mongo-core/src/main/java/org/apereo/cas/mongo/MongoDbConnectionFactory.java#L190-L226","documentation":"MongoDbConnectionFactory builds a Mongo client settings object. If no clientUri is set, it falls back to splitting the `host` property on commas; if that yields no usable hosts it cannot construct any connection target, so it throws BeanCreationException during Mongo client bean creation (via mongoDbFactory).","triggerScenarios":"Configuring a Mongo-backed feature (tickets, services, authn) with cas.mongo.client-uri unset and cas.mongo.host empty/blank; buildMongoDbClient then produces a zero-length host array and fails.","commonSituations":"Forgetting to set host/uri for Mongo modules; YAML indentation mistakes leaving the host key empty; env placeholder ${MONGO_HOST} resolving to nothing; setting only the port while host is missing.","solutions":["Set cas.mongo.host, e.g. cas.mongo.host=localhost (comma-separate replicas: host1:27017,host2:27017)","Or set a full connection string: cas.mongo.client-uri=mongodb://user:pass@host1:27017,host2:27017/db?replicaSet=rs0","Verify the property source actually resolves (check env vars/profiles) so host is not blank at runtime"],"exampleFix":"// before\ncas.mongo.host=\ncas.mongo.port=27017\n\n// after\ncas.mongo.host=localhost:27017\n# or\ncas.mongo.client-uri=mongodb://localhost:27017/cas","handlingStrategy":"validation","validationCode":"if (StringUtils.isBlank(mongo.getClientUri()) && StringUtils.isBlank(mongo.getHost())) {\n    throw new IllegalStateException(\"Set cas.mongo.client-uri or cas.mongo.host before enabling Mongo modules\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    client = MongoDbConnectionFactory.buildMongoDbClient(mongo);\n} catch (BeanCreationException e) {\n    logger.error(\"Mongo client construction failed: {}\", e.getMessage());\n    throw e;\n}","preventionTips":["Prefer a full client-uri for replica sets; it encodes hosts, auth and options in one place","Check that env placeholders like ${MONGO_HOST} resolve in the deployed environment","Validate config at boot with a connectivity ping before modules depend on Mongo"],"tags":["mongodb","configuration","startup"],"backgroundTag":"missing-required-config-field","analyzedSha":"e7288fc434b4f4505b8452e1a57e8fb3111bb863","analyzedAt":"2026-09-08T15:39:16.015Z","contentChangedAt":"2026-09-08T15:39:16.015Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}