nodejs oracle improve ability - node.js

i am using node-oracleDB connector to connect to my local database , and i want to ask if it was an other(optimal) way to connect to the database, in order to improve communication ability between nodejs and oracle. thanks

I think using connection pooling is the good way. You can find some examples of pooling at here.
Please look at this file https://github.com/oracle/node-oracledb/blob/master/test/pool.js and read carefully to understand how handling connections in a pool.

Node-oracledb is the connector to use when writing Node.js applications that need to connect to Oracle Database.
Most apps will want to use a connection pool
var oracledb = require('oracledb');
oracledb.createPool (
{
user : "hr"
password : "welcome"
connectString : "localhost/XE"
},
function(err, pool)
{
pool.getConnection (
function(err, connection)
{
// use the connection
connection.execute(. . .
// release the connection when finished
connection.close(
function(err)
{
if (err) { console.error(err.message); }
});
});
});
});
There are some simplifications such as the 'default' connection pool that make it easier to share a pool across different modules. This is all covered in the documentation.

Related

How can I perfectly work with Oracle DB in my Node.js project

I am currently working on a web site using Angular, Node.Js, express and an Oracle database.
I'm still not familiar with all this technologies and I'm doing my best! My problem is the Oracle database connection can't be exported. I searched about this and I think it's a promise thing. The only solution I found is to connect to the database every time I use a new SQL query which is impossible for me because the project is big.
Is there any method I can use to make the database connection in a separate file and export it when I need it?
With a multi-user app, you should use a connection pool. You open it at app start, and then access it as needed - you don't have to pass anything around.
If you open the connection pool in your initialization routine:
await oracledb.createPool({
user: dbConfig.user,
password: dbConfig.password,
connectString: dbConfig.connectString
});
console.log('Connection pool started');
then any other module that needs a connection can simply do:
conn = await oracledb.getConnection();
result = await conn.execute(statement, binds, opts);
await conn.close();
Read up on connection pools and sizing and threads in the node-oracledb
documentation on Connection Pooling.
Also see the series Build REST APIs for Node.js and its code https://github.com/oracle/oracle-db-examples/tree/master/javascript/rest-api. In particular, look at database.js.

Mongodb Client connections with node.js issue

When I run my MongoDB connection with my node.js application which gaming platform here I facing the problem of an increasing number of MongoDB connections [Without using any query but its increasing repeatedly] which reaches to 819 and my MongoDB replication server stop responding and indirectly application stop the work. But I want to maintain a minimum of 20 connections on how to solve these issues please help me.
**
Mongodb connection: const connectionString =
'mongodb://AAAA:PASSWORD#HOST1:27001,HOST2:27002,HOST3:27003/dbName?replicaSet=rep1';
MongoClient.connect(connectionString, function (err, database) {
if
(err) {
console.log(err);
console.log("unable to connect Mongodb database on url: " + connectionString);
} else {
dbs['dashboardLog'] = database;
console.log("Mongodb database connected to server on url: " + connectionString); } });
**
I am using MongoDB client driver version: 3.4.23
Node.js: version 6.11
Fade-up with this issue please helps thanks in advance!!!
MongoClient already comes with connection pool support, just set the value you want for poolSize, if I remember correctly the default is 5 so set it to 20. You don't have to create a new connection everytime you want to run a new query. Just connect once and then keep that around.
See this article for more information https://blog.mlab.com/2017/05/mongodb-connection-pooling-for-express-applications/

Opening less connections with rethinkDB

