Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 6 ■ LOCKING AND LATCHING 217 USERNAME SID ID1 ID2 LMODE REQUEST BLOCK TYPE --------- ---- ------- ------ ----- ------- ----- ---- OPS$TKYTE 161 262151 16584 6 0 0 TX OPS$TKYTE 161 62074 0 3 0 0 TM OPS$TKYTE 161 62073 0 3 0 0 TM ops$tkyte@ORA10G> select object_name, object_id 2 from user_objects 3 where object_name in ('T1','T2') 4 / OBJECT_NAME OBJECT_ID ------------ ---------- T1 62073 T2 62074 Whereas we get only one TX lock per transaction, we can get as many TM locks as the objects we modify. Here, the interesting thing is that the ID1 column for the TM lock is the object ID of the DML-locked object, so it easy to find the object on which the lock is being held. An interesting aside to the TM lock: the total number of TM locks allowed in the system is configurable by you (for details, see the DML_LOCKS parameter definition in the Oracle Database Reference manual). It may in fact be set to zero. This does not mean that your database becomes a read-only database (no locks), but rather that DDL is not permitted. This is useful in very specialized applications, such as RAC implementations, to reduce the amount of intrainstance coordination that would otherwise take place. You can also remove the ability to gain TM locks on an object-by-object basis using the ALTER TABLE TABLENAME DISABLE TABLE LOCK command. This is a quick way to make it “harder” to accidentally drop a table, as you will have to re-enable the table lock before dropping the table. It can also be used to detect a full table lock as a result of the unindexed foreign key we discussed previously. DDL Locks DDL locks are automatically placed against objects during a DDL operation to protect them from changes by other sessions. For example, if I perform the DDL operation ALTERTABLE T, the table T will have an exclusive DDL lock placed against it, preventing other sessions from getting DDL locks and TM locks on this table. DDL locks are held for the duration of the DDL statement and are released immediately afterward. This is done, in effect, by always wrapping DDL statements in implicit commits (or a commit/rollback pair). For this reason, DDL always commits in Oracle. Every CREATE, ALTER, and so on statement is really executed as shown in this pseudo-code: Begin Commit; DDL-STATEMENT Commit; Exception When others then rollback; End;

218 CHAPTER 6 ■ LOCKING AND LATCHING So, DDL will always commit, even if it is unsuccessful. DDL starts by committing—be aware of this. It commits first so that if it has to roll back, it will not roll back your transaction. If you execute DDL, it will make permanent any outstanding work you have performed, even if the DDL is not successful. If you need to execute DDL, but you do not want it to commit your existing transaction, you may use an autonomous transaction. There are three types of DDL locks: • Exclusive DDL locks: These prevent other sessions from gaining a DDL lock or TM (DML) lock themselves. This means that you may query a table during a DDL operation, but you may not modify it in any way. • Share DDL locks: These protect the structure of the referenced object against modification by other sessions, but allow modifications to the data. • Breakable parse locks: These allow an object, such as a query plan cached in the Shared pool, to register its reliance on some other object. If you perform DDL against that object, Oracle will review the list of objects that have registered their dependence and invalidate them. Hence, these locks are “breakable”—they do not prevent the DDL from occurring. Most DDL takes an exclusive DDL lock. If you issue a statement such as Alter table t add new_column date; the table T will be unavailable for modifications during the execution of that statement. The table may be queried using SELECT during this time, but most other operations will be prevented, including all DDL statements. In Oracle, some DDL operations may now take place without DDL locks. For example, I can issue the following: create index t_idx on t(x) ONLINE; The ONLINE keyword modifies the method by which the index is actually built. Instead of taking an exclusive DDL lock, preventing modifications of data, Oracle will only attempt to acquire a low-level (mode 2) TM lock on the table. This will effectively prevent other DDL from taking place, but it will allow DML to occur normally. Oracle accomplishes this feat by keeping a record of modifications made to the table during the DDL statement and applying these changes to the new index as it finishes the CREATE. This greatly increases the availability of data. Other types of DDL take share DDL locks. These are taken out against dependent objects when you create stored, compiled objects, such as procedures and views. For example, if you execute Create view MyView as select * from emp, dept where emp.deptno = dept.deptno; share DDL locks will be placed against both EMP and DEPT, while the CREATE VIEW command is being processed. You can modify the contents of these tables, but you cannot modify their structure.

218<br />

CHAPTER 6 ■ LOCKING AND LATCHING<br />

So, DDL will always commit, even if it is unsuccessful. DDL starts by committing—be<br />

aware of this. It commits first so that if it has to roll back, it will not roll back your transaction.<br />

If you execute DDL, it will make permanent any outst<strong>and</strong>ing work you have performed, even<br />

if the DDL is not successful. If you need to execute DDL, but you do not want it to commit<br />

your existing transaction, you may use an autonomous transaction.<br />

There are three types of DDL locks:<br />

• Exclusive DDL locks: These prevent other sessions from gaining a DDL lock or TM<br />

(DML) lock themselves. This means that you may query a table during a DDL operation,<br />

but you may not modify it in any way.<br />

• Share DDL locks: These protect the structure of the referenced object against modification<br />

by other sessions, but allow modifications to the data.<br />

• Breakable parse locks: These allow an object, such as a query plan cached in the Shared<br />

pool, to register its reliance on some other object. If you perform DDL against that<br />

object, <strong>Oracle</strong> will review the list of objects that have registered their dependence <strong>and</strong><br />

invalidate them. Hence, these locks are “breakable”—they do not prevent the DDL from<br />

occurring.<br />

Most DDL takes an exclusive DDL lock. If you issue a statement such as<br />

Alter table t add new_column date;<br />

the table T will be unavailable for modifications during the execution of that statement. The<br />

table may be queried using SELECT during this time, but most other operations will be prevented,<br />

including all DDL statements. In <strong>Oracle</strong>, some DDL operations may now take place<br />

without DDL locks. For example, I can issue the following:<br />

create index t_idx on t(x) ONLINE;<br />

The ONLINE keyword modifies the method by which the index is actually built. Instead of<br />

taking an exclusive DDL lock, preventing modifications of data, <strong>Oracle</strong> will only attempt to<br />

acquire a low-level (mode 2) TM lock on the table. This will effectively prevent other DDL from<br />

taking place, but it will allow DML to occur normally. <strong>Oracle</strong> accomplishes this feat by keeping<br />

a record of modifications made to the table during the DDL statement <strong>and</strong> applying these<br />

changes to the new index as it finishes the CREATE. This greatly increases the availability of<br />

data.<br />

Other types of DDL take share DDL locks. These are taken out against dependent objects<br />

when you create stored, compiled objects, such as procedures <strong>and</strong> views. For example, if you<br />

execute<br />

Create view MyView<br />

as<br />

select *<br />

from emp, dept<br />

where emp.deptno = dept.deptno;<br />

share DDL locks will be placed against both EMP <strong>and</strong> DEPT, while the CREATE VIEW comm<strong>and</strong> is<br />

being processed. You can modify the contents of these tables, but you cannot modify their<br />

structure.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!