---
title: "ORACLE Isolation Level"
author: "hubin4"
date: "2011-08-03T17:34:11Z"
tags: []
categories: ["数据库"]
slug: "oracle-isolation-level"
draft: false
lastmod: "2012-07-19T14:13:26Z"
id: 1139370
url: "https://eeelin.iteye.com/blog/1139370"
---

**ORACLE Isolation Level**

 

4个级别,3种读取情况

 

 

Isolation Level **Dirty Read** **Nonrepeatable Read** **Phantom Read** **Read uncommitted** Possible Possible Possible **Read committed** Not possible Possible Possible **Repeatable read** Not possible Not possible Possible **Serializable** Not possible Not possible Not possible

 

**Dirty reads\[脏读]****:**

A transaction reads data that has been written by another transaction that has not been committed yet.

读人家还没有提交的数据。人家有可能是瞎插几条数据，马上就会回滚的，但是这种“脏读”会把这些数据搜出来，让我们看见根本不会进数据库的数据，所以叫“脏读”。数据库不要设置这种隔离级别。

 

**Nonrepeatable (fuzzy) reads\[不可重复读，模糊读]:**

A transaction rereads data it has previously read and finds that another committed transaction has modified or deleted the data. For example, a user queries a row and then later queries the same row, only to discover that the data has changed.

同一条SQL，不同时间读出不一样的数据。

就是说，在这个隔离级别上，一条SQL查出数据后，就不管那些数据了，别人可以对这些数据修改。

所以读出来的数据是模糊的，今天这些，明天就不知道了。

通常数据库的隔离级别都是这个级别【read committed】。

 

**Phantom reads\[幻读]:**

A transaction reruns a query returning a set of rows that satisfies a search condition and finds that another committed transaction has inserted additional rows that satisfy the condition.

For example, a transaction queries the number of employees. Five minutes later it performs the same query, but now the number has increased by one because another user inserted a record for a new hire. More data satisfies the query criteria than before, but unlike in a fuzzy read the previously read data is unchanged.

**幻读是非常正常的事情，你提交的东西我当然要读啦，注意是新提交的。**

 

 

 

再说说级别：

1\. Serializable

这就是锁，数据库全锁死，一个人或者一个线程操作数据库，上面的情况都不会发生。数据库是不会设置这个级别的。

 

2. Repeatable read

设置这个级别就是可以重复读的，每条SQL执行后，被这条SQL搜出的数据会锁上，不让改，不让删，所以每次读出来肯定是一样的数据，但是**幻读还是会的，符合条件的新数据插进来了，也会被查出来。**

这种级别也会把数据库的性能降得很低，**数据查上来看看就行了，怎么可以阻止人家去修改这些数据呢？**

 

3.Read committed

通常都是用这个的，脏读不发生，其他发生好了。

 

下面是三种常用数据库查看和设置的一些信息:

 

 

mysql查看与设置事务隔离级别

\-------------------------

1.查看当前会话隔离级别

select @@tx\_isolation;

 

2.查看系统当前隔离级别

select @@global.tx\_isolation;

 

3.设置当前会话隔离级别

set session transaction isolatin level repeatable read;

 

4.设置系统当前隔离级别

set global transaction isolation level repeatable read;

 

5.命令行，开始事务时

set autocommit=off 或者 start transaction

 

 

sqlserver查看事务隔离级别\[没有测试]

\---------------------------------

SELECT CASE transaction\_isolation\_level 

WHEN 0 THEN 'Unspecified' 

WHEN 1 THEN 'ReadUncomitted' 

WHEN 2 THEN 'Readcomitted' 

WHEN 3 THEN 'Repeatable' 

WHEN 4 THEN 'Serializable' 

WHEN 5 THEN 'Snapshot' END AS TRANSACTION\_ISOLATION\_LEVEL 

FROM sys.dm\_exec\_sessions 

where session\_id = @@SPID

 

 

oracle11g查看与设置事务隔离级别

\------------------------------

查看:

网上也有人问这问题,但是都没有回答,去了权威的论坛

发现有人被骂了,说ORACLE的隔离级别不可以单独拿出来说

不要去知道oracle的隔离级别,要了解其机制,乱七八糟一大堆

我估计oracle的每个session,都有自己的隔离级别,如果你非要看,

如果用toad客户端可以按照以下步骤:

 

0\.

保证自动提交是关闭的.

 

1.拿到自己的 sid serial#, like后面的参数是自己的电脑名字,用的是toad客户端 

SELECT   t.sid, t.serial#, t.*   FROM   v$session t WHERE   t.terminal  LIKE '%HUBIN-XP%'

 

2\.

--try

delete from schema.table where obj\_id='10001';

--then 

select * from v$transaction ;

--you can see a  row in this view

 

3.跑这SQL,参数是1拿到的,可以看见你要的级别了

SELECT s.sid, s.serial#,

  CASE BITAND(t.flag, POWER(2, 28))

    WHEN 0 THEN 'READ COMMITTED'

    ELSE 'SERIALIZABLE'

  END AS isolation\_level

FROM v$transaction t, v$session s

WHERE t.addr = s.taddr

  AND s.sid = :sid

  AND s.serial# = :serial;

 

设置:

你确定你真理解真的要设置oracle的级别?