Table of contents
affairs
A transaction is a collection of operations. A transaction will submit or withdraw an operation request to the system together with all operations as a whole, that is, these operations either succeed at the same time or fail at the same time.
Basic operation:
-- 1. 查询张三账户余额
select * from account where name = '张三';
-- 2. 将张三账户余额-1000
update account set money = money - 1000 where name = '张三';
-- 此语句出错后张三钱减少但是李四钱没有增加
模拟sql语句错误
-- 3. 将李四账户余额+1000
update account set money = money + 1000 where name = '李四';
-- 查看事务提交方式
SELECT @@AUTOCOMMIT;
-- 设置事务提交方式,1为自动提交,0为手动提交,该设置只对当前会话有效
SET @@AUTOCOMMIT = 0;
-- 提交事务
COMMIT;
-- 回滚事务
ROLLBACK;
-- 设置手动提交后上面代码改为:
select * from account where name = '张三';
update account set money = money - 1000 where name = '张三';
update account set money = money + 1000 where name = '李四';
commit;
Operation method two:
-- 开启事务:
START TRANSACTION 或 BEGIN TRANSACTION;
-- 提交事务:
COMMIT;
-- 回滚事务:
ROLLBACK;
Operation example:
start transaction;
select * from account where name = '张三';
update account set money = money - 1000 where name = '张三';
update account set money = money + 1000 where name = '李四';
commit;
Four characteristics of ACID
- Atomicity: A transaction is an indivisible minimal operation that either succeeds or fails
- Consistency: When the transaction is completed, all data must be kept in a consistent state
- Isolation: The isolation mechanism provided by the database system ensures that transactions run in an independent environment that is not affected by external concurrent operations
- Durability: Once a transaction is committed or rolled back, its changes to the data in the database are permanent
concurrent transactions
question | describe |
---|---|
dirty read | A transaction reads data that has not yet been committed by another transaction |
non-repeatable read | A transaction reads the same record successively, but the data read twice is different |
Phantom reading | When a transaction queries data according to conditions, there is no corresponding data row, but when inserting data, it is found that this row of data already exists |
Concurrent transaction isolation level:
isolation level | dirty read | non-repeatable read | Phantom reading |
---|---|---|---|
Read uncommitted | √ | √ | √ |
Read committed | × | √ | √ |
Repeatable Read (default) | × | × | √ |
Serializable | × | × | × |
- √ indicates that the problem will occur under the current isolation level
- Serializable has the lowest performance; Read uncommitted has the highest performance and the worst data security
Check the transaction isolation level:
SELECT @@TRANSACTION_ISOLATION;
set the transaction isolation level:
SET [ SESSION | GLOBAL ] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE };
SESSION is the session level, which means it is only valid for the current session, and GLOBAL means it is valid for all sessions
I hope it will be helpful to you who are viewing the article, remember to pay attention, comment, and favorite, thank you