ORACLE 锁机制
ORACLE锁机制中有两种锁分为:排他锁、共享锁 排他锁:又称X锁,当用户操纵一条数据时,oracle会自动隐式的未该用户操纵的这条记录加上排他锁。加上排他锁后其他事务是不能对该条数据操纵的,只能查看,其他事务也不能再继加X锁。当本事务结束后,其他事务才
ORACLE锁机制中有两种锁分为:排他锁、共享锁
排他锁:又称X锁,当用户操纵一条数据时,oracle会自动隐式的未该用户操纵的这条记录加上排他锁。加上排他锁后其他事务是不能对该条数据操纵的,只能查看,其他事务也不能再继加X锁。当本事务结束后,其他事务才可加X锁操纵这条数据。
共享锁:又称S锁,加锁的用户只要查看权限,而不能增删改。其他用户也可对该数据加S锁,也只能有查看权限不能增删改。
ORACLE中加锁对象的不同,也可分为三种类型的锁。
1、DML锁
DML锁是最常用的锁,为保护表对象数据的一致完整性。该锁再可细分为
行级锁:简单说锁定表的数据行;也称排他锁又称TX锁。当对某些数据行使用DML语句时,oracle自动对该数据行添加行级锁,其他事务只能等到事务提交或释放后才能执行这些数据的DML操作;行级锁不是单独的锁,系统添加行级锁时,oracle也会自动加上该数据对应的表级锁,是为防止其他用户使用DDL语句修改表结构等从而破坏行级锁的约束
表级锁:简单说锁定整个对象表;也称表锁又称TM锁。表级锁可分为5种:
1).行共享
2).行排他
3).共享
4).共享排他
5).排他
2、DDL锁
3、系统锁:系统内部锁,用户无法调用查看的到,不需要了解。
手动加锁
一般情况下,ORACLE执行DML语句,一般时不需要手动加锁的,它会自动加锁。也可以手动的方式去添加锁,手动添加方式为:
1).SELECT 语句 FOR UPDATE [of columns] [wait n | nowait] [skip locked]; --对行记录加锁
2).LOCK TABLE tableName IN lockemode MODE NOWAIT --对表加锁
加锁语句练习:
1).使用SELECT 语句 FOR UPDATE语句加锁:
会话1:增加一条记录,不提交事务
SQL> select * from classes;
CID CNAME
--------------------------------------- ----------
1 0901
2 0902
3 0903
4 1111
SQL> update classes set cname='0000' where cid=4;
1 row updated
--在会话1中插入了一条记录但不提交,这时会话1同时拥有TX(行级锁),TM(表级锁)
会话2:使用SELECT * FOR UPDATE 语句加行级锁
SQL> select * from classes for update nowait;
select * from classes for update nowait
ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源
--会话2对表里面行数据添加行级锁失败,因为会话1中还在占用该锁。 如不指定nowait | wait 关键字,会一直处于等待阶段,直到锁被释放才可获取到锁。
SQL> select * from classes for update;
SQL> --等待中....
2).使用LOCK TABLE 语句加锁
会话1:在会话1中修改一条记录,但不提交。
SQL> select * from classes;
CID CNAME
--------------------------------------- ----------
1 0901
2 0902
3 0903
4 0000
SQL> update classes set cname='1111' where cid=4;
1 row updated
--这时会话1中拥有TM,TX锁。
会话2:会话2表中添加行拍他锁
SQL> lock table classes in row exclusive mode nowait;
Table(s) locked
--添加表级锁成功。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node
