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 219 The last type of DDL lock is a breakable parse lock. When your session parses a statement, a parse lock is taken against every object referenced by that statement. These locks are taken in order to allow the parsed, cached statement to be invalidated (flushed) in the Shared pool if a referenced object is dropped or altered in some way. A view that is invaluable for looking at this information is DBA_DDL_LOCKS. There is no V$ view for you to look at. The DBA_DDL_LOCKS view is built on the more mysterious X$ tables and, by default, it will not be installed in your database. You can install this and other locking views by running the catblock.sql script found in the directory [ORACLE_HOME]/rdbms/admin. This script must be executed as the user SYS in order to succeed. Once you have executed this script, you can run a query against the view. For example, in a single-user database I see the following: ops$tkyte@ORA10G> select session_id sid, owner, name, type, 2 mode_held held, mode_requested request 3 from dba_ddl_locks; SID OWNER NAME TYPE HELD REQUEST ---- --------- --------------------- -------------------- ---- --------- 161 SYS DBMS_UTILITY Body Null None 161 SYS DBMS_UTILITY Body Null None 161 SYS DBMS_APPLICATION_INFO Table/Procedure/Type Null None 161 OPS$TKYTE OPS$TKYTE 18 Null None 161 SYS DBMS_OUTPUT Body Null None 161 SYS DATABASE 18 Null None 161 SYS DBMS_UTILITY Table/Procedure/Type Null None 161 SYS DBMS_UTILITY Table/Procedure/Type Null None 161 SYS PLITBLM Table/Procedure/Type Null None 161 SYS DBMS_APPLICATION_INFO Body Null None 161 SYS DBMS_OUTPUT Table/Procedure/Type Null None 11 rows selected. These are all the objects that my session is “locking.” I have breakable parse locks on a couple of the DBMS_* packages. These are a side effect of using SQL*Plus; it calls DBMS_ APPLICATION_INFO, for example. I may see more than one copy of various objects here—this is normal, and it just means I have more than one thing I’m using in the Shared pool that references these objects. It is interesting to note that in the view, the OWNER column is not the owner of the lock; rather, it is the owner of the object being locked. This is why you see many SYS rows. SYS owns these packages, but they all belong to my session. To see a breakable parse lock in action, let’s first create and run a stored procedure, P: ops$tkyte@ORA10G> create or replace procedure p as begin null; end; 2 / Procedure created. ops$tkyte@ORA10G> exec p PL/SQL procedure successfully completed.

220 CHAPTER 6 ■ LOCKING AND LATCHING The procedure, P, will now show up in the DBA_DDL_LOCKS view. We have a parse lock on it: ops$tkyte@ORA10G> select session_id sid, owner, name, type, 2 mode_held held, mode_requested request 3 from dba_ddl_locks 4 / SID OWNER NAME TYPE HELD REQUEST ---- --------- --------------------- -------------------- ---- --------- 161 OPS$TKYTE P Table/Procedure/Type Null None 161 SYS DBMS_UTILITY Body Null None 161 SYS DBMS_UTILITY Body Null None ... 161 SYS DBMS_OUTPUT Table/Procedure/Type Null None 12 rows selected. We then recompile our procedure and query the view again: ops$tkyte@ORA10G> alter procedure p compile; Procedure altered. ops$tkyte@ORA10G> select session_id sid, owner, name, type, 2 mode_held held, mode_requested request 3 from dba_ddl_locks 4 / SID OWNER NAME TYPE HELD REQUEST ---- --------- --------------------- -------------------- ---- --------- 161 SYS DBMS_UTILITY Body Null None 161 SYS DBMS_UTILITY Body Null None ... 161 SYS DBMS_OUTPUT Table/Procedure/Type Null None 11 rows selected. We find that P is now missing from the view. Our parse lock has been broken. This view is useful to you, as a developer, when it is found that some piece of code won’t compile in the test or development system—it hangs and eventually times out. This indicates that someone else is using it (actually running it), and you can use this view to see who that might be. The same will happen with GRANTS and other types of DDL against the object. You cannot grant EXECUTE on a procedure that is running, for example. You can use the same method to discover the potential blockers and waiters. Latches Latches are lightweight serialization devices used to coordinate multiuser access to shared data structures, objects, and files.

220<br />

CHAPTER 6 ■ LOCKING AND LATCHING<br />

The procedure, P, will now show up in the DBA_DDL_LOCKS view. We have a parse lock on it:<br />

ops$tkyte@ORA10G> select session_id sid, owner, name, type,<br />

2 mode_held held, mode_requested request<br />

3 from dba_ddl_locks<br />

4 /<br />

SID OWNER NAME TYPE HELD REQUEST<br />

---- --------- --------------------- -------------------- ---- ---------<br />

161 OPS$TKYTE P Table/Procedure/Type Null None<br />

161 SYS DBMS_UTILITY Body Null None<br />

161 SYS DBMS_UTILITY Body Null None<br />

...<br />

161 SYS DBMS_OUTPUT Table/Procedure/Type Null None<br />

12 rows selected.<br />

We then recompile our procedure <strong>and</strong> query the view again:<br />

ops$tkyte@ORA10G> alter procedure p compile;<br />

Procedure altered.<br />

ops$tkyte@ORA10G> select session_id sid, owner, name, type,<br />

2 mode_held held, mode_requested request<br />

3 from dba_ddl_locks<br />

4 /<br />

SID OWNER NAME TYPE HELD REQUEST<br />

---- --------- --------------------- -------------------- ---- ---------<br />

161 SYS DBMS_UTILITY Body Null None<br />

161 SYS DBMS_UTILITY Body Null None<br />

...<br />

161 SYS DBMS_OUTPUT Table/Procedure/Type Null None<br />

11 rows selected.<br />

We find that P is now missing from the view. Our parse lock has been broken.<br />

This view is useful to you, as a developer, when it is found that some piece of code won’t<br />

compile in the test or development system—it hangs <strong>and</strong> eventually times out. This indicates<br />

that someone else is using it (actually running it), <strong>and</strong> you can use this view to see who that<br />

might be. The same will happen with GRANTS <strong>and</strong> other types of DDL against the object. You<br />

cannot grant EXECUTE on a procedure that is running, for example. You can use the same<br />

method to discover the potential blockers <strong>and</strong> waiters.<br />

Latches<br />

Latches are lightweight serialization devices used to coordinate multiuser access to shared<br />

data structures, objects, <strong>and</strong> files.

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

Saved successfully!

Ooh no, something went wrong!