transactions(事务)MYSQL

transactions(事务)

a group of sql staterment that represent a single unit of work

所有语句都必须成功执行,不然就会failed(原子性)

MySQL5.5之前默认存储引擎是MyISAM
5.5之后的存储引擎是InnoDB。
上述两者的最大差异就是InnoDB支持事务

ACID:

  • atomicity 原子性

  • Consistency(理解一致性 ):数据库在事务开始前和后,数据库的完整性没有遭到破坏

  • isolation(隔离)不同事务之间相互分割

  • durability(持久性): 事务在执行后会保存数据库的状态(内存数据长久存储在硬盘里)

变量autocommit 默认值为on 当执行一个sql语句时会把它放到一个事务中,如果sql语句成功执行,则会在事务后加 commit

与事务相关的两条SQL语句:commit(提交)rollback(回滚)

默认的concurrency处理:

​ if your transaction tries to modify a row or mutiply rows, it puts a lock on these rows, and this lock prevents other transaction modify these rows until your transaction is done

concurrency problem:is a very fairly advanced promblem

  1. lost updates 当两个事务同时开始,并对同一行进行更改,最后提交的那个事务将会覆盖早于它结束的事务的修改内容
  2. dirty reads 当一个事务对某一项进行更改但没有进行提交,这时有另一个事务对这一项进行读取 read uncommit data, our data was dirty
    • method: to set a isolation level of our transaction to recommit it,
    • one transaction update is not immediately visible to the other transaction即只能对提交(commit)之后的数据进行读操作 only read commited datanone
  3. repeating reads: 在一个事务进行种,另一个事务对数据进行了更改,导致第一个事务两次查询的结果不同 is a none repeatable or inconsistent read
    • method : set a isolation level called repeatable read 如果你对事务设置了repeatable read 他会建立一个snapshot以确保同一个事务相同的查询结果不同
  4. phantom reads : 例如第一个事务对一个表中的数据进行了修改,比如这种修改涉及到表中的“全部数据行”。同时,第二个事务也修改这个表中的数据,这种修改是向表中插入“一行新数据”。那么,以后就会发生操作第一个事务的用户发现表中还存在没有修改的数据行,就好象发生了幻觉一样.一般解决幻读的方法是增加范围锁
    • method : if the data is getting changed in the meantime, our transaction will wait to get the most recent data

isolation level:使用isolation level 只有当你想防止 phantom read时

  1. read uncommited:
  2. read commited
  3. repeatable read: 保证一个事务在进行两次查询时结果一致**(default isalotion level)**
  4. serializable:if data is getting changed in the meantime, our transaction will wait to get the most recent data

the more we increase our isolation level, we get less concurrency problem, but the more profermance(性能) and scaleability(可扩展性) problem we going to experienceto recap a lowerr isolation level give us more concurrency, more user can access the same data at the same time

set transaction isolation level isolation_level;//这样只会改变下一个transaction的isolation level

不可重复读是对于同一条记录内容的“不可重复读”幻读是对于某一范围的数据集,发现查询数据集的行数多了或者少了,从而出现的不一致。所以不可重复读的原因是 对于要查询的那条数据进行了UPDATE或DELETE而幻读是对于要查询的 那个范围的数据集,进行了INSERT。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值