如何捕获 Sequelize 连接错误 [英] how to catch Sequelize connection error

查看:15
本文介绍了如何捕获 Sequelize 连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果出现续集连接错误,如何捕获?

How to catch a sequelize connection error in case there is one?

我试过了

var connection = new Sequelize("db://uri");
connection.on("error", function() { /* perhaps reconnect here */ });

但显然这不受支持.

我想这样做是因为我认为 sequelize 可能会偶尔抛出未处理的 ETIMEOUT 并使我的节点进程崩溃.

I wanted to do this because I think sequelize might be throwing an occasional unhandled ETIMEOUT and crashing my node process.

目前我正在使用 sequelize 连接一个 mysql 实例.我只需要 2-3 个小时,在此期间我会做很多读取查询.在此期间,mysql 服务器不会连接到任何其他设备.

Currently I am using sequelize to connect a mysql instance. I only need it for like 2-3 hours and during that time I will be doing a many read queries. The mysql server will not be connected to anything else during that time.

推荐答案

在使用外部迁移或数据库完整性至关重要的情况下使用 sync() 是相当危险的(什么时候不是!)

Using sync() for this is considerably dangerous when using external migrations or when database integrity is paramount (when isn't it!)

比较最新的方法是使用authenticate()

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

这篇关于如何捕获 Sequelize 连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