博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL事务
阅读量:7080 次
发布时间:2019-06-28

本文共 2299 字,大约阅读时间需要 7 分钟。

事务特性ACID

1. Atomicity(原子性)2. Consistency(一致性)3. Isolation(隔离性)4. Durability(持久性)
  • 查看事务隔离级别
    select @@tx_isolation;
  • 开始关闭事务
//开始事务start transaction/begin;//提交或回滚commit/rollback
  • 设置事务自动提交开关
    SET autocommit = {0 | 1}
  • 设置事务隔离级别
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL READ COMMITTED可选的事务隔离级别有:REPEATABLE READREAD COMMITTEDREAD UNCOMMITTEDSERIALIZABLE

数据库并发问题

  • 读脏
    事务T1读取了事务T2未提交的更新后的数据。
  • 不可重复读
    事务T1执行过程中,事务T2提交了新数据,事务T1在事务T2提交前后读到的数据不一致。
  • 幻读
    事务T1的插入或删除(事务已提交)导致事务T2在行数上不一致的情况。

    MySQL各事务隔离级别对并发问题的解决

事务隔离级别 读脏 不可重复读 幻读
读未提交(Read-Uncommitted) 可能 可能 可能
读提交(Read-Committed) 不可能 可能 可能
可重复读(Repeatable-Read) 不可能 不可能 可能
串行化(Serializable) 不可能 不可能 不可能
下面两个例子说明RR下是有幻读现象的
create table goods(id int primary key,name varchar(100),amount int not null);
  1. 插入幻读
事务1 事务2
begin; begin;
select * from goods;
Empty set (0.00 sec)
insert into goods(id, name, amount) values(1, '苹果', 100);
commit;
insert into goods(id, name, amount) values(1, '苹果', 100);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

对于事务1,开始表为空的,后来插入是重复的key,幻觉出现。

  1. 更新幻读
事务1 事务2
begin; begin;
select * from goods;
1 row in set (0.00 sec)
insert into goods(id, name, amount) values(2, '香蕉', 100);
commit;
update goods set amount=1;
Rows matched: 2 Changed: 2 Warnings: 0

对于事务1,开始查询表有一条记录,后来更新却发现有两条数据被更新了,幻觉出现。

共享锁与排他锁

共享锁(shared (S) lock)
A kind of lock that allows other transactions to read the locked object, and to also acquire other shared locks onit, but not to write to it.  共享锁又叫S锁,一个事务对资源加共享锁,那么其他事务还可以对此资源加共享锁,但是不能加排他锁。也即是说对资源加共享锁意味着资源可以被读但是不能对其进行删除和修改。如果事务T1对某一资源加共享锁,在没有其他事务对此资源加共享锁的情况下那么T1还可以对此资源加排它锁。  使用语法:begin;select * from tableName where id=2 lock in share mode;commit;
排他锁(exclusive (X) lock )
A kind of lock that prevents any other transaction from locking the same row. Depending on the transactionisolation level, this kind of lock might block other transactions from writing to the same row, or might also blockother transactions from reading the same row. The default InnoDB isolation level, REPEATABLE READ, enableshigher concurrency by allowing transactions to read rows that have exclusive locks, a technique known asconsistent read.  排他锁又叫X锁,在事务中删除、更新以及插入都会对资源加排它锁,对资源加排它锁后就不能对其加共享锁和排它锁了。如果再加则会引起阻塞直到上一个锁释放。使用语法:begin;select * from tableName where id=2 for update;commit;

转载于:https://blog.51cto.com/wenshengzhu/2165713

你可能感兴趣的文章
R.java was modified manually! Reverting to generated version!(R文件丢失异常原因汇总)
查看>>
ASP.NET MVC Model验证(一)
查看>>
opensuse12.1安装VirtualBox
查看>>
获取系统特殊文件
查看>>
C语言中的for命令
查看>>
Hash算法
查看>>
urlWithString、fileURLWithPath的区别
查看>>
Elasticsearch
查看>>
【ASM内部原理】_asm_kill_unresponsive_clients & _asm_healthcheck_timeout
查看>>
基于业务单元的开发与部署模式
查看>>
WCF 无法激活服务,因为它不支持 ASP.NET 兼容性
查看>>
爱阅读,经典编程图书分享
查看>>
oracle checkpoint
查看>>
流程图与代码的重构
查看>>
Shell 编程基础(三)
查看>>
phpRedisAdmin安装与配置
查看>>
我的友情链接
查看>>
OpenStack服务组件
查看>>
java中substring的用法
查看>>
Mysql DBA 高级运维学习之路-Mysql常见多实例配置方案及多实例安装
查看>>