rlliso3multi.sql
来自「derby database source code.good for you.」· SQL 代码 · 共 496 行 · 第 1/2 页
SQL
496 行
connect 'wombat' as inserter;set isolation to rr;-- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.NoHoldForConnection;set connection scanner;autocommit off;create table test_5 (a int, b varchar(1000), c varchar(1000));insert into test_5 values (1, PADSTRING('a',1000), PADSTRING('a',1000));insert into test_5 values (2, PADSTRING('b',1000), PADSTRING('b',1000));create index test_5_idx on test_5 (a);commit;set connection inserter;autocommit off;commit;---------------------------------------------------------------------------------- Set up scanner to be doing a row locked index to base row scan on the index.-- By using group fetch it will read and release locks on multiple rows from-- the index and save away row pointers from the index.--------------------------------------------------------------------------------set connection scanner;CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault','10');get cursor scan_cursor as 'select a, b from test_5 where a <= 2 ';call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault', '16');-- because of group locking will get locks on 1, 2, 3, 4, and 5 and then will-- release the locks on 1, 2, 3, and 4. The last one is released on close or-- on next call emptying the cursor.next scan_cursor;---------------------------------------------------------------------------------- Insert a row previous to all other rows, this should block and back out.--------------------------------------------------------------------------------set connection inserter;insert into test_5 values (0, PADSTRING('b',1000), PADSTRING('b',1000));---------------------------------------------------------------------------------- The scan should finish fine without blocking.--------------------------------------------------------------------------------set connection scanner;next scan_cursor;next scan_cursor;-- commit the insertset connection inserter;commit;-- scanner should now see 1 and 2set connection scanner;close scan_cursor;select a from test_5;---------------------------------------------------------------------------------- cleanup.--------------------------------------------------------------------------------set connection inserter;commit;disconnect;set connection scanner;commit;drop table test_5;commit;disconnect;---------------------------------------------------------------------------------- Test 6: test update locks--------------------------------------------------------------------------------connect 'wombat' as t6scanner;set isolation to rr;-- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.NoHoldForConnection;connect 'wombat' as t6updater;set isolation to rr;-- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.NoHoldForConnection;connect 'wombat' as t6writer;set isolation to rr;-- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.NoHoldForConnection;run resource 'LockTableQuery.subsql';-- set upset connection t6updater;autocommit off;create table test_6 (a int, b int);insert into test_6 values (1,1);insert into test_6 values (2,2);insert into test_6 values (8,8);create index test6_idx on test_6 (a);commit;select * from lock_table order by tabname, type desc, mode, cnt, lockname;----==================================================-- t6updater gets an update lock on row where a=1--==================================================get cursor update_cursor as 'select b from test_6 where a=1 for update of b';select * from lock_table order by tabname, type desc, mode, cnt, lockname;next update_cursor;select * from lock_table order by tabname, type desc, mode, cnt, lockname;set connection t6scanner;autocommit off;------------------------------------------------------ try to scan the table, should timeout--------------------------------------------------select * from test_6;select * from lock_table order by tabname, type desc, mode, cnt, lockname;------------------------------------------------------ try to update the table, should timeout--------------------------------------------------update test_6 set b=99 where a = 1;------------------------------------------------------ try to update the table, should timeout (previous key is locked)--------------------------------------------------update test_6 set b=99 where a = 2;------------------------------------------------------ try to update the table, should go through--------------------------------------------------update test_6 set b=99 where a = 8;select * from lock_table order by tabname, type desc, mode, cnt, lockname;commit;select * from lock_table order by tabname, type desc, mode, cnt, lockname;------------------------------------------------------ try to get an update lock--------------------------------------------------get cursor update_cursor2 as 'select b from test_6 where a=1 for update of b';select * from lock_table order by tabname, type desc, mode, cnt, lockname;------------------------------------------------------ should timeout (other transaction has a shared lock on this row)--------------------------------------------------next update_cursor2;select * from lock_table order by tabname, type desc, mode, cnt, lockname;------------------------------------------------------ should succeed (no other transaction has a shared lock on this row)--------------------------------------------------get cursor update_cursor3 as 'select b from test_6 where a=8 for update of b';select type, cnt, mode, tabname, lockname, state from lock_table2 order by tabname, type desc, mode, cnt, lockname;next update_cursor3;select * from lock_table order by tabname, type desc, mode, cnt, lockname;commit;close update_cursor2;close update_cursor3;set connection t6updater;commit;close update_cursor;set connection t6scanner;select * from lock_table order by tabname, type desc, mode, cnt, lockname;----==================================================-- t6scanner gets a read lock--==================================================select b from test_6 where a=1;select * from lock_table order by tabname, type desc, mode, cnt, lockname;------------------------------------------------------ should succeed (can get an update lock if there is already a shared lock)--------------------------------------------------set connection t6updater;get cursor update_cursor as 'select b from test_6 where a=1 for update of b';next update_cursor;select * from lock_table order by tabname, type desc, mode, cnt, lockname;commit;close update_cursor;set connection t6scanner;commit;----==================================================-- t6writer gets a write lock--==================================================set connection t6writer;autocommit off;update test_6 set b=77 where a=2;select * from lock_table order by tabname, type desc, mode, cnt, lockname;set connection t6updater;get cursor update_cursor as 'select b from test_6 where a=2 for update of b';select * from lock_table order by tabname, type desc, mode, cnt, lockname;-------------------------------------------------------- should timeout----------------------------------------------------next update_cursor;---------------------------------------------------------------------------------- cleanup.--------------------------------------------------------------------------------set connection t6updater;close update_cursor;drop table test_6;commit;disconnect;set connection t6scanner;disconnect;set connection t6writer;disconnect;exit;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?