I want to know whether there is any existing user with emailid in my db by the name say xyz#abc.com .
function isDuplicateUser(emailId)
{
var sql='SELECT count(*) FROM UserDetails WHERE emailId ="'+emailId+'";';
var con = mysql.createConnection({
host:"localhost",
user: "root",
password: "32577488",
database:"mydb"
});
con.connect(function(err) {
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Inside Duplicate check");
console.log(result[0]);
console.log(result.count);
console.log(result[0].emailId);
console.log(result[0].count);
console.log("1 record inserted");
});
});
}
But in all the console.log statements I am getting undefined ! I thought of using count so that if there is more than 0 than there exists a user with that userid ! Please help me ! I have another doubt also It is ok to get connection in every function ! Like for signup have a
function signup(email,password,name)
{
var sql =''....;
//getting connection once
}
function signin(emailid,password)
{
//getting connection here
return success;
}
It seems like code is getting replicated many times !
please, try with this sql statement and let me know the result:
// replace your var sql with this one
const sql = `SELECT count(*) FROM UserDetails WHERE emailId = '${emailId}'`;
In the other hand, regarding how to manage connection in each function, my recommendation is create a pool of connections in your app and get a connection from the pool on each function you need (you need to take care about releasing the connection when you finish to make them available for new incoming requests):
// connection first time when node starts
const options = {
connectionLimit: 10,
host: HOST,
user: USER,
password: PASSWORD,
database: DATABASE,
port: 3306,
timezone: 'Z',
// debug: true,
multipleStatements: true,
// ...azureCertificate && sslOptions,
};
const pool = mysql.createPool(options);
// on each function, get a connection from the pool using this function
getConnection() {
return new Promise((resolve, reject) => {
if (pool === null) {
return reject(new Error(`MySQL connection didn't established. You must connect first.`));
}
pool.getConnection((err, connection) => {
if (err) {
if (connection) {
connection.release();
}
return reject(err);
}
return resolve(connection);
});
});
}
Hope this helps.
This example was made using npm package mysql: https://www.npmjs.com/package/mysql
Related
I am trying to connect my application to the database using the connection pool method, its connecting fine, and data insertion is happening fine without any issues but other queries in the same file are slowing down.
I have tried with release() method also not working properly.
How can release the pool to the next query once it's executed the current query?
Below is my dbpool.js file code where I am writing a common generalized database connection,
var pg = require('pg');
var PGUSER = 'postgres';
var PGDATABASE = 'test_database';
var config = {
user: PGUSER, // name of the user account
host: 'localhost',
database: PGDATABASE, // name of the database
password: 'password#AWS',
port: 5432,
max: 10,
idleTimeoutMillis: 10000
};
const pool = new pg.Pool(config);
const DB = {
query: function(query, callback) {
pool.connect((err, client, done) => {
if(err){ return callback(err); }
client.query(query, (err, results) => {
// done();
client.release();
// if(err) { console.error("ERROR: ", err) }
if(err) { return callback(err); }
callback(null, results.rows);
})
});
}
};
module.exports = DB;
I tried with both the done() and client.release() method but no luck. If I use both then I am getting an error message client is already released.
Below is my socket.js file code:
var express = require('express');
const connection = require('./dbpool.js');
if(arData == '0022'){
const queryText = "INSERT INTO alert(alert_data) VALUES('"+arData+"')";
connection.query(queryText,(err, res) => {
if(err){
console.log(err.stack);
}
});
}
if(arData == '0011'){
const queryText = "INSERT INTO table2(alert_data) VALUES('"+arData+"')";
connection.query(queryText,(err, res) => {
if(err){
console.log(err.stack);
}
});
}
function ReverseCommunication(){
const select1 = "SELECT * FROM alert WHERE action = '0' ORDER BY alert_id ASC LIMIT 1";
connection.query(select1, (err, res) =>{
if(err) {
console.log("Error1");
res.json({"error":true});
}
else{
console.log("res==",res);
}
});
}
setInterval(function(){
ReverseCommunication();
}, 2000)
With pool you shouldn't need to close the connection. With pool it will reuse the connection pool for subsequent request so you don't have to connect to the DB each time.
(i'm not a PG expert here, sure other could expand on that way better then I )
What works for us is to set up the dbpool file you have like this
const {Pool,Client} = require('pg');
const pool = new Pool({
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_URL,
database: process.env.POSTGRES_DATABASE,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
keepAlive: true,
connectionTimeoutMillis: 10000, // 10 seconds
max: 10
});
pool.connect()
.then(() => console.log('pg connected'))
.catch(err => console.error(err))
module.exports = pool
Then use the pool.query like you have now with pool.connect
Also, just a side note what lib are you using for PG? Noticed your queries are dynamic, you may want to adjust those to prevent possible SQL-injection.
I encountered a weird bug when doing a quick coding assignment.
Here is my code.
Lets call this 'A'
//Grab all the animals from the database
WebApp.get('/all',(req,res) =>
{
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '1234', //Enter your password here
// I found that mySQL 8.0 uses a new default authent plugin whereas 5.7 uses a different one, If you get a ER_NOT_SUPPORTED_AUTH_MODE error from the response, try referring to this post to alter your root password. (https://stackoverflow.com/questions/50373427/node-js-cant-authenticate-to-mysql-8-0)
database: 'animals'
});
const query = "SELECT * FROM animals";
connection.query(query, (err, rows, fields) =>
{
if (err)
{
console.error('error : ' + err.stack);
res.sendStatus(500);
return;
}
console.log("Fetched animals successfully");
//console.log(rows); // Use this for error checking to see if a authent problem occurs.
res.json(rows);
});
});
and this 'B'
//Grab a selected animal from the database given a valid Id.
WebApp.get('/:id',(req,res) =>
{
console.log("Fetching user with id: " + req.params.id);
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '1234', //Enter your password here
// I found that mySQL 8.0 uses a new default authent plugin whereas 5.7 uses a different one, If you get a ER_NOT_SUPPORTED_AUTH_MODE error from the response, try referring to this post to alter your root password. (https://stackoverflow.com/questions/50373427/node-js-cant-authenticate-to-mysql-8-0)
database: 'animals'
});
const animalId = req.params.id;
const query = "SELECT * FROM animals WHERE id = ?";
connection.query(query, [animalId], (err, rows, fields) =>
{
if (err)
{
console.error('error : ' + err.stack);
res.sendStatus(500);
return;
}
console.log("Fetched animals successfully");
//console.log(rows); // Use this for error checking to see if a authent problem occurs.
res.json(rows);
});
});
For some reason, if I put A before B it works, and I get successful results from my queries. However, if I put B before A, B will return successfully, but A will return '[]'. Anyone know why?
Thanks for any help!
Have you tried terminating the connection after each request, or considered using a connection pool? I am not familiar with nodeJS integration with MySQL, but in SQLServer, it is best to use ConnectionPool, when asynchronously making database requests.
In my node.js app I am using the mysql library for database connectivity.
When I start my node server I can query the database perfectly fine – no issues
When I query the database after 5 minutes the server returns the following error:
{"code":"PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR","fatal":false}
If I restart my node.js server I can query again with no issues…
Here is my code
const mysql = require('mysql');
let connection = mysql.createPool({
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database
});
router.post('/subscription', (req, res) => {
const user = req.body;
const q = 'INSERT into Subscription SET ?';
connection.query(q, user, (err, results) => {
if (err)
return res.json(err);
return res.json(results);
});
});
I have used both mysql.createConnection and mysql.createPool…. also tried ending the connection manually with connection.end….
Both results end in the same error.
You need to get a connection from the pool and use that, not query the pool itself. When you get a connection from the pool, the pool will make sure you get a valid connection from the pool. So your code would be:
const mysql = require('mysql');
let pool = mysql.createPool({
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database
});
router.post('/subscription', (req, res) => {
const user = req.body;
const q = 'INSERT into Subscription SET ?';
pool.getConnection(function(err, connection) {
if (err)
return res.json(err);
connection.query(q, user, (err, results) => {
if (err)
return res.json(err);
return res.json(results);
});
})
});
UPDATE:
You don't need to do separate pool.getConnection and connection.query, you can combine them into a pool.query which will get a connection, do the query and release the connection. So, the updated code would be:
const mysql = require('mysql');
let pool = mysql.createPool({
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database
});
router.post('/subscription', (req, res) => {
const user = req.body;
const q = 'INSERT into Subscription SET ?';
pool.query(q, user, function(err, connection) {
if (err)
return res.json(err);
return res.json(results);
});
});
I think my connections aren't being released properly. Sometimes I get an error stating that my pool has reached its limit. Also, sometimes accessing the db randomly takes 15+ seconds. Whenever I check how many connections are in use using pool._allConnections.length, it never returns anything above 60. Here is my code:
const mysql = require('mysql');
const config = require('./config.json');
const pool = mysql.createPool({
connectionLimit : 999,
host: config.host,
user: config.user,
password: config.password,
database: config.database
});
const db = (() => {
_query = (query, params, callback) => {
pool.getConnection((err, connection) => {
if (err) {
callback(null, err);
} else {
connection.query(query, params, (err, rows) => {
connection.release();
if (!err) {
callback(rows);
} else {
callback(null, err);
}
});
}
});
};
return {
query: _query
};
})();
module.exports = db;
i've faced same issue and https://github.com/mysqljs/mysql/issues/1518 help me. Notice line
Yeah, that was the issue. I was calling mysql.createPool on each
query.
Actually you are importing db from query.js (let say your post code) to fire a query. every time you fire a query it create a new pool.to solve this issue you can put createPool code block to app.js and can share it global or can use in query.js via any other code style.
Referring official doc https://github.com/mysqljs/mysql#pooling-connections find line
Since the pool.query method is a short-hand for the pool.getConnection
-> connection.query -> connection.release() flow, calling pool.end() before all the queries added via pool.query have completed,
later i used this to stop headache of release connection
Hello everyone,
just wondering , is this the proper way to :
get connection from connection pool ,
send a request to mysql server,
wait for result
return the connection back to connection pool?
This below code is called everytime an user logs in, it checks his username and token, if it's a match it will open main menu page, if not it will return back to login page.
The truth is, that it's all working in Chrome, but sometimes it does not work in Firefox , it just doesn't call the connection.query() part at all..
So I'm just checking with you guys , if everything is okay with the code below..Or if there is anything I could improve or change..
var db_pool = mysql.createPool({
host: 'localhost',
user: 'dbuser',
password: 'pass',
database: 'db_name'
});
function CheckUser(username, token)
{
db_pool.getConnection(function(err, connection)
{
console.log(" [i] Getting connection from pool. ");
var entry = 0;
var query = connection.query("SELECT token FROM users where token = '"+token+"' and user_id = '"+username+"'");
query.on('result', function(data){
entry++;
});
query.on('error',
function(err){
throw(err);
}
);
query.on('end',
function()
{
if(entry == 1)
{
console.log(" [info] User ["+username+"] authorized.");
/* DO STUFF */
}else
{
console.log(" [Error] User ["+username+"] does not have token: ["+token+"].");
return false;
}
}
);
console.log(" [i] Returning back connection to pool. ");
connection.release();
});
}
Any help is greatly appreciated,
Alex
I believe that the intermittent behavior is because your release() statement is in line with the connection rather than in the 'end' handler. Here is what works for me:
var mysql = require('mysql'),
config = require('./config.json');
var pool = mysql.createPool( config );
var checkUser = function(username, token) {
pool.getConnection(function(err, connection) {
if (err) throw err;
console.log(" [i] Getting connection from pool. ");
var entry = 0,
statement = 'SELECT token FROM users where token = ? and user_id = ?',
query = connection.query( statement, [ token, username ] );
query.on('result', function(data) {
console.log( data );
entry++;
});
query.on('error', function(err) {
console.log( err );
throw err;
});
query.on('end', function() {
if (entry === 1) {
console.log(" [info] User ["+username+"] authorized.");
/* DO STUFF */
} else {
console.log(" [Error] User [", username, "] does not have token [', token, ']');
}
console.log(" [i] Returning back connection to pool. ");
connection.release();
});
});
};
console.log('check the user...');
checkUser( 'test-user', 'mytoken');
I was able to test this (with an alternate statement) with no problems. I also put the config stuff in a separate file and replaced the string statement with parameters.
Hope this helps...