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