Skip to content

Error handling in mySQL in Node #37

@scripting

Description

@scripting

The setting

  1. I am using the Node.js mysql package to interface to the mySQL app that's running locally.

  2. All queries are done by the Node app, locally.

  3. The Node app is also an HTTP server. A couple of the calls to the server via HTTP result in local calls to the database. The Node app is basically a gateway for the database.

The problem

I do a query first thing in the morning but the connection from the Node app on the database is been lost. Here's the error I see in the terminal app.

The only call that the app makes at this time is within a function called runSqltext. Here's what it looks like.

function runSqltext (s, callback) {
	theSqlConnection.query (s, function (err, result) {
		if (err) {
			console.log (err.message);
			if (callback !== undefined) {
				callback (undefined);
				}
			}
		else {
			console.log ("result == " + utils.jsonStringify (result));
			if (callback !== undefined) {
				callback (result);
				}
			}
		});
	}

As you can see I am catching the error and displaying it on the console. There is no message on the console. So it seems the error is not flowing through this code.

I have read the error handling section of the Node package docs. They provide an easy way out:

connection.on('error', function() {});

But they also are careful to say that this is not the recommended procedure.

So what do I do?

Follow-up question

Since I now know that the connection can be lost, should I be checking for that, say every second, and re-establishing the connection when it's lost? How can I tell that the connection is lost?

I do this in my websockets code, so it's no big deal, just not sure what's going on here.

Thanks in advance for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions