(5)在客户端A执行更新语句update account set balance = balance - 50 where id =1,lilei的balance没有变成350,居然是400,是不是很奇怪,数据不 一致啊,如果你这么想就太天真 了,在应用程序中,并 不知道其他会话回滚了,要想解决这个问题可以采用读已提交的隔离级别
3、读已提交
(1)打开一个客户端A,并设置当前事务模式为read committed(读已提交 ),查询表account的所有记录: set tx_isolation=’read-committed’;
一个表是child表,一个是parent表,假设child表的某一列child_id映射到parent表的c_child_id列,那么从业务角度讲,此时我直接insert一条child_id=100记录到child表是存在风险的,因为刚insert的时候可能在parent表里删除了这条c_child_id=100的记录,那么业务数据就存在不一致的风险。正确的方法是再插入时执行select * from parent where c_child_id=100 lock in share mode,锁定了parent表的这条记录,然后执行insert into child(child_id) values (100)就ok了。
-- 查看近期死锁日志信息:show engine innodb status ===================================== 2021-12-2123:27:240x750c INNODB MONITOR OUTPUT ===================================== Persecond averages calculated from the last39 seconds ----------------- BACKGROUND THREAD ----------------- srv_master_thread loops: 698 srv_active, 0 srv_shutdown, 695589 srv_idle srv_master_thread log flush and writes: 696287 ---------- SEMAPHORES ---------- OS WAIT ARRAY INFO: reservation count 585 OS WAIT ARRAY INFO: signal count 545 RW-shared spins 548, rounds 960, OS waits 417 RW-excl spins 25, rounds 595, OS waits 27 RW-sx spins 3, rounds 61, OS waits 2 Spin rounds per wait: 1.75 RW-shared, 23.80 RW-excl, 20.33 RW-sx ------------------------ LATEST DETECTED DEADLOCK ------------------------ 2021-12-2123:27:010xe360 *** (1) TRANSACTION: TRANSACTION 399250, ACTIVE 30 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 1136, 2row lock(s) MySQL thread id 33, OS thread handle 23912, query id 802533 localhost ::1 root statistics select*from account where id=2for update *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 2822 page no5 n bits 80 index PRIMARYoftable `study_imysql_ndex`.`account` trx id 399250 lock_mode X locks rec but not gap waiting Record lock, heap no2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 6; hex 000000061775; asc u;; 2: len 7; hex a90000011e0110; asc ;; 3: len 4; hex 6e616d65; asc name;; 4: len 4; hex 80003e80; asc> ;;
*** (2) TRANSACTION: TRANSACTION 399251, ACTIVE 15 sec starting index read, thread declared inside InnoDB 5000 mysql tables in use 1, locked 1 3 lock struct(s), heap size 1136, 2row lock(s) MySQL thread id 34, OS thread handle 58208, query id 802537 localhost ::1 root statistics select*from account where id=1for update *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 2822 page no5 n bits 80 index PRIMARYoftable `study_imysql_ndex`.`account` trx id 399251 lock_mode X locks rec but not gap Record lock, heap no2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 6; hex 000000061775; asc u;; 2: len 7; hex a90000011e0110; asc ;; 3: len 4; hex 6e616d65; asc name;; 4: len 4; hex 80003e80; asc> ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 2822 page no5 n bits 80 index PRIMARYoftable `study_imysql_ndex`.`account` trx id 399251 lock_mode X locks rec but not gap waiting Record lock, heap no6 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000001; asc ;; 1: len 6; hex 000000061771; asc q;; 2: len 7; hex a7000001df0110; asc ;; 3: len 5; hex 6c696c6569; asc lilei;; 4: len 4; hex 800001c2; asc ;;
*** WE ROLL BACK TRANSACTION (2) ------------ TRANSACTIONS ------------ Trx id counter 399252 Purge done for trx's n:o < 399236 undo n:o < 0 state: running but idle History list length 540 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 283720369143296, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 283720369145040, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 283720369141552, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 399250, ACTIVE 53 sec 3 lock struct(s), heap size 1136, 2 row lock(s) MySQL thread id 33, OS thread handle 23912, query id 802544 localhost ::1 root -------- FILE I/O -------- I/O thread 0 state: wait Windows aio (insert buffer thread) I/O thread 1 state: wait Windows aio (log thread) I/O thread 2 state: wait Windows aio (read thread) I/O thread 3 state: wait Windows aio (read thread) I/O thread 4 state: wait Windows aio (read thread) I/O thread 5 state: wait Windows aio (read thread) I/O thread 6 state: wait Windows aio (write thread) I/O thread 7 state: wait Windows aio (write thread) I/O thread 8 state: wait Windows aio (write thread) I/O thread 9 state: wait Windows aio (write thread) Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] , ibuf aio reads:, log i/o's:, sync i/o's: Pending flushes (fsync) log: 0; buffer pool: 0 11388 OS file reads, 275231 OS file writes, 268926 OS fsyncs 0.00 reads/s, 0 avg bytes/read, 0.15 writes/s, 0.00 fsyncs/s ------------------------------------- INSERT BUFFER AND ADAPTIVE HASH INDEX ------------------------------------- Ibuf: size 1, free list len 113, seg size 115, 51 merges merged operations: insert 76, delete mark 78, delete 42 discarded operations: insert 0, delete mark 0, delete 0 Hash table size 2267, node heap has 1 buffer(s) Hash table size 2267, node heap has 1 buffer(s) Hash table size 2267, node heap has 1 buffer(s) Hash table size 2267, node heap has 1 buffer(s) Hash table size 2267, node heap has 1 buffer(s) Hash table size 2267, node heap has 1 buffer(s) Hash table size 2267, node heap has 3 buffer(s) Hash table size 2267, node heap has 1 buffer(s) 0.36 hash searches/s, 0.38 non-hash searches/s --- LOG --- Log sequence number 3913168090 Log flushed up to 3913168090 Pages flushed up to 3913168090 Last checkpoint at 3913168081 0 pending log flushes, 0 pending chkp writes 267248 log i/o's done, 0.00 log i/o's/second ---------------------- BUFFER POOL AND MEMORY ---------------------- Total large memory allocated 8585216 Dictionary memory allocated 3494975 Buffer pool size 512 Free buffers 246 Database pages 256 Old database pages 0 Modified db pages 0 Pending reads 0 Pending writes: LRU 0, flush list 0, single page 0 Pages made young 0, not young 0 0.00 youngs/s, 0.00 non-youngs/s Pages read 10815, created 2268, written 6967 0.00 reads/s, 0.00 creates/s, 0.15 writes/s Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000 Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s LRU len: 256, unzip_LRU len: 0 I/O sum[9]:cur[0], unzip sum[0]:cur[0] -------------- ROW OPERATIONS -------------- 0 queries inside InnoDB, 0 queries in queue 0 read views open inside InnoDB Process ID=5632, Main thread ID=0000000000000B1C , state=sleeping Number of rows inserted 279451, updated 682, deleted 187, read 1383947 0.15 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.97 reads/s ---------------------------- END OF INNODB MONITOR OUTPUT ============================