0

I started working on a KMongo project in Kotlin, though when I connect to my database KMongo creates 2 connections, instead of one (look at the image here). Does somebody know why?

My class to connect with the database

object Mongo {
    lateinit var connectionString: String
    lateinit var database: String

    private lateinit var mongo: MongoDatabase

    fun connect() {
        val client = KMongo.createClient(connectionString)
        mongo = client.getDatabase(database)
    }

    fun get(collection: String): CoroutineCollection<Document> {
        return this.mongo.getCollection(collection).coroutine
    }

}

fun database(database: Mongo.() -> Unit) = Mongo.apply(database)

The main class

fun main() {
    Thread {
        while (true) {
        }
    }.start()

    database {
        connectionString = "mongodb://username:password@host:port/test?w=majority"
        database = "Test"
    }.also { it.connect() }
}

For the dependencies I used

  • kmongo-async
  • kmongo-coroutine
Marian
  • 44
  • 1
  • 7
  • That quite probably depends on the connection string. – Joe Sep 20 '21 at 00:58
  • My connection String has the following pattern: `mongodb://username:password@host:port/test?w=majority` – Marian Sep 21 '21 at 15:23
  • Is the mongod configured with a replica set name? – Joe Sep 21 '21 at 17:21
  • No I don't think so. Would you set this up in the connection string too? – Marian Sep 22 '21 at 18:26
  • If you are connecting to a replica set, it is normal for the driver to open one connection to discover/monitor the nodes, and others for data operations – Joe Sep 22 '21 at 20:12
  • I'm using the free atlas tier. From what I read I think I don't use a replica set. Maybe I'm misunderstanding that but I also checked the connection String template here https://docs.mongodb.com/manual/reference/connection-string/. There the replica set string looks very different from mine too. – Marian Sep 23 '21 at 14:03
  • Atlas doesn't offer single nodes, so it is definitely a replica set. – Joe Sep 23 '21 at 15:53
  • Oh ok, I understand. Well, that clarifies my question. Thank you very much! – Marian Sep 23 '21 at 18:04

0 Answers0