MySQL事务

1、事务的概念

(1)事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体,一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么都不执行。
(2)事务是一个不可分割的工作逻辑单元。在数据库系统上执行并发操作时,事务是最小的控制单元。
(3)事务适用于多用户同时操作的数据库系统的场景,如银行、保险公司及证券交易系统等等。
(4)事务通过事务的整体性以保证数据的一致性。
(5)事务能够提高在向表中更新和插入信息期间的可靠性。

它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位

2、事务的ACID特点

ACID:是指在可靠数据库管理系统(DBMS)中,事务(transaction)应该具有的四个特性:原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)。这是可靠数据库所应具备的几个特性。

原子性:指事务是一个不可再分割的工作单位,事务中的操作要么都发生,要么都不发生。

  • 事务是一个完整的操作,事务的各元素是不可分的。
  • 事务中的所有元素必须作为一个整体提交或回滚。
  • 如果事务中的任何元素失败,则整个事务将失败。

案例:
A给B转帐100元钱的时候只执行了扣款语句,就提交了,此时如果突然断电,A账号已经发生了扣款,B账号却没收到加款,在生活中就会引起纠纷。这种情况就需要事务的原子性来保证事务要么都执行,要么就都不执行。

一致性:指在事务开始之前和事务结束以后,数据库的完整性约束没有被破坏。

  • 当事务完成时,数据必须处于一致状态。
  • 在事务开始前,数据库中存储的数据处于一致状态。
  • 在正在进行的事务中,数据可能处于不一致的状态。
  • 当事务成功完成时,数据必须再次回到已知的一致状态。

案例:
对银行转帐事务,不管事务成功还是失败,应该保证事务结束后表中A和B的存款总额跟事务执行前一致。

隔离性:指在并发环境中,当不同的事务同时操纵相同的数据时,每个事务都有各自的完整数据空间。对数据进行修改的所有并发事务是彼此隔离的,表明事务必须是独立的,它不应以任何方式依赖于或影响其他事务。
修改数据的事务可在另一个使用相同数据的事务开始之前访问这些数据,或者在另一个使用相同数据的事务结束之后访问这些数据。
也就是说并发访问数据库时,一个用户的事务不被其他事务所干扰,各并发事务之间数据库是独立的。

当多个客户端并发地访问同一个表时,可能出现下面的一致性问题:

(1)脏读:当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据。

(2)不可重复读:指在一个事务内,多次读同一数据。在这个事务还没有结束时,另外一个事务也访问该同一数据。那么,在第一个事务中的两次读数据之间,由于第二个事务的修改,那么第一个事务两次读到的的数据可能是不一样的。这样就发生了在一个事务内两次读到的数据是不一样的,因此称为是不可重复读。(即不能读到相同的数据内容)

(3)幻读:一个事务对一个表中的数据进行了修改,这种修改涉及到表中的全部数据行。同时,另一个事务也修改这个表中的数据,这种修改是向表中插入一行新数据。那么,操作前一个事务的用户会发现表中还有一个没有修改的数据行,就好象发生了幻觉一样。

(4)丢失更新:两个事务同时读取同一条记录,A先修改记录,B也修改记录(B不知道A修改过),B提交数据后B的修改结果覆盖了A的修改结果。

事务的隔离级别决定了事务之间可见的级别。
MySQL事务支持如下四种隔离,用以控制事务所做的修改,并将修改通告至其它并发的事务:

注意: mysql默认的事务处理级别是可重复读repeatable read ,而Oracle和SQL Server是提交读read committed 。

持久性:在事务完成以后,该事务所对数据库所作的更改便持久的保存在数据库之中,并不会被回滚。
指不管系统是否发生故障,事务处理的结果都是永久的。
一旦事务被提交,事务的效果会被永久地保留在数据库中。

总结:在事务管理中,原子性是基础,隔离性是手段,一致性是目的,持久性是结果。

3、事务隔离的操作

事务隔离级别的作用范围分为两种:

  • 全局级:对所有的会话有效
  • 会话级:只对当前的会话有效

(1)查询全局事务隔离级别

