Skip to content

Connecting to Mongo fails with an UnknownHostException when spring.data.mongodb.additional-hosts is configured #36441

@ghost

Description

I have some MongoDB config with Spring boot:

spring:
  data:
    mongodb:
      database: admin
      username: admin
      password: pass
      host: localhost
      additional-hosts: [somehost]

When I start my application, I see the following error in logs:

com.mongodb.MongoSocketException: localhostsomehost
	at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:217) ~[mongodb-driver-core-4.9.1.jar:na]
	at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:78) ~[mongodb-driver-core-4.9.1.jar:na]
	at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:68) ~[mongodb-driver-core-4.9.1.jar:na]
	at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:204) ~[mongodb-driver-core-4.9.1.jar:na]
	at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:199) ~[mongodb-driver-core-4.9.1.jar:na]
	at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:159) ~[mongodb-driver-core-4.9.1.jar:na]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.net.UnknownHostException: localhostsomehost
	at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:801) ~[na:na]
	at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:887) ~[na:na]
	at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1533) ~[na:na]
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1385) ~[na:na]
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1306) ~[na:na]
	at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:209) ~[mongodb-driver-core-4.9.1.jar:na]
	... 6 common frames omitted

I've found that in PropertiesMongoConnectionDetails class connection string (getConnectionString() function) is being built and comma is missing between host value and first additional host value:

		builder.append((this.properties.getHost() != null) ? this.properties.getHost() : "localhost");
		if (this.properties.getPort() != null) {
			builder.append(":");
			builder.append(this.properties.getPort());
		}
		if (this.properties.getAdditionalHosts() != null) {
			builder.append(String.join(",", this.properties.getAdditionalHosts()));
		}

String.join(",", this.properties.getAdditionalHosts()) will insert comma between additional-hosts values, but not at the start

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions