kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all DB Client open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in DB Client

v9.8.1

3.9.1

v16.16.14

version 4.0.0

doobie

typegoose

v9.8.1

p6spy

3.9.1

SchemaCrawler

v16.16.14

scalikejdbc

version 4.0.0

Popular Libraries in DB Client

Trending New libraries in DB Client

Top Authors in DB Client

1

11 Libraries

97

2

11 Libraries

415

3

9 Libraries

51

4

9 Libraries

369

5

8 Libraries

174

6

6 Libraries

286

7

5 Libraries

98

8

4 Libraries

98

9

4 Libraries

66

10

4 Libraries

89

1

11 Libraries

97

2

11 Libraries

415

3

9 Libraries

51

4

9 Libraries

369

5

8 Libraries

174

6

6 Libraries

286

7

5 Libraries

98

8

4 Libraries

98

9

4 Libraries

66

10

4 Libraries

89

Trending Kits in DB Client

No Trending Kits are available at this moment for DB Client

Trending Discussions on DB Client

    Closing MongoClient connection on exit when using MongoDB Native driver?
    Bypassing Firestore Security Rules in jest tests
    How to use condition and multiple steps in a reactive function
    WordPress Nginx Configuration - GoDaddy Managed WP
    Check if user input exist in database using php and mongodb
    Strange behaviour insert_one mongocxx 3.6
    Simplest way to override an endpoint in google guice when writing functional tests
    How to bind lifetimes of Futures to fn arguments in Rust
    Encoding UTF8 string to latin1/iso-8859-1 with Go and MySQL
    Where is routing implemented (Request routing to the different replicas) when making request to the DynamoDB server?

QUESTION

Closing MongoClient connection on exit when using MongoDB Native driver?

Asked 2022-Apr-07 at 09:49

Should the MongoClient connection be closed every time the server shuts down?

I have seen the following code snippet and wanted to know if this is actually valid and should be done or if it's completely unnecessary to perform a closing on exit:

1// Adding listeners
2function setupListeners(client: MongoClient){
3    client.addListener('topologyClosed', ()=>{
4        isTopologyConnected = false;
5        console.warn("topologyClosed");
6    })
7}
8process.on("exit", () => {
9    console.log("EXIT - MongoDB Client disconnected");
10    closeConnection()
11});
12
13//Cleanups
14//catching signals and doing cleanup
15['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
16    'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
17].forEach(function (signal) {
18    process.on(signal, function () {
19       if (isTopologyConnected){
20                client.close();
21            }
22            process.exit(1);
23    });
24});
25

Thanks a lot.

ANSWER

Answered 2022-Apr-07 at 09:49

Should the MongoClient connection be closed every time the server shuts down?

Yes, it is a good practice to close the connection. As for every connection, mongo DB does assign a thread for its execution. If you won't close it, it keeps using the resources on the DB server.

Node.js connections use the pool to connect to DB and it can be reused while it is not being used, but it is good practice to close the connection if you are exiting the script as it won't close the connection automatically.

Source https://stackoverflow.com/questions/71779732

Community Discussions contain sources that include Stack Exchange Network

    Closing MongoClient connection on exit when using MongoDB Native driver?
    Bypassing Firestore Security Rules in jest tests
    How to use condition and multiple steps in a reactive function
    WordPress Nginx Configuration - GoDaddy Managed WP
    Check if user input exist in database using php and mongodb
    Strange behaviour insert_one mongocxx 3.6
    Simplest way to override an endpoint in google guice when writing functional tests
    How to bind lifetimes of Futures to fn arguments in Rust
    Encoding UTF8 string to latin1/iso-8859-1 with Go and MySQL
    Where is routing implemented (Request routing to the different replicas) when making request to the DynamoDB server?

QUESTION

Closing MongoClient connection on exit when using MongoDB Native driver?

Asked 2022-Apr-07 at 09:49

Should the MongoClient connection be closed every time the server shuts down?

I have seen the following code snippet and wanted to know if this is actually valid and should be done or if it's completely unnecessary to perform a closing on exit:

1// Adding listeners
2function setupListeners(client: MongoClient){
3    client.addListener('topologyClosed', ()=>{
4        isTopologyConnected = false;
5        console.warn("topologyClosed");
6    })
7}
8process.on("exit", () => {
9    console.log("EXIT - MongoDB Client disconnected");
10    closeConnection()
11});
12
13//Cleanups
14//catching signals and doing cleanup
15['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
16    'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
17].forEach(function (signal) {
18    process.on(signal, function () {
19       if (isTopologyConnected){
20                client.close();
21            }
22            process.exit(1);
23    });
24});
25

Thanks a lot.

ANSWER

Answered 2022-Apr-07 at 09:49

Should the MongoClient connection be closed every time the server shuts down?

Yes, it is a good practice to close the connection. As for every connection, mongo DB does assign a thread for its execution. If you won't close it, it keeps using the resources on the DB server.

Node.js connections use the pool to connect to DB and it can be reused while it is not being used, but it is good practice to close the connection if you are exiting the script as it won't close the connection automatically.

Source https://stackoverflow.com/questions/71779732