show global variables like '%isolation%';
SELECT @@global.tx_isolation;

(2)查询会话事务隔离级别


show session variables like '%isolation%';
SELECT @@session.tx_isolation; 
SELECT @@tx_isolation;

(3)设置全局事务隔离级别

set global transaction isolation level read committed;
set @@global.tx_isolation='read-committed';   #重启服务后失效

(4)设置会话事务隔离级别

set session transaction isolation level repeatable read;
set @@session.tx_isolation='repeatable-read';  #仅在当前会话中立即有效

4、事务控制语句

BEGIN 或 START TRANSACTION:显式地开启一个事务。
COMMIT 或 COMMIT WORK:提交事务,并使已对数据库进行的所有修改变为永久性的。
ROLLBACK 或 ROLLBACK WORK:回滚会结束用户的事务,并撤销正在进行的所有未提交的修改。
SAVEPOINT S1:使用 SAVEPOINT 允许在事务中创建一个回滚点,一个事务中可以有多个 SAVEPOINT;“S1”代表回滚点名称。
ROLLBACK TO [SAVEPOINT] S1:把事务回滚到标记点。

begin;

启一个事务,修改内容但不保存,退出重新登陆,数据未改变

mysql> use zx;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   20 |
+----+------+------+------+
1 row in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update zx set age=age-2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   18 |
+----+------+------+------+
1 row in set (0.00 sec)

mysql> quit
Bye
[root@zx2 ~]# mysql -u root -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44-log Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use zx;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   20 |
+----+------+------+------+
1 row in set (0.00 sec)

mysql>

begin;+commit;

开启事务,修改完提交事务,数据得到永久更改

mysql> use zx;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   20 |
+----+------+------+------+
1 row in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update zx set age=age-2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   18 |
+----+------+------+------+
1 row in set (0.00 sec)

mysql>

rollback;

开启一个事务,修改完数据进行回滚操作,会结束事务,并撤销未提交的修改

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   18 |
|  2 | zx   | 男   |   20 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update zx set age=age+22 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update zx set age=age-2 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   40 |
|  2 | zx   | 男   |   18 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   18 |
|  2 | zx   | 男   |   20 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql>

savepoint s1; 与 rollback to s1;

开启一个事务,修改数据后在此处创建一个回滚点无论之后如何修改,返回该回滚点,数据就会恢复

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   62 |
|  2 | zx   | 男   |   14 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update zx set age=age-2 where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update zx set age=age-2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> savepoint zx1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   60 |
|  2 | zx   | 男   |   12 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> update zx set age=age-2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update zx set age=age-2 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   58 |
|  2 | zx   | 男   |   10 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> rollback to zx1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   60 |
|  2 | zx   | 男   |   12 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql>

5、使用 set 设置控制事务

set global/sesion autocommit = 0/1 #golbal全局级别,session会话级别,0关闭自动提交,1开启自动提交
show global/session variables like 'autocommit';

创建测试表

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   60 |
|  2 | zx   | 男   |   12 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql>

默认情况下autocommit是开启的,会自动保持修改的内容

mysql> show global variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.01 sec)

mysql> show  session variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.00 sec)

关闭当前会话的自动提交

mysql> set session autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> show  session variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

mysql>

因为关闭了自动提交功能,修改内容后不会自动保存

mysql> update zx set age=age-2 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   60 |
|  2 | zx   | 男   |   10 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> quit
Bye
[root@zx2 ~]# mysql -u root -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44-log Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from zx.zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   60 |
|  2 | zx   | 男   |   12 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql>

如何在关闭自动提交后保存修改内容

mysql> select * from zx.zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   60 |
|  2 | zx   | 男   |   12 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> update zx.zx set age=age-2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update zx.zx set age=age-2 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from zx.zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   58 |
|  2 | zx   | 男   |   10 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql> commit;  ##手动提交即可
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@zx2 ~]# mysql -u root -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.44-log Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from zx.zx;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | scj  | 男   |   58 |
|  2 | zx   | 男   |   10 |
+----+------+------+------+
2 rows in set (0.00 sec)

mysql>

  • 18
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值