IFThenElse
IFThenElse

Reputation: 228

Sequelize with MariaDB timeout errors

In my NodeJS app I use Sequelize to work with my MariaDB. Very often I get timeout error where Sequelize cannot connect the database. I think this is a normal behavior but I want to react on such errors, like with restart the NodeJS Server.

So my question is, anybody knows how to catch this kind of error?

text: "Connection timeout: failed to create socket after 1002ms",
sql: null,
fatal: true,
errno: 45012
sqlState: "08S01"
code "ERR_CONNECTION_TIMEOUT"

This error comes in red and the NodeJS app is killed.

I use this to connect to the database:

const Sequelize = require("sequelize");

const sequelize = new Sequelize("xxx", "xxx", "xxx", {
  host: "xxxx",
  dialect: "mariadb",
  pool: {
    max: 15,
    min: 0,
    idle: 10000,
  },
  logging: false,
  timezone: "+01:00",
});

sequelize
  .authenticate()
  .then(() => {
    console.log("Connection to database has been established successfully.");
  })
  .catch((err) => {
    console.error("Unable to connect to database:", err);
  });

module.exports = sequelize;

Any idea how to prvent this killed NodeJS instance? I also use pm2

Note: This is not a permanent problem with my NodeJS and MAriaDB. This happens randomly. Most time it works 300000 times well and then I got this error. A direct restart of the NodeJS will let it run again until this error occours.

Upvotes: 2

Views: 2181

Answers (0)

Related Questions