Using Zend Framework, I need to (1) read a record from a MySQL database, and (2) immediately write back to that record to indicate that it has been read. I don't want other processes or queries to be able to read from or write to the same record in between steps (1) and (2).
I was considering using a transaction for these steps. If I use the following methods, will that fulfil my requirements?
Zend_Db_Adapter_Abstract::beginTransaction()
Zend_Db_Adapter_Abstract::commit()
Zend_Db_Adapter_Abstract::rollBack()
Presupposing you are using the InnoDB engine for tables that you will issue transactions on:
If the requirement is that you first need to read the row and exclusively lock it, before you are going to update it, you should issue a
SELECT ... FOR UPDATE
query. Something like:Or simply issue 'raw'
SELECT ... FOR UPDATE
andUPDATE
SQL statements on$db
of course.