The only portable way to achieve consistency between rooms and tags and making sure rooms are never returned after they had been deleted is locking them with SELECT FOR UPDATE.
However in some systems locking is a side effect of concurrency control, and you achieve the same results without specifying FOR UPDATE explicitly.
To solve this problem, Thread 1 should
SELECT id FROM rooms FOR UPDATE, thereby preventing Thread 2 from deleting fromroomsuntil Thread 1 is done. Is that correct?
This depends on the concurrency control your database system is using.
-
MyISAMinMySQL(and several other old systems) does lock the whole table for the duration of a query. -
In
SQL Server,SELECTqueries place shared locks on the records / pages / tables they have examined, whileDMLqueries place update locks (which later get promoted to exclusive or demoted to shared locks). Exclusive locks are incompatible with shared locks, so eitherSELECTorDELETEquery will lock until another session commits. -
In databases which use
MVCC(likeOracle,PostgreSQL,MySQLwithInnoDB), aDMLquery creates a copy of the record (in one or another way) and generally readers do not block writers and vice versa. For these databases, aSELECT FOR UPDATEwould come handy: it would lock eitherSELECTor theDELETEquery until another session commits, just asSQL Serverdoes.
When should one use
REPEATABLE_READtransaction isolation versusREAD_COMMITTEDwithSELECT ... FOR UPDATE?
Generally, REPEATABLE READ does not forbid phantom rows (rows that appeared or disappeared in another transaction, rather than being modified)
-
In
Oracleand earlierPostgreSQLversions,REPEATABLE READis actually a synonym forSERIALIZABLE. Basically, this means that the transaction does not see changes made after it has started. So in this setup, the lastThread 1query will return the room as if it has never been deleted (which may or may not be what you wanted). If you don’t want to show the rooms after they have been deleted, you should lock the rows withSELECT FOR UPDATE -
In
InnoDB,REPEATABLE READandSERIALIZABLEare different things: readers inSERIALIZABLEmode set next-key locks on the records they evaluate, effectively preventing the concurrentDMLon them. So you don’t need aSELECT FOR UPDATEin serializable mode, but do need them inREPEATABLE READorREAD COMMITED.
Note that the standard on isolation modes does prescribe that you don’t see certain quirks in your queries but does not define how (with locking or with MVCC or otherwise).
When I say “you don’t need SELECT FOR UPDATE” I really should have added “because of side effects of certain database engine implementation”.