Where to close db connection?

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

1 Like