锁机制

yfyf4455 2009-04-08 08:36:31

大家有谁可以讲讲锁机制不?谢谢^_^
...全文
175 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
傻儿哥 2009-04-08
  • 打赏
  • 举报
回复
Deadlock Detection 13.3.2.1 检测死锁
139 Oracle automatically detects deadlock situations and resolves them by rolling back one of the statements involved in the deadlock, thereby releasing one set of the conflicting row locks. A corresponding message also is returned to the transaction that undergoes statement-level rollback. The statement rolled back is the one belonging to the transaction that detects the deadlock. Usually, the signalled transaction should be rolled back explicitly, but it can retry the rolled-back statement after waiting.
  Oracle 能自动地检测死锁的情况,回滚造成死锁的某个语句,以便释放冲突的行级锁,从而解决死锁问题。Oracle 将向执行了语句级回滚的事务返回一个错误信息。哪个事务检测出了死锁,哪个事务中的语句就将被回滚。通常,Oracle 通知某一事务后,此事务需要显示地执行回滚操作,但事务可以在等待之后重试被回滚的语句。
 
140 Note:

In distributed transactions, local deadlocks are detected by analyzing wait data, and global deadlocks are detected by a time out. Once detected, nondistributed and distributed deadlocks are handled by the database and application in the same way. 提示:

对于分布式事务,Oracle 通过分析被等待的数据来检测本地死锁(local deadlock),而全局死锁(global deadlock)则是根据超时(time out)来检测的。无论分布式或非分布式死锁,在数据库及应用程序中的处理方法都是相同的。
141 Deadlocks most often occur when transactions explicitly override the default locking of Oracle. Because Oracle itself does no lock escalation and does not use read locks for queries, but does use row-level locking (rather than page-level locking), deadlocks occur infrequently in Oracle.
  通常,用户显式指定的锁方式覆盖(override)了 Oracle 默认的锁方式时更容易发生死锁。由于 Oracle 中不存在锁升级(lock escalation),也不会为查询使用读取锁(read lock),而只使用行级锁(row-level lock)(不是分页级锁(page-level lock)),因此在 Oracle 中死锁的现象较少见。
 
傻儿哥 2009-04-08
  • 打赏
  • 举报
回复
Lock Duration 13.3.1.2 锁的持续时间
125 All locks acquired by statements within a transaction are held for the duration of the transaction, preventing destructive interference including dirty reads, lost updates, and destructive DDL operations from concurrent transactions. The changes made by the SQL statements of one transaction become visible only to other transactions that start after the first transaction is committed.
  事务内各语句获得的锁在事务执行期内有效,以防止事务间破坏性的相互干扰,例如:脏读取(dirty read),无效地更新(lost update),以及其他并发事务中具有破坏性的 DDL 操作。如果某个事务中的 SQL 语句对数据进行了修改,只有在此事务提交后开始的事务才能看到前者修改的结果。
 
126 Oracle releases all locks acquired by the statements within a transaction when you either commit or undo the transaction. Oracle also releases locks acquired after a savepoint when rolling back to the savepoint. However, only transactions not waiting for the previously locked resources can acquire locks on the now available resources. Waiting transactions will continue to wait until after the original transaction commits or rolls back completely.
  当用户提交(commit)或撤销(undo)一个事务后,Oracle 将释放此事务内各个 SQL 语句获得的锁。当用户在事务内回滚到某个保存点(savepoint)后,Oracle 也会释放此保存点后获得的锁。只有当前没有等待被锁资源的事务才能获得可用资源的锁。等待事务不会对可用资源加锁而是继续等待,直至拥有其所等待资源的事务完成提交或回滚。
 
127 Data Lock Conversion Versus Lock Escalation 13.3.1.3 数据锁转换及锁升级
128 A transaction holds exclusive row locks for all rows inserted, updated, or deleted within the transaction. Because row locks are acquired at the highest degree of restrictiveness, no lock conversion is required or performed.
  事务拥有在此事务内被插入(insert),更新(update),删除(delete)的数据行的排他行级锁(exclusive row lock)。对于数据行来说,排他行级锁已经是限制程度最高的锁,因此无需再进行锁转换(lock conversion)。
 
129 Oracle automatically converts a table lock of lower restrictiveness to one of higher restrictiveness as appropriate. For example, assume that a transaction uses a SELECT statement with the FOR UPDATE clause to lock rows of a table. As a result, it acquires the exclusive row locks and a row share table lock for the table. If the transaction later updates one or more of the locked rows, the row share table lock is automatically converted to a row exclusive table lock.
  对于数据表,Oracle 能够自动地将限制程度较低的锁转化为限制程度更高的锁。例如,某个事务内使用了 SELECT ... FOR UPDATE 语句对数据行加锁。此时,事务获得了相关数据行上的排他行级锁,以及相关数据表上的行共享表级锁(row share table lock)。如果此事务将对某些数据行进行更新,那么之前获得的行共享表级锁将自动地转换为行排他表级锁(row exclusive table lock)。
 
