【问题标题】:Sequelize transactioons with stored procedures使用存储过程对事务进行 Sequelize
【发布时间】:2020-06-22 03:46:04
【问题描述】:

我遇到了一个奇怪的问题,我需要一些帮助。

我在将数据插入多个表的场景中使用事务。

我在 mysl 数据库中有一个存储过程,其中发生了以下情况:

第 1 步:插入表 1 --- 成功

步骤 2:插入表 2 ---成功

第 3 步 INSERT INTO TABLE3 --- 失败

奇怪的是,当通过 sequlize 发生回滚时,它只发生在 TABLE3 上,而不发生在前两个插入上。

代码:

export const executor = async (query: Function, db: any) => {
  try {
    const result = await db.sequelize.transaction(async t => {
      const resp = await query(t);

      return resp;
    });

    // If the execution reaches this line, the transaction has been committed successfully
    // `result` is whatever was returned from the transaction callback (the `user`, in this case)
    return result;
  } catch (error) {
    // If the execution reaches this line, an error occurred.
    // The transaction has already been rolled back automatically by Sequelize!

    // log here

    console.log('ERROR', error);

    throw new Error('Internal server error');
  }
};

我这样调用我的存储过程:

const r = await executor(
  param =>
    db.sequelize.query(
      'CALL registerUser(:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguageId, :assigned, :expId, :imageId, :tableId, :position)',
      {
        replacements: {
          email: args.input.Email,
          password: PasswordHash,
          roleId: 1,
          firstName: args.input.FirstName,
          lastName: args.input.LastName,
          age: new Date(new Date(args.input.Age).toUTCString()),
          jobTitle: args.input.JobTitle,
          prefLanguageId: 1,
          assigned: false,
          expId: '9b42b3d0-b227-11ea-b63f-9746e0754cfe',
          imageId,
          tableId: null,
          position: null,
        },
      },
      { transaction: param }
    ),
  db
    );

正如我上面提到的,存储过程中的一个表中的INSERT 失败,这是自我测试以来的设计,但我希望在回滚时删除所有已插入所有表的数据调用。

我想错了吗?我可以在 proc 本身中设置一个事务,但有点违背了使用 sequelize 的目的。

【问题讨论】:

    标签: mysql sequelize.js


    【解决方案1】:

    您使用transaction 属性作为第三个参数传递了一个对象,但您应该在第二个参数中将它传递到replacements 属性旁边:

    db.sequelize.query(
          'CALL registerUser(:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguageId, :assigned, :expId, :imageId, :tableId, :position)',
          {
            replacements: {
              email: args.input.Email,
              password: PasswordHash,
              roleId: 1,
              firstName: args.input.FirstName,
              lastName: args.input.LastName,
              age: new Date(new Date(args.input.Age).toUTCString()),
              jobTitle: args.input.JobTitle,
              prefLanguageId: 1,
              assigned: false,
              expId: '9b42b3d0-b227-11ea-b63f-9746e0754cfe',
              imageId,
              tableId: null,
              position: null,
            },
            transaction: param
          })```
    

    【讨论】:

      猜你喜欢
      • 2018-02-09
      • 1970-01-01
      • 2011-09-09
      • 2021-05-19
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2019-06-25
      相关资源
      最近更新 更多