Understanding Transactions

Asked 2 years ago, Updated 2 years ago, 42 views


to verify the inconsistency of transaction processing. In session A, the price is 1000 yen plus 1000 yen to 2000 yen, and
After that, I will try to increase the price to 1000 yen and 5000 yen for the B session to confirm that it is not consistent.

According to my prediction, it should have been 6000 yen for B session, but when B session is updated, it becomes wait and A session B session moves correctly and becomes 7000 yen.
In that case, I thought only transactions would be fine.
I thought it was necessary to lock the line of the transaction...

sql postgresql

2022-09-30 10:25

1 Answers

I believe that the transaction was carried out at the default Read Committed isolation level.

https://www.postgresql.jp/document/13/html/transaction-iso.html#XACT-READ-COMMITTED

As you can see here, the behavior of the update at Read Committed is as follows:

As a result of this behavior, an excerpt from the above document says that if the update is written using the original value as shown below, this will be the result as intended.

BEGIN;
UPDATE accounts SET balance=balance+100.00 WHERE acctnum=12345;
UPDATE accounts SET balance=balance-100.00 WHERE acctnum=7534;
COMMIT;


2022-09-30 10:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.