I am trying to connect cluster in Mongo Atlas.
The access permission is already given to the cluster for all IP's in MongoDB Atlas.
The application I have is SpringBoot application.
- Java-11
- SpringBoot - 2.5.5
- MongoDB 6+
The expcetion while making the connection is:
Timed out after 30000 ms while waiting to connect. Client view of cluster state is
{type=UNKNOWN, servers=[{address=CusterName:27017, type=UNKNOWN, state=CONNECTING,
exception={com.mongodb.MongoSocketException: CusterName}, caused by {java.net.UnknownHostException: CusterName}}];
nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect.
Client view of cluster state is {type=UNKNOWN, servers=[{address=CusterName:27017, type=UNKNOWN, state=CONNECTING,
exception={com.mongodb.MongoSocketException: CusterName}, caused by {java.net.UnknownHostException: CusterName}}]
The java class which I am using to create a pool is:
import com.mongodb.*;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.connection.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Configuration
public class MongoConnectionPool {
@Bean
@Autowired
public MongoDatabaseFactory mongoFactory() {
List<ServerAddress> serverAddresses = new ArrayList<>();
String address = "<clusterName>:<port>"
String[] hostAndPort = address.split(":");
String host = hostAndPort[0];
Integer port = Integer.parseInt(hostAndPort[1]);
ServerAddress serverAddress = new ServerAddress(host, port);
serverAddresses.add(serverAddress);
ConnectionString connUrl = new ConnectionString("mongodb+srv://<username>:<password>@<clusterName>/<databaseName>");
ConnectionPoolSettings poolSettings = ConnectionPoolSettings.builder()
.maxWaitTime(100, TimeUnit.MILLISECONDS)
.maxConnectionIdleTime(50, TimeUnit.MILLISECONDS)
.maxConnectionLifeTime(30, TimeUnit.MILLISECONDS)
.maxSize(12)
.minSize(2)
.build();
ServerSettings serverSettings = ServerSettings.builder()
.heartbeatFrequency(300, TimeUnit.MILLISECONDS)
.minHeartbeatFrequency(100, TimeUnit.MILLISECONDS)
.build();
ClusterSettings clusterSettings = ClusterSettings.builder()
.hosts(serverAddresses)
.localThreshold(200, TimeUnit.MILLISECONDS)
.serverSelectionTimeout(250, TimeUnit.MILLISECONDS)
.requiredClusterType(ClusterType.REPLICA_SET)
.mode(ClusterConnectionMode.SINGLE)
.requiredReplicaSetName("rs0")
.build();
SocketSettings socketSettings = SocketSettings.builder()
.connectTimeout(100, TimeUnit.MILLISECONDS)
.readTimeout(50, TimeUnit.MILLISECONDS)
.build();
SslSettings sslSettings = SslSettings.builder()
.enabled(false)
.invalidHostNameAllowed(false)
.build();
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connUrl)
.applyToClusterSettings(builder -> builder.applySettings(clusterSettings))
.applyToConnectionPoolSettings(builder -> builder.applySettings(poolSettings))
.applyToServerSettings(builder -> builder.applySettings(serverSettings))
.applyToSocketSettings(builder -> builder.applySettings(socketSettings))
.applyToSslSettings(builder -> builder.applySettings(sslSettings))
.readPreference(ReadPreference.primary())
.build();
MongoClient mongoClient = MongoClients.create(settings);
return new SimpleMongoClientDatabaseFactory(mongoClient, "<databaseName>");
}
@Bean
MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
}
I am not sure, why it is not connecting to the database cluster. Any suggestions?
Thanks,
Atul
I am able to solve the issue. I have commented couple of settings and then its working fine.
I do not know what the issue is with ClusterSettings and SSLSettings code. I guess it is a bug(not sure)
Thanks,
Atul