I've understood how both these concurrency controls work in plain English. However I was more interested how pessimistic control must be done in code. Here is what I feel, let's assume two users are trying to update a wiki document
Pessimistic control
Here, we are told to make use of transactions.
BEGIN
SELECT DOC FROM WIKI WHERE DOC_ID = 1;
/* business logic */
UPDATE WIKI SET DOC = INPUT WHERE DOC_ID = 1;
END
However this is still prone to overwriting the previous updates. I feel there has to be a second check within the transaction to see if any writes have happened after the select statement, if yes rollback or else commit. Am I correct?