130 Lock escalation occurs when numerous locks are held at one level of granularity (for example, rows) and a database raises the locks to a higher level of granularity (for example, table). For example, if a single user locks many rows in a table, some databases automatically escalate the user's row locks to a single table. The number of locks is reduced, but the restrictiveness of what is being locked is increased.
  锁升级(lock escalation)是指处于同一粒度级别(例如,数据行级)上的锁被数据库升级为更高粒度级别(例如,表)上的锁。例如,某个用户已经对某个表内的多行数据加锁,某些数据库管理系统会将用户的这些行级锁升级为一个单一的表级锁。此时系统内锁的数量减少了,对被锁资源的限制程度增加了。
 
131 Oracle never escalates locks. Lock escalation greatly increases the likelihood of deadlocks. Imagine the situation where the system is trying to escalate locks on behalf of transaction T1 but cannot because of the locks held by transaction T2. A deadlock is created if transaction T2 also requires lock escalation of the same data before it can proceed.
  Oracle 数据库中不存在锁升级。锁升级将显著地增加发生死锁(deadlock)的可能性。例如,数据库试图将事务 T1 中对某资源的锁升级,同时事务 T2 也拥有此资源的锁。如果此时事务 T2 也需要将锁升级则将产生死锁。
 
傻儿哥 2009-04-08
  • 打赏
  • 举报
回复
How Oracle Locks Data 13.3 Oracle 如何锁数据
115 Locks are mechanisms that prevent destructive interaction between transactions accessing the same resource—either user objects such as tables and rows or system objects not visible to users, such as shared data structures in memory and data dictionary rows.
  锁(lock)是防止访问相同资源(例如表或数据行等用户对象,或内存中的共享数据结构及数据字典等对用户不可见的系统对象)的事务产生破坏性交互的机制。
 
116 In all cases, Oracle automatically obtains necessary locks when executing SQL statements, so users need not be concerned with such details. Oracle automatically uses the lowest applicable level of restrictiveness to provide the highest degree of data concurrency yet also provide fail-safe data integrity. Oracle also allows the user to lock data manually.
  在任何情况下,Oracle 都能够自动地获得执行 SQL 语句所必须的所有锁,无需用户干预。Oracle 会尽可能地减少锁产生的影响,从而最大程度地保证数据的并发访问能力,并确保数据一致性及错误恢复。同时,Oracle 也支持用户手工加锁的操作。
 
Transactions and Data Concurrency 13.3.1 事务及数据并发访问
119 Oracle provides data concurrency and integrity between transactions using its locking mechanisms. Because the locking mechanisms of Oracle are tied closely to transaction control, application designers need only define transactions properly, and Oracle automatically manages locking.
  Oracle 利用其锁机制(locking mechanism)来实现事务间的数据并发访问及数据一致性。由于 Oracle 的锁机制是与事务控制紧密相关的,因此应用开发者只需正确地定义事务,Oracle 就能自动地对所需的锁进行管理。
 
120 Keep in mind that Oracle locking is fully automatic and requires no user action. Implicit locking occurs for all SQL statements so that database users never need to lock any resource explicitly. Oracle's default locking mechanisms lock data at the lowest level of restrictiveness to guarantee data integrity while allowing the highest degree of data concurrency.
  Oracle 的锁机制是由 Oracle 自动实现的,完全无需用户干预。任何 SQL 语句执行时 Oracle 都隐式地对 SQL 所需的锁进行管理,因此用户无需显式地对资源加锁。Oracle 默认采用的锁机制能尽可能地减小对数据访问的限制,在保证数据一致性的同时实现高度的数据并发性。
 
Modes of Locking 13.3.1.1 锁的模式
123 Oracle uses two modes of locking in a multiuser database:
Exclusive lock mode prevents the associates resource from being shared. This lock mode is obtained to modify data. The first transaction to lock a resource exclusively is the only transaction that can alter the resource until the exclusive lock is released.
Share lock mode allows the associated resource to be shared, depending on the operations involved. Multiple users reading data can share the data, holding share locks to prevent concurrent access by a writer (who needs an exclusive lock). Several transactions can acquire share locks on the same resource.
在多用户的数据库系统中,Oracle 使用两种模式的锁:
排他锁(exclusive lock)模式:能够阻止共享被加锁的资源。对数据进行修改时必须获得此种模式的锁。第一个排他地对资源加锁的事物是唯一可以对此资源进行修改的事物,直至排他锁被释放。
共享锁(share lock)模式:依据操作类型有条件地允许共享被加锁的资源。对数据进行读取的多个用户可共享此数据,这些用户可以对资源加以共享锁,防止其他用户 并发地修改此资源(对数据进行修改的用户需要排他锁)。多个事务可以对相同的资源加共享锁。

傻儿哥 2009-04-08
  • 打赏
  • 举报
回复
通常,多用户数据库需要利用锁机制解决数据并发访问,数据一致性及完整性问题。锁(lock)是一种防止多个事务访问同一资源时产生破坏性的相互影响的机制。

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