TypeORM:如何在queryRunner中运行SELECT ... FOR UPDATE?

时间:2019-09-11 04:01:04

标签: typeorm

如何在SELECT... FOR UPDATE内运行queryRunner? 我看到queryRunner只能访问manager的TypeORM文档,以及如何访问Repository来锁定记录。例如queryRunner.getRepository(User).setLock("pessimistic_write").....

      console.log("----------------- START TRANSACTION -----------------");

      const queryRunner = connection.createQueryRunner();
      // establish real database connection using our new query runner
      await queryRunner.connect();

      await queryRunner.startTransaction();

      try {
        // `SELECT....FOR UPDATE`
        // Want to .setLock("pessimistic_write")


        // commit transaction now:
        await queryRunner.commitTransaction();
      } catch (err) {
        // since we have errors lets rollback changes we made
        await queryRunner.rollbackTransaction();
      } finally {
        // you need to release query runner which is manually created:
        await queryRunner.release();
      }

      console.log("----------------- FINISH TRANSACTION -----------------");

欢迎任何建议!

1 个答案:

答案 0 :(得分:0)

是的,实际上我们可以从Repository访问manager,问题已解决?

        users = await queryRunner.manager
          .getRepository(User)
          .createQueryBuilder("user")
          .useTransaction(true)
          .setLock("pessimistic_write")
          .where("user.status = :status", { status: status })
          .getMany();