数据库事务隔离级别 之 学习笔记

        最近在改一个项目的时候,牵扯到数据库控制并发访问的知识,就参考了《Hibernate实战》这本书,结果越看越糊涂,在看到“ANSI事务隔离性级别”的时候更是不知所云;最终决定放弃它,拿来英文版的读,顺便产生了下面的文字:

 

作为Hibernate 应用程序的开发人员,你的任务是理解数据库的能力,以及根据特定的 场景(或是按照数据的完整性的要求),来改变数据库的隔离行为;(文中摘的,这段话还是不错的)

 

ANSI transaction Isolation levels:

           1. A system that permits dirty reads but not lost updates is said to operate in read uncommitted isolation. One transaction may not write to a row if another uncommitted transaction has already written to it. Any transaction may read any row, however. This isolation level may be implemented in the database-management system with exclusive write locks.(只对写有要求,一个数 据只能够被一个“写事务”所拥有,但同时可以被多个读事务拥有);

 

          2. A system that permits unrepeatable reads but not dirty reads is said to implement read committed transaction isolaction. This may be achieved by using shared read locks and exclusive write locks. Reading transactions don't block other transactions from accessing a row. However, an uncommitted writing transaction blocks all other transactions from accessing the row.( 这里对“写事务”有了更高要求的定义:当“写事务”在处理特定数据的时候,确 保独占数据--“读事务”也不能够访问;从另一个方面,“读事务”不可能够读取未被 提交的数据,但是“读事务”不能够保证在读数据的时候有别的事务修改数据,这里 unrepeatable指在一个“读事务”中前后两次读取的相同的数据可能会不一样);

 

          3. A system operating in repeatable read isolation mode permits neither unrepeatable reads nor dirty reads. Phantom reads may occur. Reading transactions block writing transactions( but not other reading transactions), and writing transactions block all other transactions.(把“ 读事务”和“写事务”完全隔离了开来,也就是说,当一块数据被访问的时候,只有两 种情况,一是被读取--可以有多个“读事务”同事进行,一是被写入--只能由一个“写 事务”独占 )

 

          4. Serializable provides the strictest transaction isolation. This isolation level emulates serial transaction execution, as if transactions were executed one after another, serially, rather than concurrently. Serializability may not be implemented using only row-level locks. There must instead be some other mechanism that prevents a newly inserted row from becoming visible to a transaction that has already executed a query that would return the row.(这个级别的隔离更彻底,任何一块数据只能够被一个事务所 独占)

 

          总体概括:这四种隔离的情况的效果越来越好,但是代价越来越大;其实上面的 英文名字起得很好的,read uncommitted isolation--允许读取没有被提交的数据块; read committed transaction isolaction -- 读取的数据块一定是被提交过的; repeatable read isolation -- 允许重复读取 的 隔离模式,换句话说,前面两种隔离 方式是不能够重复读取的,具体看上面的解释; Strictest transaction isolation -- 最严格的事务隔离模式,但是代价最高;

 

         从另一个角度看一下四种隔离模式:所有的“写事务”都是相互独立的,一个数据块在一个特定的时刻最多只有一个“写事务”与之联系;第一个隔离模式读和写是完全混合的,第二个隔离模式时,在读之前确保没有“写事务”与之联系,但是在这个过程中可以有别的事务参与进来(包括“写模式”),这种情况下数据内容就有可能改变,导致“读事务”中的数据是脏数据,这种隔离模式其实是不可重复读取的(unrepeatable read);第三中模式,把“读事务”与“写事务”完全分离了,在“读事务”的进行中就不用担心这些数据是脏数据了,这种模式成为repeatable read isolation 也就理所当然了; 最后一种模式使所有的“读事务”也变成相互独立,由此,所有的操作变得相互独立了,也就是说一个数据块只对应一个事务,没有并发性可言,安全性很好,但是代价极高;

 

另外:这里的数据库隔离和最近看的java中的concurrency 有点神似!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值