数据库中,读取数据时常见的几种情况:
Dirty Read:A transaction reads data that has been written by another transaction that has not been committed yet.
Fuzzy Read:A transaction rereads data it has previously read and finds that another committed transaction has modified or deleted the data.
Phantom Read: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.
上述文字可概括为:
1.脏读:读取未提交的数据;
2.模糊读:读取已变动的数据;
3.幻读:读取已提交的数据。
在oracle中,仅定义了两种级别的隔离等级,分别是READ COMMITTED、SERIALIZABLE。
------------------ ------------------ ------------------ ------------------
Isolation Level Dirty Read Fuzzy Read Phantom Read
------------------ ------------------ ------------------ ------------------
READ COMMITTED no yes yes
------------------ ------------------ ------------------ ------------------
SERIALIZABLE no no no
------------------ ------------------ ------------------ ------------------
由上表可知,oracle是不允许脏读的。
举例:
--会话1
create table t_isolation(id integer,name varchar2(10));
insert into t_isolation(id,name) values (1,'a');
commit;
--会话2
set transaction isolation level read committed;
select * from t_isolation;
--会话1
--不提交
insert into t_isolation(id,name) values (2,'b');
--会话2
select * from t_isolation;
执行查询,发现查询不到刚插入的数据
update t_isolation set name='c' where id=2;
执行更新,发现无数据可更新
delete from t_isolation where id=2;
执行删除,发现无数据可删除
--会话1
commit;
--会话2
select * from t_isolation;
执行查询,发现可以查询到刚插入的数据
既然可查询到刚插入的数据,那么更新和删除自然也没问题,这里就不详细举例了。
--会话3
set transaction isolation level serializable;
select * from t_isolation;
--会话1
--不提交
delete from t_isolation where id=2;
--会话3
select * from t_isolation;
发现删除的数据可查询到
--会话1
commit;
--会话3
select * from t_isolation;
发现删除的数据可查询到
事务隔离级别
最新推荐文章于 2024-06-23 21:40:41 发布