i read a lot of topic on stack overflow and original docs of node-mysql but nowhere it is clearly described how to handle connections.
i create connection in the very beginning of my script with .createConnection(params)everything works fine i do my last operation with db and naturally im closing it by .end() method the problem starts here
when another user connects my server instead of creating new connection -which i wrote in the very beginning as i told- gives me an error Cannot enqueue Query after invoking quit
the point here i dont understand is i ended connection in previous users section end thi sis new user and script is called from the beginning
why an already ended connection cannot be re-connected is this a bug or am i doing something wrong
I found out the same connection cannot be used twice so i simply added a callback inside connection#end() and if theres no error i set my connection variable to null simply something like this
var connection =connection?connection:mysql.createConnection(dbParams);
connection.connect(
function(err){
if(err)console.log("cannot connect due to",err);
else console.log("connected");
})
//something happens here with db
connection.end(
function(err){
if(!err)connection=null;
else console.log(err)
});
And now everything works perfect
Related
I have a Lambda function that receives a message from SNS and uses a custom module that queries an external database and outputs calculations to the database. The module works fine: the Lambda has internet access via VPC and successfully connects to the database and outputs the desired data to the database, but I am still getting the error "Task timed out after 3.00 seconds." The module itself uses sequelize, async/await, and promises.
I increased the max timeout and the only difference was that the number of seconds in the error message increased to the timeout limit. I tried reserving concurrency and the error persists. Every part of my function works great other than the fact that the callback never resolves, producing a timeout error. I have tried running the function with and without the "context.callbackWaitsForEmptyEventLoop" statement, running it with the statement only makes the code return before any of the rating engine function is completed. Here is the rating engine code: https://github.com/elizajanus/rating-engine-module
Is it possible that the database connection is not closing within my custom module and preventing the code from fully completing the imported function? Or could it be something else? This issue may be connected: https://github.com/sequelize/sequelize/issues/8468
const {RatingEngine} = require('./rating-engine');
exports.handler = (event, context, callback) => {
const message = event.Records[0].Sns.Message;
RatingEngine(message, message.d_customer_id,
message.d_total_distance_travelled);
callback(null, 'move record created in database');
};
#Eliza Janus, you can test your code locally with https://www.npmjs.com/package/lambda-local, this will help you you to better identify the problem debugging the code.
I'm trying to send information from android to mysql database through express server (using node js)
however there seems to be a problem in the code, it keeps showing error, but I just can't find what's wrong.
I think there's problem with connection.query statement.
Actually this is not my code, but it's the source code of my teacher. I checked everything in the android twice, but there's nothing wrong. There's gotta be something wrong here :(
This is what it is keep saying. Please help :( Stock for few days...
When you call the getConnection() method, the callback function passed to it should check for errors first. The reason you're running into the "cannot get property of undefined" error is most likely because the connection object is never returned and therefore undefined, probably because getConnection() encounters an error.
Try adding an error check condition and log the error to see why getConnection() fails:
mysql_connection_info.getConnection(function(err, connection) {
// check for errors in getting the connection
if(err) {
console.log(err)
return
}
// carry on with the actual query if there are no errors
})
I think you are missingmysql_connection_info.connect()
place it just after creating the connection pool.
I have a question regarding the flow of go lang code.
In my main function, I am opening mysql connection and then using `defer" to close the connection at the end of the connection.
I have route where WebSocket is set up and used.
My Question is will program open connection every time, WebSocket is used to send and receive a message or will it just open once the page was loaded.
Here is how my code looks like:-
package main
import (
// Loading various package
)
func main() {
// Opening DB connection -> *sql.DB
db := openMySql()
// Closing DB connection
defer db.Close()
// Route for "websocket" end point
app.Get("/ws", wsHandler(db))
// Another route using "WebSocket" endpoint.
app.Get("/message", message(db))
}
Now, while a user is at "message" route, whenever he is sending the message to other users, Will mysql - open and close connection event will happen every time, when the message is being sent and receive using "/ws" route?
Or will it happen Just once? whenever "/message" route and "/ws" event is called the first time.
My Purpose of using "db" in "wsHandler" function is to verify and check if the user has permission to send a message to the particular room or not.
But there is no point opening and closing connection every second while WebSocket emits message or typing event.
What would be the best way to handle permission checking in "/ws" route, if above code is horror? Considering a fact there will be few hundred thousand concurrent users.
Assuming db is *sql.DB your code seems fine, I'm also assuming that your example is incomplete and your main does not actually return right away.
The docs on Open state:
The returned DB is safe for concurrent use by multiple goroutines and
maintains its own pool of idle connections. Thus, the Open function
should be called just once. It is rarely necessary to close a DB.
So wsHandler and message should be ok to use it as they please as long as they don't close DB themselves.
I am using express 4.x, and the latest MySQL package for node.
The pattern for a PHP application (which I am most familiar with) is to have some sort of database connection common file that gets included and the connection is automatically closed upon the completion of the script. When implementing it in an express app, it might look something like this:
// includes and such
// ...
var db = require('./lib/db');
app.use(db({
host: 'localhost',
user: 'root',
pass: '',
dbname: 'testdb'
}));
app.get('/', function (req, res) {
req.db.query('SELECT * FROM users', function (err, users) {
res.render('home', {
users: users
});
});
});
Excuse the lack of error handling, this is a primitive example. In any case, my db() function returns middleware that will connect to the database and store the connection object req.db, effectively giving a new object to each request. There are a few problems with this method:
This does not scale at all; database connections (which are expensive) are going to scale linearly with fairly inexpensive requests.
Database connections are not closed automatically and will kill the application if an uncaught error trickles up. You have to either catch it and reconnection (feels like an antipattern) or write more middleware that EVERYTHING must call pior to output to ensure the connection is closed (anti-DRY, arguably)
The next pattern I've seen is to simply open one connection as the app starts.
var mysql = require('mysql');
var connection = mysql.createConnection(config);
connection.on('connect', function () {
// start app.js here
});
Problems with this:
Still does not scale. One connection will easily get clogged with more than just 10-20 requests on my production boxes (1gb-2gb RAM, 3.0ghz quad CPU).
Connections will still timeout after a while, I have to provide an error handler to catch it and reconnection - very kludgy.
My question is, what kind of approach should be taken with handing database connections in an express app? It needs to scale (not infinitely, just within reason), I should not have to manually close in the route/include extra middleware for every path, and I (preferably) to not want to catch timeout errors and reopen them.
Since, you're talk about MySQL in NodeJS, I have to point you to KnexJS! You'll find writing queries is much more fun. The other thing they use is connection pooling, which should solve your problem. It's using a little package called generic-pool-redux which manages things like DB connections.
The idea is you have one place your express app access the DB through code. That code, as it turns out, is using a connection pool to share the load among connections. I initialize mine something like this:
var Knex = require('knex');
Knex.knex = Knex({...}); //set options for DB
In other files
var knex = require('knex').knex;
Now all files that could access the DB are using the same connection pool (set up once at start).
I'm sure there are other connection pool packages out there for Node and MySQL, but I personally recommend KnexJS if you're doing any dynamic or complex SQL queries. Good luck!
I am getting the following error on the same query:
Warning: mysql_query(): Access denied for user 'www-data'#'localhost' (using password: NO)
The error only occurs on the first try from any particular IP. After I refresh the page, the script runs fine. To be more specific, I will run it from a browser in one location, and it will error. If I refresh the page, the problem is fixed. That is until I try from another location.
From what I gather, its losing the connection to the database server and trying to reconnect with the default username. What's confusing me is that it fails on the same query each time. Let's call this query_X. Multiple queries run before query_X including selects, inserts, and updates. Query_X looks like this:
UPDATE game_users
SET status_cd=$tmp_status,
expire_date=date_add('$currentExpire', interval $l_license_duration_days day)
WHERE game_user_id=$l_game_user_id
As an example, the variables being passed are:
$tmp_status = 1;
$currentExpire = '2011-12-05';
$l_license_duration_days = 30;
$l_game_user_id = 1;
What is it about this query that makes the connection fail, and why does refreshing the page fix the problem?
Fyi, I'm using persistent connect.
Edit: I just tried again from an ip that I tried last night. And I received the error again. That is on the first run of the script. After I ran the page a second time, it worked fine.
Edit: It will also error the first time it's run on a particular browser, even if it is on the same IP, i.e. it will error once on firefox and then be fine, error on chrome once and then be fine, etc.
Edit: I've narrowed it down to a mail() function that was happening just before query_X. I still don't know why the mail function is closing the mySQL connection. I've decided to put the mail function (which works properly and sends the email) at the end of the php file, where I would close the connection anyways. It's a hack, but I've spent too much time on this already.
It looks like you closed the connection before some call to mysql_query(). The message of access denied should happen on the mysql_connect() phase.
Are you sure the error is in the query and not in the connect / close functions?