[oracle实验] Locking:数据一致性和完整性
2013-05-27 16:36
190 查看
公车上看concept,有关oracle锁机制,跟MSSQL有些不同,抽空坐下实验验证一下
oracle通过锁机制在事务间提供数据并发、一致性和完整性,这些操作自动执行,无需用户干预。
情景模拟:多个用户并发修改数据表的某一行。这里实验一个B/S应用,多用户环境使用下列语句修改 HR.EMPLOYEES表
这个语句确保在应用程序查询并显示给终端用户之后,正在修改的employee_id数据不会被修改。这样,应用程序避免出现一个用户覆盖了另一个用户做出的修改的问题,或叫lost update
跟着下表操作验证:
在 t2,t3,t4,t5时刻,很显然session2对employee的修改无效,避免了lost update发生。
oracle通过锁机制在事务间提供数据并发、一致性和完整性,这些操作自动执行,无需用户干预。
情景模拟:多个用户并发修改数据表的某一行。这里实验一个B/S应用,多用户环境使用下列语句修改 HR.EMPLOYEES表
UPDATE employees SET email = ?, phone_number = ? WHERE employee_id = ? AND email = ? AND phone_number = ?
这个语句确保在应用程序查询并显示给终端用户之后,正在修改的employee_id数据不会被修改。这样,应用程序避免出现一个用户覆盖了另一个用户做出的修改的问题,或叫lost update
跟着下表操作验证:
时间 | Session 1 | Session 2 | 解释 |
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 hr1 user queries hr.employees for 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 hr2 user queries hr.employees for 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 hr1 user updates the phone number in the row to 515.555.1234, which acquires a lock on the GHIMURO row. | |
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 hr2 user attempts to update the same row, but is blocked because hr1 is currently processing the row. The attempted update by hr2 occurs almost simultaneously with the hr1 update. | |
t4 | COMMIT; Commit complete. | In session 1, the hr1 user 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 hr2 user discovers that the GHIMURO row 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 hr1 user realizes that it updated the GHIMURO row 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 hr2 user queries hr.employees for 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 hr2 user attempts to update the same row, but is blocked because hr1 is currently processing the row. | |
t9 | ROLLBACK; Rollback complete. | In session 1, the hr1 user rolls back the transaction, which ends it. | |
t10 | 1 row updated. | In session 2, the update of the phone number succeeds because the session 1 update was rolled back. The GHIMURO row matches its predicate, so the update succeeds. | |
t11 | COMMIT; Commit complete. | Session 2 commits the update, ending the transaction. |
相关文章推荐
- oracle的完整性约束:check/not null/unique/primary/foreign key
- Oracle 10g Flashback实验
- 2014-1-15 关于oracle的一些实验
- oracle 热备份与恢复实验
- ORACLE空间管理实验4:块管理之ASSM三级位图结构
- oracle实验记录 (分析oracle硬解析&软解析&fast soft parse)
- Oracle利用RMAN做异地恢复实验
- 专题实验第一篇:ORACLE网络
- oracle实验16:分支函数
- Oracle 数据完整性,约束
- 我的Oracle 9i学习日志(17)-- 维护数据完整性.a
- 数据库原理实验实验四 oracle的安全性和完整性控制
- Oracle之课程实验四(创建表空间)
- oracle linux 5.8安装oracle 11g rac 第一章:配置实验环境
- oracle: ocp题解与实验(7/205) .
- oracle 11g 学习笔记 10_31(2)_维护数据的完整性
- [数据库] Navicat for Oracle设置唯一性和递增序列实验
- Oracle恢复实验(一)
- Oracle——Default pool 之cache特性 以及 测试default pool使用效果实验分享
- 实验: Oracle中表shrink与move后index的状态