Description
My project env:
1.laravel 5.7.6
2.mysql 8.0.12 The transaction level RR
3.php 7.1.22
Description :
The problem is the first transaction is success execute。
second and three transaction is rollback,but they insert
the order table record still in database。
I don't know why the transaction rolled back the data is still there。
logging:
I use three threads to simulate ordering,
this is simplified version my log。
[2018-10-03 17:44:13] dev.INFO: php-thread-id-32993 transaction start
[2018-10-03 17:44:13] dev.INFO: php-thread-id-33136 transaction start
[2018-10-03 17:44:13] dev.INFO: {"sql":"select * from
h8_finance_account_logs
whereuser_id
= ? order byid
desc limit 1 for update","bindings":[1010001],"time":0.59,"connection":{},"connectionName":"mysql"} (X-LOCK the record)
[2018-10-03 17:44:13] dev.INFO: 32993 GET X-LOCK
[2018-10-03 17:44:13] dev.INFO: php-thread-id-32643 transaction start
... do business (like insert order table)
[2018-10-03 17:44:13] dev.INFO: {"sql":"insert into
h8_finance_account_logs
(user_id
),"bindings":[1010001],"time":0.67,"connection":{},"connectionName":"mysql"} (insert new log)
[2018-10-03 17:44:13] dev.INFO: {"sql":"select * fromh8_finance_account_logs
whereuser_id
= ? order byid
desc limit 1 for update","bindings":[1010001],"time":0.59,"connection":{},"connectionName":"mysql"} (X-LOCK the record)
[2018-10-03 17:44:13] dev.INFO: {"sql":"select * from
h8_finance_account_logs
whereuser_id
= ? order byid
desc limit 1 for update","bindings":[1010001],"time":0.59,"connection":{},"connectionName":"mysql"} (X-LOCK the record)
[2018-10-03 17:44:13] dev.INFO: 32643 GET X-LOCK
[2018-10-03 17:44:13] dev.INFO: 33136 GET X-LOCK
[2018-10-03 17:44:13] dev.INFO: {"sql":"insert into
h8_finance_account_logs
(user_id
)[1010001],"time":0.34,"connection":{},"connectionName":"mysql"}
(thread 32993 insert second record)
[2018-10-03 17:44:13] dev.INFO: 32993 commit success
At this point transaction 1 has completed the commit,
next second and three transaction will be
... do business (like insert order table)
But they have no balance to pay the order,so they will be rollback。
[2018-10-03 17:44:13] dev.INFO: php-thread-id-33136 rollback
[2018-10-03 17:44:13] dev.INFO: php-thread-id-32643 rollback
All three processes perform the same logic
1.Place the order
2.pay
3.commit or rollback