我应该保持数据库连接打开吗?

时间:2022-04-11 16:50:55

When I connect to Rexster graph server with Grex should I keep the database connection open?

当我使用Grex连接到Rexster图形服务器时,我应该保持数据库连接打开吗?

var grex = require('grex');
var client = grex.createClient();

client.connect({ graph: 'graph'}, function(err, client) {
    if (err) { console.error(err); }

    ...
});

I think I should because nodejs is single threaded so there's no chance of different requests trying to use the one connection at the same time.

我想我应该因为nodejs是单线程的,所以不可能同时尝试使用一个连接的不同请求。

1 个解决方案

#1


2  

Yes, you should. There 's no reason to have the overhead of connecting on every request. There will not be any issue of "mangling", as your code will be run in a single thread anyway.

是的你应该。没有理由在每个请求上都有连接的开销。不会有任何“mangling”问题,因为您的代码无论如何都将在一个线程中运行。

Furthermore, you could even have a pool of connections waiting to serve your requests in case you have a heavy usage application. Some adapters do it for you automatically, for example, MongoClient has a default pool of 5 connections.

此外,如果您的应用程序繁重,您甚至可以拥有一个连接池来等待您的请求。某些适配器会自动为您执行此操作,例如,MongoClient具有5个连接的默认池。

#1


2  

Yes, you should. There 's no reason to have the overhead of connecting on every request. There will not be any issue of "mangling", as your code will be run in a single thread anyway.

是的你应该。没有理由在每个请求上都有连接的开销。不会有任何“mangling”问题,因为您的代码无论如何都将在一个线程中运行。

Furthermore, you could even have a pool of connections waiting to serve your requests in case you have a heavy usage application. Some adapters do it for you automatically, for example, MongoClient has a default pool of 5 connections.

此外,如果您的应用程序繁重,您甚至可以拥有一个连接池来等待您的请求。某些适配器会自动为您执行此操作,例如,MongoClient具有5个连接的默认池。