public inbox for [email protected]  
help / color / mirror / Atom feed
From: Hui Jackson <[email protected]>
To: [email protected]
Subject: Strange behavior of transcations
Date: Sun, 5 Jun 2022 19:10:01 +0800
Message-ID: <CAHXAyjyT2zDt9Ozq-VnM80x7f3tm0yJt3uUaWCkR8bfLvfLwJA@mail.gmail.com> (raw)

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
]);
}


view thread (3+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Strange behavior of transcations
  In-Reply-To: <CAHXAyjyT2zDt9Ozq-VnM80x7f3tm0yJt3uUaWCkR8bfLvfLwJA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox