I have a class called Connection like one below. This only executes select statements. I have non pooling connection for insert or update.
var _mysql = require('mysql');
function Connection()
{
//private variables and dependencies includes
//create mysql pool connection requires nodejs mysql this connection is used only for selects.
var _connectionSelect = _mysql.createPool({
host : _config.mySQLDB.select.dbHost,
user : _config.mySQLDB.select.dbUser,
password : _config.mySQLDB.select.dbPass,
database : _config.mySQLDB.select.dbName,
supportBigNumbers : true,
connectTimeout : 7000,
connectionLimit : 5,
queueLimit : 5
});
this.executeSelect = function(sql, callback, Message)
{
//connects to mysql.
_connectionSelect.getConnection(function(connectionError, Connection){
if(connectionError)
{
console.log(connectionError);
//throws error if connection or sql gone wrong
Message.add("error", 'serviceDown');
Message.add("devError", 'unknownError');
callback(false);
}
else
{
//executes the query passed
Connection.query(sql, function(error, rows) {
Message.incerementQuery();
if(error)
{
Connection.release();
console.log(error+sql);
//throws error if connection or sql gone wrong
Message.add("error", 'unknownError');
Message.add("devError", "seriousError", "Database errors at resource server side");
callback(false);
}
else
{
Connection.release();
//executes the callback function
callback(rows);
}
});
}
});
};
}
exports.Connection = Connection;
I created an instance of this class whenever I want to execute a query.
I am aware that the default concurrent connections in MySQL is 100 and I wanted to keep that number.
Whenever I try running my application, this connection pooling is incrementing every select and reaches 100 connections pretty soon.
As you can see I am releasing the connection on success or error states. I am pretty sure that I must be doing something wrong, but difficult to figure out.
Is it because how I create instances of this class? I was hoping that if I supply
connectionLimit : 5
even if I create many instances of this class it should only utilise 5 connection?
Note: I have only one instance of this app in my local machine.
Sorry to be so amateur, I am new to this streaming I/O business. I love the idea of pooling but if I cant sort this out, I may need to use traditional open and close connection for every query . Any help would be much appreciated.
Many thanks,
Karthik
Got the answer from Doug Wilson from git hub https://github.com/dougwilson.
I should have instantiated createPool outside of the function. Works like a charm.
The code goes like
var _mysql = require('mysql');
//create mysql pool connection requires nodejs mysql this connection is used only for selects.
var _connectionSelect = _mysql.createPool({
host : _config.mySQLDB.select.dbHost,
user : _config.mySQLDB.select.dbUser,
password : _config.mySQLDB.select.dbPass,
database : _config.mySQLDB.select.dbName,
supportBigNumbers : true,
connectTimeout : 7000,
connectionLimit : 5,
queueLimit : 5
}
function Connection()
{
//private variables and dependencies includes
);
this.executeSelect = function(sql, callback, Message)
{
//connects to mysql.
_connectionSelect.getConnection(function(connectionError, Connection){
if(connectionError)
{
console.log(connectionError);
//throws error if connection or sql gone wrong
Message.add("error", 'serviceDown');
Message.add("devError", 'unknownError');
callback(false);
}
else
{
//executes the query passed
Connection.query(sql, function(error, rows) {
Message.incerementQuery();
if(error)
{
Connection.release();
console.log(error+sql);
//throws error if connection or sql gone wrong
Message.add("error", 'unknownError');
Message.add("devError", "seriousError", "Database errors at resource server side");
callback(false);
}
else
{
Connection.release();
//executes the callback function
callback(rows);
}
});
}
});
};
}
exports.Connection = Connection;
Thanks a lot. Sorry to be so stupid.
Karthik
Related
I am trying to set up an alexa skill that calls MySQL Querys when a certain question gets asked. Nothing I tried seemed to work because I either get an error or nothing happens at all.
I am using/what I am working with:
Alexa Developer Console
Cloud9 as IDE(which uploads the code to AWS Lambda, where I defined the environmental variables used in my code)
AWS Lambda, NodeJS
Amazon RDS, which hosts my DB instance
MySQL Workbench (where I created a few tables to test the database, which works fine)
I tried several ways to solve my problem, like creating a connection or a pool, but I think it has to be handled differently, because Alexa has to wait for the response.
const GetOeffnungszeiten_Handler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'GetOeffnungszeiten' ;
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
const responseBuilder = handlerInput.responseBuilder;
let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let say = 'OUTPUT: ';
var mysql = require('mysql');
var connection = mysql.createPool({
host : process.env.MYSQL_HOSTNAME,
user : process.env.MYSQL_USERNAME,
password : process.env.MYSQL_PASSWORD,
database : process.env.MYSQL_DATABASE,
port : process.env.MYSQL_PORT
});
exports.handler = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
connection.query('select name from persons where id=1', function (error, results, fields) {
connection.release();
if (error) {
callback(error);
say=say+'0';
} else {
callback(null,results[0].name);
say=say+' 1';
}
});
});
};
return responseBuilder
.speak(say)
.reprompt('try again, ' + say)
.getResponse();
},
};
I expect the output to either be "OUTPUT: 1" or "OUTPUT: 0" but it is "OUTPUT: "
With output I refer to the say variable.
Your function is returning responseBuilder...getResponse() before the SQL connection finishes and callback is called.
I would suggest to refactor your code using async and await to make it easier to read and to understand. (read https://stormacq.com/2019/06/22/async-js.html for help)
Be sure to return the Alexa response only when your call to MySQL returns, and not before. Remember that Alexa timeout is 8 secs, so your code need to return before that. Be sure that the AWS Lambda timeout is aligned to the Alexa timeout too (put it at 7 secs)
Finally, I would advise against using MySQL for Alexa skills. Because each Lambda invocation might be served by different containers, your code will create a connection pool for each interaction between customers and your skill, creating a significant delay to bring a response to customers. DynamoDB and Elastic Cache are much better suited to Alexa skills.
I have a node/express/mysql2 web app that accesses a mySql DB through a connection pool object and I often run into the following issue: I leave the code for a while then when i come back and access pages that run queries I'll get
Error in foo: Error: connect ETIMEDOUT
Error in bar: Error: read ECONNRESET
I guess that on the other side mysql sees idle connections and close them, the client app doesn't know that, get those connections from the pool and then run into those issues, fine. But I was under the impression that this was automatically handled by mysql2 ?
This is roughly how i organised the db code
sqlConnectionPool.js
const dbParam = require('./dbParam.js');
const sqlPool = require('mysql2/promise').createPool(dbParam.connection.prod);
module.exports = sqlPool;
dummyQuery.js
const sqlPool = require('./sqlConnectionPool.js');
module.exports.updatefoo = async (ID, sqlConnection = undefined) => {
let connection;
try {
connection = sqlConnection === undefined ? await sqlPool.getConnection() : await sqlConnection;
const [updateResult] = await connection.query('update foo set barID=?', [ID]);
if (updateResult.affectedRows !== 1) {
throw (new Error(`error on ID ${ID}`));
}
return undefined;
} catch (err) {
console.error(`Error in updatefoo: ${err}`);
return err;
} finally {
if (sqlConnection === undefined) {
connection.release();
}
}
};
Is there something I'm missing to have those errors automatically handled, or simply not run into them ? I guess the mysql2 library needs to close the connection when they get connreset or conntimeout error and return them to the pool...
Thanks !
I think you should use something like setTimeout() to jab good the DB.
I need to create a lambda function to act as the middleman between a mobile Java app and an AWS RDS MySQL database. The idea is to submit queries from the mobile app and then send them off to the lambda function, which will then return the query. I have a basic MySQL query set up in my AWS lambda:
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool({
host : config.dbhost,
user : config.dbuser,
password : config.dbpassword,
database : config.dbname
});
exports.handler = (event, context, callback) -> {
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
// Use the connection
connection.query('select Album from record', function (error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) callback(error);
else callback(null, results[0].Album);
// Don't use the connection here, it has been returned to the pool.
});
});
};
And all that I am currently trying to do is get this code to run and output what the query will return. I've seen tutorials where people seem to just click test and have the code run, but it keeps asking me to create a test, and I'm not sure what exactly I would need to do to test this function.
EDIT: I realized I was missing a small change in my lambda uploaded code, but I am now getting an error on line 10 saying there is an unexpected token >.
I'm not sure what's wrong here, as the tutorial I watched seems to have the same exact thing.
Since you're not passing in any parameters through the context, you can just create a test with the defaults or an empty object {}, and click Test in the console. It will invoke your Lambda function as if it had been called from your mobile app, and you can debug from there.
I am very new to Node.js development and I am working on an app that requires me to pull users from a mysql database. I am using the promise-mysql library to query a mysql database. I am trying to use a connection pool like this:
var pool = mysql.createPool({
host: hgh.host,
user: hgh.user,
password: hgh.pw,
database: hgh.name,
connectionLimit: 10
});
As a global variable in my module.
I then have the above function to return a connection from the pool.
function connect() {
return pool.getConnection().then(function(connection) {
return connection
}).catch(function(error) {
console.log("Connect failed");
throw ErrorModel.generateErrorObject(error, 500);
});
}
Below is a function I am using to query the database with:
function getUser(username) {
var sql_query = `SELECT * FROM userstable WHERE userName = ` + `'` + username + `'`;
return connect().then(function(conn) {
return conn.query(sql_query).then(function(rows) {
console.log("HGH getUser Then");
console.log(pool);
conn.release();
return rows;
});
}).catch(function(error) {
console.log("HGH getUser Catch");
console.log(error);
throw ErrorModel.generateErrorObject(error, 500);
});
}
I am getting this issue:
conn.release is not a function when trying to release my current connection into the pool. Is my logic wrong here? My goal is to have a pool with a bunch of connections (up to a certain number) and if a user needs to query, the getConnection() function just grabs them either a free connection from the pool, or creates them one. Problem is I cannot for the life of me release it back to the pool as free..Every time I make a request with conn.end() instead of release the connection remains in the _allConnections array of the pool when I console it out, and there are absolutely no connections in the _freeConnections array.
Anyone know how I can make connections free again??
Looking at the module's code I found the function for releasing a connection from a pool:
pool.prototype.releaseConnection = function releaseConnection(connection) {
//Use the underlying connection from the mysql-module here:
return this.pool.releaseConnection(connection.connection);
};
So if all of these functions live in the same file you could do the following in the getUser function:
replace
conn.release();
with
pool.releaseConnection(conn);
Looking at the code, promise-mysql wraps the original connection object, which doesn't expose the release method. However, the original is exposed as a property called connection, so this works:
conn.connection.release();
A few random thoughts:
You should probably escape your query input:
var sql_query = `SELECT * FROM userstable WHERE userName = ${ pool.escape(username) }`;
Your code doesn't release connections when an error occurs (because the code in the .then() callback wouldn't get called); it's better to use .finally() to do the releasing, as that will get called for both resolved and rejected cases:
function connect() {
var conn = null;
return pool.getConnection().then(function(connection) {
conn = connection;
return connection;
}).catch(function(error) {
console.log("Connect failed", error);
}).finally(function() {
if (conn) {
conn.connection.release();
}
});
}
I cannot understand why could I connect to Mysql after release.
And is there a way to check the status of Mysql ?
Thanks for the help !
var mysqlConfig = {
host : "abcd",
port : "3306",
user : "root",
password : "root",
database : "test"
};
var pool = mysql.createPool(mysqlConfig);
pool.getConnection(function(err, conn) {
// release pool
conn.release();
// After release, Why could I connect to Mysql ????
conn.query('SELECT * FROM user_info WHERE user_id = ?', [id], function(err, rows) {
if (err) {
pushErr();
}
// ...
});
});
Based on the documentation here: https://github.com/felixge/node-mysql it looks like the connection being 'released' doesn't destroy the connection per se, it just signals the pool that it can be used by someone else if needed. If you notice the example, they release as part of the query callback. If you need to actually get rid of the connection, you should use conn.destroy().