Search code examples
node.jstitanrexster

Should I keep database connection open?


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

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.


Solution

  • 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.

    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.