My question is; Can i have less connections open when using rethindb? Right now I'm opening a new one every time I want to insert or get some data. Im afraid that this is not a right thing to do. Is there any way I can open just one and use it instead? I'm using nodejs.
Yes. You can run multiple queries on a connection. That's the recommended way of doing things.
The best way is to use connection pool. For nodejs, for example, we are using rethinkdb-pool.
I havent looked into the opensource connection pools for rethink, but I have a node app that uses rethink, and it will have a limited number of users, So I save my one connection to rethink as a global var and then use it for all queries.
'use strict';
var r = require('rethinkdb);
var rethinkConnect = null;
r.connect(
{
'host': 'localhost',
'port': '28015',
'password': 'noneya',
},
function(err, conn) {
if (err) {
console.log(err);
} else {
rethinkConnect = conn;
}
}
);
Now all queries the node.js server makes can use the connection. Keep in mind this code is async so you could not immediately make a query the next line after r.connect(). You could, however, use the payload of an inbound socket.io event as the params of a rethink query.
I'd advise you to use rethinkdbdash, an "advanced Node.js RethinkDB driver" that includes connection pools and some other advanced features. It has many more stars and contributors than rethinkdb-pool.

What's the proper way of using Postgres connections in Node?

I was wondering if anyone can help me understand what the proper way of maintaining multiple connections to multiple postgres servers via https://github.com/brianc/node-postgres is.
Obviously when running a node server for long duration we want to make sure we keep everything clean with no leaks and so I am wondering what the proper pattern is.
Please remember that my Node server will need to connect to 7-8 Postgres servers.
https://github.com/brianc/node-postgres supports the idea of pools. I am wondering: do I just connect to all servers on initial Node server set up and maintain open connections and each function can ask for a pool when it needs to talk to a server?
In other words, am I supposed to call pg.connect every time I make a server query? (minus the var pg and var connectionString which could be global)
Can't I just have a single connection be on and ready?
var pg = require('pg');
var connectionString = "pg://brian:1234#localhost/postgres"
pg.connect(connectionString, function(err, client, done) {
client.query('SELECT name FROM users WHERE email = $1', ['brian#example.com'], function(err, result) {
assert.equal('brianc', result.rows[0].name);
done();
});
});
Code snippets are greatly appreciated.

mongodb nodejs native driver close connection or not

Is it a good practice in nodejs to open mongodb connection per each request, and close it in the callback?
app.get('/some_route', function(){
MongoClient.connect(url,function(err, db){
//some db query with callback
db.collection("some_collection").findOne(doc, function(err,item){
if(err){
res.send(err);
//close db connection
db.close();
}else{
//do something with item
res.send(item);
//close db connection
db.close();
}
});
});
Some said that opening/closing mongodb connection on each request isn't necessary, since once opened, a pool of connections can be shared.
The question is how to maintain and share that pool? Is mongoose doing that automatically already?
Especially, on mongodb timeout or disconnect, does it need to be reconnected?
I find contradictory answers here close mongodb connection per request or not
Almost all the online doc nodejs mongodb native driver and example code I read, a db.open() is paired with db.close() somewhere in the callback.
Because if a pool of connection is shared, one might code
According to christkv's answer, one might code:
var p_db=null;
var c_opt = {server:{auto_reconnect:true}};
app.get('/some_route', function(){
//pseudo code
if (!p_db){
MongoClient.connect(url, c_opt, function(err,db){
p_db = db;
p_db.collection("some_collection").findOne(doc, function(err,item){
if(err){
res.send(err);
}else{
//do something with item
res.send(item);
}
});
});
}else {
p_db.collection("some_collection").findOne(doc, function(err,item){
if(err){
res.send(err);
}else{
//do something with item
res.send(item);
}
});
});
According to a major contributor to the driver source, It's best to connect to the database at startup and keep reusing that same connection for each request.
The mongodb native driver has a connection pool which it maintains internally and currently defaults to a maximum of 5 open connections. You can configure the maximum number of connections via the maxPoolSize option. You can also configure the connection to auto reconnect with the auto_reconnect option.
See the documentation here
You don't have to do anything to reconnect as the driver will attempt to reconnect on failure. While it waits for the reconnect to happen it will buffer all operations happening in between and replay them once the connection is up. If you want to control this yourself you can listen to the "close" event on the db instance and handle reconnection manually. On reconnect the db object will still be viable as a db is really just a wrapper around the shared connection pool and does not contain it's own separate connection logic.

Resources