【问题标题】:Whats the difference between using query runner and DataSource for a transaction?使用查询运行器和数据源进行事务有什么区别?
【发布时间】:2022-07-29 15:13:23
【问题描述】:

我想做一个原子事务。我在我的存储库中使用 typeorm。而我的交易包含两个不同的实体。最好的方法是什么?

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: node.js postgresql transactions typeorm node.js-typeorm


【解决方案1】:

在我看来更好的方法是 queryRunner。

我使用这个模板在一个事务中完成所有操作。我让 Postgresql 和 queryRunner 从池中创建一个连接。如果您的池子数量较少,则由开发人员管理更多。

const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
  await queryRunner.commitTransaction();
  return response;
}
except(err) {
   await queryRunner.rollbackTransaction();
  throw new InternalServerErrorException(err);
}
finally {
  await queryRunner.release();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多