1. 并发设计中的四种现象
1.1 Lost Updates
应用A update了表的某一行,还没有提交的情况下,应用B update了同一行。这样A的update就丢失了。由于DB2会在要update的每一行上加锁,所以能完全避免Lost Updates现象。1.2 Uncommited Reads
如果应用A update了表的某一行,还没有提交的情况下,应用B就可以读取后修改之后的数据,这种现象叫做Uncommited Reads测试1
Session 1:
$ db2 "select * from test"
ID NAME
----------- --------------------
1 miao
2 qing
2 record(s) selected.
$ db2 +c "update test set name='song' where ID = 2"
DB20000I The SQL command completed successfully.Session 2:
$ db2 "select * from test where ID = 2 with UR"
ID NAME
----------- --------------------
2 song
1 record(s) selected. 1.3 Nonrepeatable Reads
在同一个事务中,第二次读取到的数据和第一次读取到的数据不一样(内容不同或者更少),这种现象叫Nonrepeatable Reads。例如,A第一次读取了3行数据,但并未对这3行数据加锁,这时候应用B删除/修改了其中一行,则A第二次读取的时候结果肯定与第一次不同。
测试2
Session 1:
$ db2 +c "select * from test"
ID NAME
----------- --------------------
1 miao
2 qing
3 song
3 record(s) selectedSesion 2:
$ db2 +c "delete from test where id = 2" DB20000I The SQL command completed successfully. $ db2 commit DB20000I The SQL command completed successfully.
Session 1: