I want to implement database row wise lock in PostgreSQL with Nestjs and TypeORM. Could you please any one let me know how to implement it in correct way. I tried with the following code but not sure whether it is locking the particular row.
async testFun(): Promise<any> {
const user = await this.userRepo
.createQueryBuilder("user")
.useTransaction(true)
.setLock("pessimistic_write")
.where("id = :id", { id: 2 })
.getOne()
const updateResult: any = await this.userRepo.createQueryBuilder('test')
.update()
.set({ fullname: "frm3000" })
.where("id = :id", { id: 2 })
.execute();
return updateResult;
}
Query generated by TypeORM is,
query: START TRANSACTION
query: SELECT * FROM "users" "user" WHERE id = $1 FOR UPDATE -- PARAMETERS: [2]
query: COMMIT
query: UPDATE "users" SET "fullname" = $1 WHERE "id" = $2 -- PARAMETERS: ["from3000",2]
Here commit done before update query, so doubted whether the row locked or not.
Also I want to test it in my local host, with two different Ports localhost:3000 and localhost:3001. Hence I added same controller and service code in another app with port 3001 with delay,
async testFun(): Promise<any> {
const user = await this.userRepo
.createQueryBuilder("user")
.useTransaction(true)
.setLock("pessimistic_write")
.where("id = :id", { id: 2 })
.getOne()
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
await sleep(20000);
const updateResult: any = await this.userRepo.createQueryBuilder('test')
.update()
.set({ fullname: "frm3001" })
.where("id = :id", { id: 2 })
.execute();
return updateResult;
}
Please let me know is this approach correct or suggest better way if any. Also the testing approach too not working.
Thanks!