Oracle的隔离级别(Isolation Level)

刚刚回顾了事务的ACID特性,以前对隔离级别理解的不是很透彻,以为ACID中的Isolation和隔离级别(Isolation Level)有关,今天理清之后,发现这两个是完全不同的概念。
ACID中的Isolation是指在一个事务的影响在其未提交前对其它事务时不可见的。这是所有的关系型数据库必须要遵循的一个准则。
而每种数据库产品的隔离级别的规定是不一样的,比如Oracle就只有以下3种隔离级别:
隔离级别
描述
Read committed
(默认)
Each query executed by a transaction sees only data that was committed before the query (not the transaction) began.
SerializableSerializable transactions see only those changes that were committed at the time the transaction began, plus those changes made by the transaction itself through INSERT, UPDATE, and DELETE statements.

Read-onlyRead-only transactions see only those changes that were committed at the time the transaction began and do not allow INSERT, UPDATE, and DELETE statements.

下面这个例子可以很好的展示ACID中的Isolation以及以上3种隔离级别在Oracle是怎么起作用的。
1. Isolation(独立性)
这是数据库天生具有的特性,不需要任何的配置。
1) 创建一个简单的测试表:
SQL>create table test (id number);

Table created.

2) 在session1中插入一条数据:
Session1> insert into test values(1);

1 row created.

3) 在session2中查询:
Session2> select * from test;

no rows selected

在session2中无法看到session1插入的数据,因为session1没有提交,而根据事务独立性的特性,未提交事务的影响在其它事务中是不可见的。

2. Read committed
这是Oracle默认的隔离级别,当一条语句开始执行时,它既能看到本事务之前对数据的影响,也能看到语句开始执行时已提交事务对数据的影响。
1) 清空测试表数据:
SQL> delete from test;

1 row deleted.

SQL> commit;

Commit complete.

2) 设置session1的隔离级别为Read committed:
Session1> alter session set isolation_level=read committed;

Session altered.

3) 在session1中插入一条数据:
Session1> insert into test values(1);

1 row created.
4) 在session1查询:
SQL> select * from test;

        ID
----------
         1
在session1中可以看到本事务之前插入的数据。
5) 在session2中插入一条数据并提交:
Session2> insert into test values(2);

1 row created.

Session2> commit;

Commit complete.
6) 在session1中查询:
Session1> select * from test;

        ID
----------
         2
         1
在session1中可以看到session2中已提交的数据。

3. Serializable
在Serializable的隔离级别中,只能看到本事务对数据的影响,而无法看到在本事务开始时未提交事务的影响。
1) 清空测试表数据:
SQL> delete from test;

2 rows deleted.

SQL> commit;

Commit complete.

2) 设置session1的隔离级别为Serializable:
Session1> alter session set isolation_level=serializable;

Session altered.
3) 在session1中插入数据:
Session1> insert into test values(1);

1 row created.
4) 在session1中查询:
Session1> select * from test;

        ID
----------
         1
在session1中可以看到本事务之前插入的数据。
5) 在session2中插入一条数据并提交:
Session2> insert into test values(2);

1 row created.

SQL> commit;

Commit complete.
6) 在session1中查询:
Session1> select * from test;

        ID
----------
         1

在session1中无法看到session2中插入的数据。

4. Read Only
在Read Only的隔离级别中,不允许INSERT, UPDATE和DELETE语句的出现,这种隔离级别很少用。
1) 清空测试表数据:
SQL> delete from test;

1 row deleted.

SQL> commit;

Commit complete.

2) 设置session1的隔离级别为Read Only:
Session1> SET TRANSACTION READ ONLY;

Transaction set.

3) 尝试在session1中插入数据:
Session1> insert into test values(1);
insert into test values(1)
*
ERROR at line 1:
ORA-01456: may not perform. insert/delete/update operation inside a READ ONLY
transaction
插入数据出错。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26277071/viewspace-708768/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26277071/viewspace-708768/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值