I make the implementation of two phase commit (2PC)
there are 2 phases - prepare, commit and there is a phase in case of rollback
communication - REST API (/prepare, /commit, /rollback endpoints)
according to the canons of 2pc in the /prepare phase, I need to lock a row in the database.
In the current implementation, I made the boolean field which called locked in each microservice in the participating tables and set the value to true in the first phase and false in the second phase.
I know that my implementation is very simple and not quite correct.
My question is: how is correctly to lock a row in tables in the first phase?
I've already tried:
select ... for update - but it will not work because it locks only at the level of one transaction, and I need to lock until the second phase
I was thinking about dbms_lock (database level lock) but I'm not entirely sure if this is the right solution.
My stack: Java, Spring Data, MySql
How is it correct to lock the row in the first phase so that it is held until the completion of the entire commit (second phase) in a distributed transaction.
P/S i know about SAGA PATTTERN and already implemented (for me now very interesting 2PC implementation)
Thanks a lot.