【发布时间】:2022-07-29 15:13:23
【问题描述】:
我想做一个原子事务。我在我的存储库中使用 typeorm。而我的交易包含两个不同的实体。最好的方法是什么?
【问题讨论】:
-
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
标签: node.js postgresql transactions typeorm node.js-typeorm
我想做一个原子事务。我在我的存储库中使用 typeorm。而我的交易包含两个不同的实体。最好的方法是什么?
【问题讨论】:
标签: node.js postgresql transactions typeorm node.js-typeorm
在我看来更好的方法是 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();
}
【讨论】: