首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在后缀中执行事务和回滚?

如何在后缀中执行事务和回滚?
EN

Stack Overflow用户
提问于 2020-05-12 14:46:54
回答 1查看 922关注 0票数 1

文件中说:

代码语言:javascript
运行
复制
try {

  const result = await sequelize.transaction(async (t) => {

    const user = await User.create({
      firstName: 'Abraham',
      lastName: 'Lincoln'
    }, { transaction: t });

    await user.setShooter({
      firstName: 'John',
      lastName: 'Boothe'
    }, { transaction: t });

    return user;

  });

  // 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)

} catch (error) {

  // If the execution reaches this line, an error occurred.
  // The transaction has already been rolled back automatically by Sequelize!

}

我想为此创建一个包装器,并想出如下所示的函数:

代码语言:javascript
运行
复制
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

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

像这样执行:

代码语言:javascript
运行
复制
const r = executor(param => {
  db.sequelize.query(
    'CALL registerSportUser (:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguaged, :assigned, :sportId, :tableId, :position, :image, :imageName)',
    {
      replacements: {
        email: args.input.Email,
        password: PasswordHash,
        roleId: args.input.RoleId,
        firstName: args.input.FirstName,
        lastName: args.input.LastName,
        age: new Date(new Date(args.input.Age).toUTCString()),
        jobTitle: args.input.JobTitle,
        prefLanguaged: args.input.PrefLanguaged,
        assigned: false,
        sportId: '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab',
        tableId: null,
        position: null,
        image: null,
        imageName: null,
      },
    },
    { transaction: param }
  );
}, db);

为了测试这一点,我发送了一个NULL值,该值被插入到不允许空的表colum中。然后,这将回滚我所有的db更改。

问题是它不会回滚任何东西..。

控制台显示:

代码语言:javascript
运行
复制
Executing (6d1aaebb-5247-4b0d-a9c4-d5b81a6da8db): START TRANSACTION;
Executing (6d1aaebb-5247-4b0d-a9c4-d5b81a6da8db): COMMIT;
Executing (default): CALL registerSportUser('email', '$2b$12$i1mc.tLpG0HjhK0Y9C/gTO1ySwBguOa2Tlr1fFZHUf1IgIKESU3gC', 1, 'firstname', 'lastName', '1988-07-17 02:00:00', 'Jobtitle', 1, false, '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab', NULL, NULL, NULL, NULL)

错误:

代码语言:javascript
运行
复制
Unhandled rejection SequelizeDatabaseError: Column 'TabelId' cannot be null

这个错误是有效的,因为im试图插入一个空值,其中不应该有空值,但是为什么不对回滚所有的更改进行后缀呢?

而不是我应该得到的错误:

代码语言:javascript
运行
复制
Executing (e5fb0b6b-1340-4f9f-9bf0-f8913ba54751): ROLLBACK;

我认为我的包装是问题,但我不知道在哪里。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-12 15:09:44

你忘了回报db.sequelize.query的承诺

代码语言:javascript
运行
复制
const r = executor(param => {
  return db.sequelize.query(
    'CALL registerSportUser (:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguaged, :assigned, :sportId, :tableId, :position, :image, :imageName)',
    {
      replacements: {
        email: args.input.Email,
        password: PasswordHash,
        roleId: args.input.RoleId,
        firstName: args.input.FirstName,
        lastName: args.input.LastName,
        age: new Date(new Date(args.input.Age).toUTCString()),
        jobTitle: args.input.JobTitle,
        prefLanguaged: args.input.PrefLanguaged,
        assigned: false,
        sportId: '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab',
        tableId: null,
        position: null,
        image: null,
        imageName: null,
      },
    },
    { transaction: param }
  );
}, db);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61754750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档