Strange behavior of transcations

From: Hui Jackson <jackhts4(at)gmail(dot)com>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Strange behavior of transcations
Date: 2022-06-05 11:10:01
Message-ID: CAHXAyjyT2zDt9Ozq-VnM80x7f3tm0yJt3uUaWCkR8bfLvfLwJA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I am working on node-postgres and there is a strange transactions.
The function aims at locking user's wallet until the refund process is
complete, then will update item's has_refund to true.
The problem I am facing is the program return no error, but the database is
not updated. Unless I spam the function for multiple times then the value
is updated.
If I remove begin and commit, then the function work perfectly.
const refundService = (itemId) =>{
await pgPool.query('BEGIN;');
const users = (await pgPool.query('SELECT * from app_user where
$1=ANY(purchase_list);', [ itemId ])).rows;
for(let i = 0; i < users.length; i++){
refund(users[i])
}
await pgPool.query('UPDATE item_lists SET has_refund = $1 where id =
$2;', [true, itemId ]);
await pgPool.query('COMMIT;');
}

const refund = (user) =>{
const refund = 10
await pgPool.query('UPDATE app_user SET wallet = wallet + $1', [refund
]);
}

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2022-06-05 14:29:27 Re: Strange behavior of transcations
Previous Message Hui Jackson 2022-05-26 10:00:49 Re: Using Transaction with if-else in prepared statement