Where to close db connection?

i’m a beginner of programming
and i want to make a discord-bot which use mongo db in kotlin

in my reference, the author always close the db connection
but i have no idea where to close connection.

is it necessary to close the db connection?

( i’m not good at english, so if i made a mistake, feel free to correct me)

Hey @1118

Just wanted to share some stackover flows related to your question.

I am using Mongoose as an ORM for my communication with Mongodb Atlas. And I do not close the connection anywhere. I just open a connection in the node express server, and monitor the connection for disconnection/reconnects

1 Like

The Kotlin application uses a MongoDB Java driver to access the database server. This is a link to a Kotlin program where the code shows the closing of the MongoClient object: How to Insert a MongoDB Document using Kotlin.

In general, it is always a good practice to close the resources after they are used and before exiting the application. The MongoClient#close() method API documentation says:

Close the client, which will close all underlying cached resources, including, for example, sockets and background monitoring threads.

Here is another example: Connect to MongoDB from Kotlin Application – Example

@Natac13 @Prasad_Saya
Thank you for the reply.
hmm…, so i should close the connection or not?

When you create a MongoClient instance you create a connection; actually a pool of connections (aka connection pool). By default, the pool has 100 connections (and this depends upon the driver). This is configurable within the Java code. This means, ideally, you should have one Mongo Client instance per JVM.

So, the application’s client uses a connection from the pool and returns it back to the connection pool after its usage. There is no need for closing the connection explicitly. This way the application avoids creating and closing connections (which is an expensive operation).

Of course, you should close the MongoClient instance, with the close() method (I had mentioned in my previous reply) at the closing of the application, to clear all the resources.

1 Like

This note is about using the code for creating the MongoClient object and its close() method.

MongoClient extends an interface called as java.lang.AutoCloseable. When the object implementing this interface is used within a try-with-resources statement (as in code sample below) the object’s close() method is called automatically at the exit of the try block (releasing the associated resources).

A feature of using this try-with-resources statement is that, even when the code within the try block aborts abruptly (due to an exception), the close method is called automatically, and safely releasing the associated resources.

try (MongoClient mongoClient = MongoClients.create("mongodb://localhost/")) {		
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection<Document> coll = database.getCollection("testColl");		
    Document doc = coll.find(new Document()).first();	

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.