After executing the UPDATE statement, the effect of the statement will become visible to the rest of the transaction (and if you complete the transaction with other transactions). The order in which Oracle physically does this is an implementation detail (similarly, the order of the SELECT result is not guaranteed unless you specify ORDER BY).
In most cases, this order does not matter to the customer. In one case, when possible, you should avoid deadlocks with another transaction that updates an overlapping rowset. UPDATE will lock the updated row until the end of the transaction, therefore, if two transactions try to lock the same rows, but in a different order, a deadlock may occur.
The standard way to avoid deadlocks is to always lock in a specific order. Unfortunately, UPDATE does not have an ORDER BY clause, but you can do this:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SELECT ... WHERE condition ORDER BY ... FOR UPDATE; UPDATE ... WHERE condition; COMMIT;
Where condition same for both operators. A serializable isolation level is necessary for WHERE to always see the same rowset in both statements.
Or, in PL / SQL, you can do something like this:
DECLARE CURSOR CUR IS SELECT * FROM YOUR_TABLE WHERE condition ORDER BY ... FOR UPDATE; BEGIN FOR LOCKED_ROW IN CUR LOOP UPDATE YOUR_TABLE SET ... WHERE CURRENT OF CUR; END LOOP; END; /
Branko dimitrijevic
source share