提问人:Han 提问时间:7/5/2022 更新时间:7/8/2022 访问量:239
我还能在交易中使用 Sequelize V6 上的 BlueBird 调用吗
Can I still use BlueBird calls on Sequelize V6 in a transaction
问:
对于我从 sequelize v5 --> v6 迁移的项目
如此处所述,现在应该使用而不是 .所以我现在使用 .另一个重大变化是 BlueBird 已被原生 Promise 取代。
但是,我有很多代码包装了仍然使用 BlueBird 的 Sequelize 调用(在事务的生命周期内),因为它有很多方便的辅助方法,而原生 Promise 缺乏这些方法。cls-hooked
continuation-local-storage
cls-hooked
但是,现在有时(但不是始终如一)Sequelize 会提到这样的奇怪错误:.从逻辑上讲,这对我来说没有意义,因为查询应该在提交事务之前已经完成(并且在使用 时工作正常)。如果我在该特定呼叫中将BlueBird与Promise交换,则无法再重现该问题。Error, error stacktrace: Error: commit has been called on this transaction(83e3c09b-efd4-4a32-bc1e-2f009a20b9c0), you can no longer use it. (The rejected query is attached as the 'sql' property of this error)
continuation-local-storage
使用 sequelize V5 时,我使用 cls-bluebird 通过修补 continuation-local-storage 命名空间来解决这些类似的问题:
import * as cls from 'continuation-local-storage'
....
const clsBluebird = require('cls-bluebird');
clsBluebird(nameSpace);
但是,是专门为(参见 cls-bluebird 的自述文件)而设计的:cls-bluebird
continuation-local-storage
修补 bluebird 以获得 continuation-local-storage 支持。
在 Sequelize V6 中,可以像这样修补 cls 挂钩的命名空间:
const cls = require('cls-hooked');
const nameSpace = cls.createNamespace("some-constant");
const clsBluebird = require('cls-bluebird');
clsBluebird(nameSpace);
有趣的是,这修复了我的“ 提交已被调用...“问题,但我怀疑让修补不受支持的库是否可靠。有人可以验证吗?cls-bluebird
cls-hooked
如果没有,我想我唯一的选择是用原生 Promise 替换我所有的 BlueBird 调用?
答:
我在 cls-hooked repo 中发现了这个问题,作者似乎建议 cls-bluebird 确实适用于 cls-hooked。
快速回答:是的,你会失去未打补丁的蓝鸟的上下文。 看看“cls-bluebird”https://github.com/TimBeyer/cls-bluebird。
评论