oracle lock 02 - Use of Locks
2014-06-06 19:57
363 查看
In a single-user database, locks are not necessary because only one user is modifying information. However, when multiple users are accessing and modifying data, the database must provide a way to prevent concurrent modification of the same data. Locks achieve
the following important database requirements:
1、Consistency(并发)
The data a session is viewing or changing must not be changed by other sessions until the user is finished.
2、Integrity(完整性)
The data and structures must reflect all changes made to them in the correct sequence.
Oracle Database provides data concurrency, consistency, and integrity among transactions through its locking mechanisms. Locking is performed automatically and requires no user action.
The need for locks can be illustrated by a concurrent update of a single row. In the following example, a simple web-based application presents the end user with an employee email and phone number. The application uses an
as the following to modify the data:
In the preceding
after the application last read and displayed it to the user. In this way, the application avoids thelost updates database problem in which one user overwrites changes made by another user, effectively losing the update by
the second user Table 9-4 shows an example of a lost update).
Table 9-4 shows the sequence of events when two sessions attempt to modify the same row in the
Table 9-4 Row Locking Example
Oracle Database automatically obtains necessary locks when executing SQL statements. For example, before the database permits a session to modify data, the session must first lock the data. The lock gives the session exclusive control over the data so that
no other transaction can modify the locked data until the lock is released.Oracle Database automatically obtains necessary locks when executing SQL statements. For example, before the database permits a session to modify data, the session must first lock the
data. The lock gives the session exclusive control over the data so that no other transaction can modify the locked data until the lock is released.
Because the locking mechanisms of Oracle Database are tied closely to transaction control, application designers need only define transactions properly, and Oracle Database automatically manages locking. Users never need to lock any resource explicitly,
although Oracle Database also enables users to lock data manually.
the following important database requirements:
1、Consistency(并发)
The data a session is viewing or changing must not be changed by other sessions until the user is finished.
2、Integrity(完整性)
The data and structures must reflect all changes made to them in the correct sequence.
Oracle Database provides data concurrency, consistency, and integrity among transactions through its locking mechanisms. Locking is performed automatically and requires no user action.
The need for locks can be illustrated by a concurrent update of a single row. In the following example, a simple web-based application presents the end user with an employee email and phone number. The application uses an
UPDATEstatement such
as the following to modify the data:
UPDATE employees SET email = ?, phone_number = ? WHERE employee_id = ? AND email = ? AND phone_number = ?
In the preceding
UPDATEstatement, the email and phone number values in the
WHEREclause are the original, unmodified values for the specified employee. This update ensures that the row that the application modifies was not changed
after the application last read and displayed it to the user. In this way, the application avoids thelost updates database problem in which one user overwrites changes made by another user, effectively losing the update by
the second user Table 9-4 shows an example of a lost update).
Table 9-4 shows the sequence of events when two sessions attempt to modify the same row in the
employeestable at roughly the same time.
Table 9-4 Row Locking Example
Time | Session 1 | Session 2 | Explanation |
---|---|---|---|
t0 | SELECT employee_id, email, phone_number FROM hr.employees WHERE last_name = 'Himuro'; EMPLOYEE_ID EMAIL PHONE_NUMBER ----------- ------- ------------ 118 GHIMURO 515.127.4565 | In session 1, the hr1user queries hr.employeesfor the Himuro record and displays the employee_id ( 118), email ( GHIMURO), and phone number ( 515.127.4565) attributes. | |
t1 | SELECT employee_id, email, phone_number FROM hr.employees WHERE last_name = 'Himuro'; EMPLOYEE_ID EMAIL PHONE_NUMBER ----------- ------- ------------ 118 GHIMURO 515.127.4565 | In session 2, the hr2user queries hr.employeesfor the Himuro record and displays the employee_id ( 118), email ( GHIMURO), and phone number ( 515.127.4565) attributes. | |
t2 | UPDATE hr.employees SET phone_number='515.555.1234' WHERE employee_id=118 AND email='GHIMURO' AND phone_number='515.127.4565'; 1 row updated. | In session 1, the hr1user updates the phone number in the row to 515.555.1234, which acquires a lock on the GHIMUROrow. | |
t3 | UPDATE hr.employees SET phone_number='515.555.1235' WHERE employee_id=118 AND email='GHIMURO' AND phone_number='515.127.4565'; -- SQL*Plus does not show -- a row updated message or -- return the prompt. | In session 2, the hr2user attempts to update the same row, but is blocked because hr1is currently processing the row. The attempted update by hr2occurs almost simultaneously with the hr1update. | |
t4 | COMMIT; Commit complete. | In session 1, the hr1user commits the transaction. The commit makes the change for Himuro permanent and unblocks session 2, which has been waiting. | |
t5 | 0 rows updated. | In session 2, the hr2user discovers that the GHIMUROrow was modified in such a way that it no longer matches its predicate. Because the predicates do not match, session 2 updates no records. | |
t6 | UPDATE hr.employees SET phone_number='515.555.1235' WHERE employee_id=118 AND email='GHIMURO' AND phone_number='515.555.1234'; 1 row updated. | In session 1, the hr1user realizes that it updated the GHIMUROrow with the wrong phone number. The user starts a new transaction and updates the phone number in the row to 515.555.1235, which locks the GHIMURO row. | |
t7 | SELECT employee_id, email, phone_number FROM hr.employees WHERE last_name = 'Himuro'; EMPLOYEE_ID EMAIL PHONE_NUMBER ----------- ------- ------------ 118 GHIMURO 515.555.1234 | In session 2, the hr2user queries hr.employeesfor the Himuro record. The record shows the phone number update committed by session 1 at t4. Oracle Database read consistency ensures that session 2 does not see the uncommitted change made at t6. | |
t8 | UPDATE hr.employees SET phone_number='515.555.1235' WHERE employee_id=118 AND email='GHIMURO' AND phone_number='515.555.1234'; -- SQL*Plus does not show -- a row updated message or -- return the prompt. | In session 2, the hr2user attempts to update the same row, but is blocked because hr1is currently processing the row. | |
t9 | ROLLBACK; Rollback complete. | In session 1, the hr1user rolls back the transaction, which ends it. | |
t10 | 1 row updated. | In session 2, the update of the phone number succee 4000 ds because the session 1 update was rolled back. The GHIMUROrow matches its predicate, so the update succeeds. | |
t11 | COMMIT; Commit complete. | Session 2 commits the update, ending the transaction. |
no other transaction can modify the locked data until the lock is released.Oracle Database automatically obtains necessary locks when executing SQL statements. For example, before the database permits a session to modify data, the session must first lock the
data. The lock gives the session exclusive control over the data so that no other transaction can modify the locked data until the lock is released.
Because the locking mechanisms of Oracle Database are tied closely to transaction control, application designers need only define transactions properly, and Oracle Database automatically manages locking. Users never need to lock any resource explicitly,
although Oracle Database also enables users to lock data manually.
相关文章推荐
- ORACLE:IMP工具ignore=y的一个问题【THE QUESTION OF ORACLE IMP TOOL WHEN USE PARAMETER IGNORE=Y】
- Understanding Threads and Locks of Oracle JRockit JVM
- ERROR 1206 (HY000): The total number of locks exceeds the lock table size 错误解决
- MySQL5.7解决‘’(1206)The total number of locks exceeds the lock table size‘’问题
- mysql The total number of locks exceeds the lock table size
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock
- 【MySQL笔记】mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
- MySql 5.1x InnoDB "ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock
- MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE
- 异常:java.sql.SQLException: The total number of locks exceeds the lock table size
- Know How To Use Check Box Mapping Of Other Values Property In Oracle Forms
- mysql Error:The total number of locks exceeds the lock table size
- Tuning Locks of Oracle JRockit JVM
- mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
- total number of locks exceeds the lock table size
- mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
- mysql出现The total number of locks exceeds the lock table size解决办法
- The total number of locks exceeds the lock table size错误
- mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法