Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1780849
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: Oracle

2012-10-02 09:54:54

Latches and Internal Locks (摘自 Oracle Concepts)

Latches and internal locks protect internal database and memory structures. Both are inaccessible to users, because users have no need to control over their occurrence or duration. 

Latches

Latches are simple, low-level serialization mechanisms to protect shared data structures in the system global area (SGA). For example, latches protect the list of users currently accessing the database and protect the data structures describing the blocks in the buffer cache. A server or background process acquires a latch for a very short time while manipulating or looking at one of these structures. The implementation of latches is operating system dependent, particularly in regard to whether and how long a process will wait for a latch.
(最重要的是:latch是保护SGA中的共享的结构,而这种结构显然是描述内存块的组织方式的,而最常见的结构显然就是各种lists/buckets。)

Internal Locks

Internal locks are higher-level, more complex mechanisms than latches and serve a variety of purposes.

Dictionary Cache Locks

These locks are of very short duration and are held on entries in dictionary caches while the entries are being modified or used. They guarantee that statements being parsed do not see inconsistent object definitions.

Dictionary cache locks can be shared or exclusive. Shared locks are released when the parse is complete. Exclusive locks are released when the DDL operation is complete.

File and Log Management Locks

These locks protect various files. For example, one lock protects the control file so that only one process at a time can change it. Another lock coordinates the use and archiving of the redo log files. Datafiles are locked to ensure that multiple instances mount a database in shared mode or that one instance mounts it in exclusive mode. Because file and log locks indicate the status of files, these locks are necessarily held for a long time.

Tablespace and Rollback Segment Locks

These locks protect tablespaces and rollback segments. For example, all instances accessing a database must agree on whether a tablespace is online or offline. Rollback segments are locked so that only one instance can write to a segment.


=================================================================================
摘自:http://www.itpub.net/forum.php?mod=viewthread&tid=1320117&extra=&highlight=&page=2

Latches are the more restrictive mechanism, because they do not allow multiple processes to inspect the protected data structure at the same time—they provide for exclusive access only.
Locks allow for better concurrency, because they maybe held in a shared mode when the data structure is simply being inspected.
This is a simplification. The redo copy latches can be shared, but this is hardware dependent.

Another significant difference between locks and latches is request queuing.(是否排队)

Requests for locks are queued if necessary and serviced in order, whereas latches do not support request queuing. (lock会排队,而latch不会排队,所以lock会发生死锁,而latch会有饿死现象。)

If a request to get a latch fails because the latch is busy, the process just continues to retry until it succeeds. So latch requests are not necessarily serviced in order.

Because a latch can only be held by one process at a time, and because there is no inherent concept of queuing, the latch data structure itself is very simple—essentially just a single location in memory representing the state of the latch.

And because the latch data structure is so simple, the functions to get and release a latch have very little work to do. By contrast, the data structures for locks are much more sophisticated because of their support for queuing and concurrency.So the functions to get, convert, and release locks have correspondingly more work to do.

Of course, it is necessary for Oracle to ensure that only one process at a time can modify the latch and lock data structures themselves.

For latches this is easy. Because each latch is just a single location in memory, Oracle is able to use the TEST AND SET, LOAD AND CLEAR, or COMPARE AND SWAP instructions of the underlying hardware's instruction set for its latch get operations. Because these are simple machine instructions that are guaranteed to be atomic, no other locking mechanism is needed. This simplicity makes latch gets very efficient.

Oracle's lock data structures, on the other hand, have several parts, and therefore cannot be modified atomically. For this reason, Oracle actually protects operations on locks with latches. (因为lock不能被原子性地修改,所以在修改lock时,实际上是利用了latch来保证唯一性的修改的。) The type of latch used varies depending on the type of lock. For example, the cache buffer locks are indirectly protected by the cache buffers chains latches, and the row cache enqueue locks are protected by the row cache objects latch.

总结一下:
Oracle中lock和latch的区别如下:
1)latch是十分轻量级的,而lock是复杂的结构;
2)latch是排他性的访问,没有排队机制,存在饿死现象;而lock允许并发,有排队机制,存在死锁现象;
3)latch是修改是原子性的,而lock的修改时要latch保护的;
4)latch只用于SGA内存中的structures

阅读(2136) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~