如何在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 -----------------");
欢迎任何建议!
答案 0 :(得分:0)
是的,实际上我们可以从Repository
访问manager
,问题已解决?
users = await queryRunner.manager
.getRepository(User)
.createQueryBuilder("user")
.useTransaction(true)
.setLock("pessimistic_write")
.where("user.status = :status", { status: status })
.getMany();