Only theSELECT INTO,OPEN,FETCH,CLOSE,LOCK TABLE,COMMIT, andROLLBACK statements are allowed in a read-only transaction. Queries cannot beFOR UPDATE.
Overriding Default Locking
By default, Oracle locks data structures for you automatically, which is a major strength of the Oracle database: different applications can read and write to the same data without harming each other's data or coordinating with each other.
You can request data locks on specific rows or entire tables if you need to override default locking. Explicit locking lets you deny access to data for the duration of a transaction.:
■ With theLOCK TABLE statement, you can explicitly lock entire tables.
■ With theSELECT FOR UPDATEstatement, you can explicitly lock specific rows of a table to make sure they do not change after you have read them. That way, you can check which or how many rows will be affected by an UPDATE or DELETE statement before issuing the statement, and no other application can change the rows in the meantime.
Overview of Transaction Processing in PL/SQL
Using FOR UPDATE
When you declare a cursor that will be referenced in theCURRENT OF clause of an UPDATE orDELETE statement, you must use theFOR UPDATE clause to acquire exclusive row locks. An example follows:
DECLARE
CURSOR c1 IS SELECT empno, sal FROM emp WHERE job = 'SALESMAN' AND comm > sal FOR UPDATE NOWAIT;
TheSELECT ...FOR UPDATE statement identifies the rows that will be updated or deleted, then locks each row in the result set. This is useful when you want to base an update on the existing values in a row. In that case, you must make sure the row is not changed by another user before the update.
The optional keywordNOWAIT tells Oracle not to wait if requested rows have been locked by another user. Control is immediately returned to your program so that it can do other work before trying again to acquire the lock. If you omit the keyword
NOWAIT, Oracle waits until the rows are available.
All rows are locked when you open the cursor, not as they are fetched. The rows are unlocked when you commit or roll back the transaction. Since the rows are no longer locked, you cannot fetch from aFOR UPDATE cursor after a commit. (For a
workaround, see"Fetching Across Commits" on page 6-34.)
When querying multiple tables, you can use theFOR UPDATE clause to confine row locking to particular tables. Rows in a table are locked only if theFOR UPDATE OF clause refers to a column in that table. For example, the following query locks rows in theemp table but not in thedept table:
DECLARE
CURSOR c1 IS SELECT ename, dname FROM emp, dept WHERE emp.deptno = dept.deptno AND job = 'MANAGER' FOR UPDATE OF sal;
As the next example shows, you use theCURRENT OF clause in anUPDATE orDELETE statement to refer to the latest row fetched from a cursor:
DECLARE
CURSOR c1 IS SELECT empno, job, sal FROM emp FOR UPDATE; BEGIN
OPEN c1; LOOP
FETCH c1 INTO ...
UPDATE emp SET sal = new_sal WHERE CURRENT OF c1; END LOOP;
Using LOCK TABLE
You use theLOCK TABLE statement to lock entire database tables in a specified lock mode so that you can share or deny access to them.. Row share locks allow concurrent access to a table; they prevent other users from locking the entire table for exclusive use. Table locks are released when your transaction issues a commit or rollback. LOCK TABLE emp IN ROW SHARE MODE NOWAIT;
The lock mode determines what other locks can be placed on the table. For example, many users can acquire row share locks on a table at the same time, but only one user at a time can acquire an exclusive lock. While one user has an exclusive lock on a table, no other users can insert, delete, or update rows in that table. For more information
Overview of Transaction Processing in PL/SQL
about lock modes, seeOracle Database Application Developer's Guide - Fundamentals.
A table lock never keeps other users from querying a table, and a query never acquires a table lock. Only if two different transactions try to modify the same row will one transaction wait for the other to complete.
Fetching Across Commits
PL/SQL raises an exception if you try to fetch from aFOR UPDATEcursor after doing a commit. TheFOR UPDATE clause locks the rows when you open the cursor, and unlocks them when you commit.
DECLARE
CURSOR c1 IS SELECT ename FROM emp FOR UPDATE OF sal; BEGIN
FOR emp_rec IN c1 LOOP -- FETCH fails on the second iteration INSERT INTO temp VALUES ('still going');
COMMIT; -- releases locks END LOOP;
END;
If you want to fetch across commits, use theROWID pseudocolumn to mimic the CURRENT OF clause. Select the rowid of each row into aUROWID variable, then use the rowid to identify the current row during subsequent updates and deletes:
DECLARE
CURSOR c1 IS SELECT ename, job, rowid FROM emp; my_ename emp.ename%TYPE; my_job emp.job%TYPE; my_rowid UROWID; BEGIN OPEN c1; LOOP
FETCH c1 INTO my_ename, my_job, my_rowid; EXIT WHEN c1%NOTFOUND;
UPDATE emp SET sal = sal * 1.05 WHERE rowid = my_rowid; -- this mimics WHERE CURRENT OF c1
COMMIT; END LOOP; CLOSE c1; END;
Because the fetched rows arenot locked by aFOR UPDATE clause, other users might unintentionally overwrite your changes. The extra space needed for read consistency is not released until the cursor is closed, which can slow down processing for large updates.
The next example shows that you can use the%ROWTYPE attribute with cursors that reference theROWID pseudocolumn:
DECLARE
CURSOR c1 IS SELECT ename, sal, rowid FROM emp; emp_rec c1%ROWTYPE;
BEGIN OPEN c1; LOOP
FETCH c1 INTO emp_rec; EXIT WHEN c1%NOTFOUND; IF ... THEN