Skip to content

MySQL Connection Error Handling #1012

Closed

Description

@0xception

Hello,

I've run into a potential issue and I'm not 100% sure if it's a bug with the mysql connection manager or something I'm doing.

When triggering a connection failure on sequelize.sync() the error events do not seem to propagate up out of the connection-manager.js I've attached an event listener using the .error() method on the sequelize.sync() which works perfectly with sqlite. But when using mysql the callback is never triggered.

var Sequelize = require("sequelize");
var sequelize = new Sequelize(database, user, pass, _opts);

// Defined Models

sequelize.sync().success(function() {
    console.log("Models Synced");
}).error(function(err) {
    console.log("Error: OOOH NOOOES");
});

The above will generate the following error

TypeError: Uncaught, unspecified "error" event.
    at TypeError (<anonymous>)
    at EventEmitter.emit (events.js:74:15)
    at Handshake._callback (./node_modules/sequelize/lib/dialects/mysql/connector-manager.js:278:19)
    at Handshake.Sequence.end (./node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
    at Protocol.handleNetworkError (./node_modules/mysql/lib/protocol/Protocol.js:238:14)
    at Connection._handleNetworkError (./node_modules/mysql/lib/Connection.js:155:18)
    at Socket.EventEmitter.emit (events.js:117:20)
    at net.js:830:16
    at process._tickCallback (node.js:415:13)
22 Oct 23:49:29 - [nodemon] app crashed - waiting for file changes before starting...

Activity

  1. m4tm4t commented on Dec 23, 2013

    @m4tm4t

    Same here but with find method. I tried to shutdown mysql and the app is crashing. (2.0.0-beta.2)

    User.find
      where:
        name: username
      limit: 1
    .success (user) ->
      console.log user
      done(null, user)
    .error (err) ->
      console.log err
      done(null, err)
    Executing (default): SELECT * FROM `users` WHERE `users`.`name`='foo' LIMIT 1;
    { [Error: connect ECONNREFUSED]
      code: 'ECONNREFUSED',
      errno: 'ECONNREFUSED',
      syscall: 'connect',
      fatal: true }
    
    TypeError: Uncaught, unspecified "error" event.
      at TypeError (<anonymous>:null:null)
      at EventEmitter.emit (events.js:74:15)
      at Handshake._callback (/home/foohey/dev/nodejs/awale/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:283:19)
      at Handshake.Sequence.end (/home/foohey/dev/nodejs/awale/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
      at Protocol.handleNetworkError (/home/foohey/dev/nodejs/awale/node_modules/mysql/lib/protocol/Protocol.js:255:14)
      at Connection._handleNetworkError (/home/foohey/dev/nodejs/awale/node_modules/mysql/lib/Connection.js:187:18)
      at Socket.EventEmitter.emit (events.js:117:20)
      at net.js:441:14
      at process._tickCallback (node.js:415:13)
    

    and try/ctach doesn't help

    EDIT

    I just realized that there is a mariadb dialect & mariasql lib. Now everything work good

  2. mickhansen commented on Dec 23, 2013

    @mickhansen
    Contributor

    TypeError: Uncaught, unspecified "error" event. is thrown when you forget to attach an error listener to an emitter (i.e. you are only listening to success).

    Are you calling another method, perhaps sync, where you aren't attaching an error listener?

  3. m4tm4t commented on Dec 23, 2013

    @m4tm4t

    But look my code, there is an error listener, and the console.log(err) is working too

  4. mickhansen commented on Dec 23, 2013

    @mickhansen
    Contributor

    Ah, you're right - What are your connection settings? (Pooling etc)

  5. m4tm4t commented on Dec 23, 2013

    @m4tm4t
    sequelize = new Sequelize(CONFIG.database.dbname, CONFIG.database.user, CONFIG.database.password,
      host: CONFIG.database.host
      logging: CONFIG.database.logging
      pool:
        maxConnections: 5
        maxIdleTime: 30
    )
  6. SkPhilipp commented on Jan 4, 2014

    @SkPhilipp

    ( sequelize 1.7.0-beta.4a, mysql 2.0.0-rc2 , node 0.10, windows )

    Experienced the same issue when referencing a database that does not exist in MySQL.

    My code:

    var sequelize = new Sequelize('mysql://root:root@localhost/demo', {
    pool: { maxConnections: 1000, maxIdleTime: 5 },
        logging: false
    });
    

    ( After that i also call a bunch of sequelize#define followed by sequelize#sync. )

    Didn't debug further, creating the database demo solved the issue for me.

  7. acoll commented on Jan 8, 2014

    @acoll

    I'm getting the same error. The stack trace pointed me to this code https://github.com/sequelize/sequelize/blob/master/lib/dialects/mysql/connector-manager.js#L260.

    Notice that the emitter isn't accessible but emits the error event. So you can't actually add a listener for it.

  8. sdepold commented on Jan 8, 2014

    @sdepold
    Member

    Hi guys. Thanks for reporting the issue. This is a known issue. The major issue is, that we don't return the emitter to the outer world and because of that its not possible to catch the error. It will be fixed in the final 1.7.0.

  9. ghost assigned on Jan 8, 2014
  10. stigmat4j commented on Jan 13, 2014

    @stigmat4j

    Hi. I get the same error only on debian. But on windows everything is ok.

    node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
    ^
    Error: Uncaught, unspecified 'error' event.
    at EventEmitter.emit (events.js:50:15)
    at Handshake._callback (/home/er1234/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:283:19)
    at Handshake.end (/home/er1234/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
    at Protocol.handleNetworkError (/home/er1234/node_modules/mysql/lib/protocol/Protocol.js:261:14)
    at Connection._handleNetworkError (/home/er1234/node_modules/mysql/lib/Connection.js:187:18)
    at Socket. (native)
    at Socket.emit (events.js:88:20)
    at Array.0 (net.js:319:25)
    at EventEmitter._tickCallback (node.js:192:40)

  11. sdepold commented on Jan 20, 2014

    @sdepold
    Member

    Will take care of that during the week.

  12. mickhansen commented on Jan 22, 2014

    @mickhansen
    Contributor

    Should be fixed by: #1282

  13. mickhansen commented on Jan 22, 2014

    @mickhansen
    Contributor

    Please re-open if not fixed by PR.

  14. msjahid commented on Jun 27, 2022

    @msjahid
    const { Sequelize } = require('sequelize');
    
    const sequelize = new Sequelize('database', 'name', 'password', {
        host: '127.0.0.1',  // or host: 'localhost'
        dialect: 'mysql',
        port: 5043, // your port 
        dialectOptions: { // optional if you have xampp or port borken
            socketPath: "/opt/lampp/var/mysql/mysql.sock"
        },
    })
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions