Skip to content

Instantly share code, notes, and snippets.

@nareshv
Created March 25, 2013 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nareshv/5234990 to your computer and use it in GitHub Desktop.
Save nareshv/5234990 to your computer and use it in GitHub Desktop.
mysql connection error handler
diff --git a/src/server/db/mysql.coffee b/src/server/db/mysql.coffee
index 76658fc..0b60062 100644
--- a/src/server/db/mysql.coffee
+++ b/src/server/db/mysql.coffee
@@ -43,6 +43,16 @@ module.exports = MysqlDb = (options) ->
client = options.client or mysql.createConnection options
client.connect()
+ # Listen to the library's error event and take appropriate action
+ client.on 'error', (err) ->
+ console.error 'Application caught error from mysql driver : ' + err.code
+ if err.code is 'PROTOCOL_CONNECTION_LOST'
+ console.info 'Issuing a re-connect on the mysql connection.'
+ client.connect()
+ if err.code is 'ECONNREFUSED'
+ console.error 'Looks server is down. Not going to re-connect right now'
+
+
snapshot_table = options.schema and "#{options.schema}.#{options.snapshot_table}" or options.snapshot_table
operations_table = options.schema and "#{options.schema}.#{options.operations_table}" or options.operations_table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment