File: rlliso2multi.out

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (1288 lines) | stat: -rw-r--r-- 68,735 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
ij(CONNECTION1)> --
--   Licensed to the Apache Software Foundation (ASF) under one or more
--   contributor license agreements.  See the NOTICE file distributed with
--   this work for additional information regarding copyright ownership.
--   The ASF licenses this file to You under the Apache License, Version 2.0
--   (the "License"); you may not use this file except in compliance with
--   the License.  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing, software
--   distributed under the License is distributed on an "AS IS" BASIS,
--   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--   See the License for the specific language governing permissions and
--   limitations under the License.
--
--------------------------------------------------------------------------------
-- Test multi user lock interaction under isolation level 2.  default isolation
-- level has been set as a property to serializable.
--------------------------------------------------------------------------------
run resource '/org/apache/derbyTesting/functionTests/tests/store/createTestProcedures.subsql';
ij(CONNECTION1)> --
--   Licensed to the Apache Software Foundation (ASF) under one or more
--   contributor license agreements.  See the NOTICE file distributed with
--   this work for additional information regarding copyright ownership.
--   The ASF licenses this file to You under the Apache License, Version 2.0
--   (the "License"); you may not use this file except in compliance with
--   the License.  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing, software
--   distributed under the License is distributed on an "AS IS" BASIS,
--   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--   See the License for the specific language governing permissions and
--   limitations under the License.
--
CREATE FUNCTION  PADSTRING (DATA VARCHAR(32000), LENGTH INTEGER) RETURNS VARCHAR(32000) EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.Formatters.padString' LANGUAGE JAVA PARAMETER STYLE JAVA;
0 rows inserted/updated/deleted
ij(CONNECTION1)> CREATE PROCEDURE WAIT_FOR_POST_COMMIT() DYNAMIC RESULT SETS 0 LANGUAGE JAVA EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.T_Access.waitForPostCommitToFinish' PARAMETER STYLE JAVA;
0 rows inserted/updated/deleted
ij(CONNECTION1)> call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize', '4096');
0 rows inserted/updated/deleted
ij(CONNECTION1)> autocommit off;
ij(CONNECTION1)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(CONNECTION1)> --------------------------------------------------------------------------------
-- Test 0: verify isolation level by seeing if a read lock is released or not.
--------------------------------------------------------------------------------
connect 'wombat' as scanner;
ij(SCANNER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(SCANNER)> connect 'wombat' as writer;
ij(WRITER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(WRITER)> -- set up
set connection scanner;
ij(SCANNER)> autocommit off;
ij(SCANNER)> create table test_0 (a int);
0 rows inserted/updated/deleted
ij(SCANNER)> insert into test_0 values (1);
1 row inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> set connection writer;
ij(WRITER)> autocommit off;
ij(WRITER)> -- isolation 2 scanner should release read lock on table after statement.
set connection scanner;
ij(SCANNER)> select * from test_0;
A          
-----------
1          
ij(SCANNER)> -- writer should be able to insert into table - scanner released read lock.
set connection writer;
ij(WRITER)> insert into test_0 values (2);
1 row inserted/updated/deleted
ij(WRITER)> -- scanner will now block on uncommitted insert, and get lock timeout
set connection scanner;
ij(SCANNER)> select * from test_0;
A          
-----------
ERROR 40XL1: A lock could not be obtained within the time requested
ij(SCANNER)> commit;
ij(SCANNER)> -- commit writer - releasing all locks.
set connection writer;
ij(WRITER)> commit;
ij(WRITER)> -- scanner will now see 2 rows
set connection scanner;
ij(SCANNER)> select * from test_0;
A          
-----------
1          
2          
ij(SCANNER)> commit;
ij(SCANNER)> -- cleanup
set connection scanner;
ij(SCANNER)> drop table test_0;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> disconnect;
ij> set connection writer;
ij(WRITER)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 1: make sure a leaf root growing get's the right lock.
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- Test setup - create a 1 page btree, with the page ready to split.
--------------------------------------------------------------------------------
connect 'wombat' as scanner;
ij(SCANNER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(SCANNER)> connect 'wombat' as rootgrower;
ij(ROOTGROWER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(ROOTGROWER)> set connection scanner;
ij(SCANNER)> autocommit off;
ij(SCANNER)> create table a (a varchar(1200), b varchar(1000)) ;
0 rows inserted/updated/deleted
ij(SCANNER)> insert into a values (PADSTRING('a',1200), PADSTRING('a',1000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into a values (PADSTRING('b',1200), PADSTRING('b',1000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into a values (PADSTRING('c',1200), PADSTRING('c',1000));
1 row inserted/updated/deleted
ij(SCANNER)> create index a_idx on a (a) ;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> set connection rootgrower;
ij(ROOTGROWER)> autocommit off;
ij(ROOTGROWER)> commit;
ij(ROOTGROWER)> --------------------------------------------------------------------------------
-- Set up scanner to be doing a row locked covered scan on the index.
--------------------------------------------------------------------------------
set connection scanner;
ij(SCANNER)> autocommit off;
ij(SCANNER)> CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault','1');
0 rows inserted/updated/deleted
ij(SCANNER)> get cursor scan_cursor as 
    'select a from a where a >= PADSTRING(''a'',1200) and a < PADSTRING(''c'',1200) ';
ij(SCANNER)> call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault', '16');
0 rows inserted/updated/deleted
ij(SCANNER)> next scan_cursor;
A                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------
a                                                                                                                              &
ij(SCANNER)> --------------------------------------------------------------------------------
-- Before DERBY-2991 the attempt to split the root would time out because the
-- scan had locked the first page.
--------------------------------------------------------------------------------
set connection rootgrower;
ij(ROOTGROWER)> autocommit off;
ij(ROOTGROWER)> insert into a values (PADSTRING('d',1200), PADSTRING('d',1000));
1 row inserted/updated/deleted
ij(ROOTGROWER)> rollback;
ij(ROOTGROWER)> --------------------------------------------------------------------------------
-- The scan should continue unaffected.
--------------------------------------------------------------------------------
set connection scanner;
ij(SCANNER)> next scan_cursor;
A                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------
b                                                                                                                              &
ij(SCANNER)> next scan_cursor;
No current row
ij(SCANNER)> --------------------------------------------------------------------------------
-- This insert will block on the previous key lock of the scanner.
--------------------------------------------------------------------------------
set connection rootgrower;
ij(ROOTGROWER)> insert into a values (PADSTRING('ab',1200), PADSTRING('ab',1000));
1 row inserted/updated/deleted
ij(ROOTGROWER)> --------------------------------------------------------------------------------
-- Now the grow root should be allowed (note that cursor scan has locks
-- on the leaf page being grown - just not the scan lock).
-- (Scan locks are no longer used after DERBY-2991.)
--------------------------------------------------------------------------------
set connection rootgrower;
ij(ROOTGROWER)> insert into a values (PADSTRING('d',1200), PADSTRING('d',1000));
1 row inserted/updated/deleted
ij(ROOTGROWER)> select a from a;
A                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------
a                                                                                                                              &
ab                                                                                                                             &
b                                                                                                                              &
c                                                                                                                              &
d                                                                                                                              &
ij(ROOTGROWER)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------
set connection rootgrower;
ij(ROOTGROWER)> commit;
ij(ROOTGROWER)> disconnect;
ij> set connection scanner;
ij(SCANNER)> commit;
ij(SCANNER)> drop table a;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 2: make sure previous key locks are gotten correctly.
--------------------------------------------------------------------------------
connect 'wombat' as client_1;
ij(CLIENT_1)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(CLIENT_1)> connect 'wombat' as client_2;
ij(CLIENT_2)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(CLIENT_2)> set connection client_1;
ij(CLIENT_1)> autocommit off;
ij(CLIENT_1)> create table a (a varchar(1000), b varchar(1000)) ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> create unique index a_idx on a (a) ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> insert into a values (PADSTRING('b',1000), PADSTRING('b',1000));
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into a values (PADSTRING('c',1000), PADSTRING('c',1000));
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into a values (PADSTRING('e',1000), PADSTRING('e',1000));
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into a values (PADSTRING('f',1000), PADSTRING('f',1000));
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into a values (PADSTRING('g',1000), PADSTRING('g',1000));
1 row inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> autocommit off;
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- client 1 will get exclusive locks on 'c'.
--------------------------------------------------------------------------------
set connection client_1;
ij(CLIENT_1)> update a set b = 'new value' where a > 'b' and a <= 'd';
1 row inserted/updated/deleted
ij(CLIENT_1)> -- run resource '/org/apache/derbyTesting/functionTests/tests/store/LockTableQuery.subsql';

set connection client_2;
ij(CLIENT_2)> --------------------------------------------------------------------------------

-- the following will not time out, the insert
-- will get a previous key insert lock which will not conflict with the
-- non-insert read-committed exclusive lock on 'c'.

--------------------------------------------------------------------------------
insert into a values (PADSTRING('d',1000), PADSTRING('d',1000));
1 row inserted/updated/deleted
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- the following should NOT cause a time out
--------------------------------------------------------------------------------
insert into a values (PADSTRING('a',1000), PADSTRING('a',1000));
1 row inserted/updated/deleted
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- the following will block because it is a unique index, and the insert is of
-- the same row being locked by client_1
--------------------------------------------------------------------------------
insert into a values (PADSTRING('c',1000), PADSTRING('c',1000));
ERROR 40XL1: A lock could not be obtained within the time requested
ij(CLIENT_2)> -- run resource '/org/apache/derbyTesting/functionTests/tests/store/LockTableQuery.subsql';

--------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------
set connection client_1;
ij(CLIENT_1)> select * from a;
A                                                                                                                               |B                                                                                                                               
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
b                                                                                                                              &|b                                                                                                                              &
c                                                                                                                              &|new value                                                                                                                       
e                                                                                                                              &|e                                                                                                                              &
f                                                                                                                              &|f                                                                                                                              &
g                                                                                                                              &|g                                                                                                                              &
ij(CLIENT_1)> commit;
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> commit;
ij(CLIENT_2)> select * from a;
A                                                                                                                               |B                                                                                                                               
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
b                                                                                                                              &|b                                                                                                                              &
c                                                                                                                              &|new value                                                                                                                       
e                                                                                                                              &|e                                                                                                                              &
f                                                                                                                              &|f                                                                                                                              &
g                                                                                                                              &|g                                                                                                                              &
ij(CLIENT_2)> drop table a;
0 rows inserted/updated/deleted
ij(CLIENT_2)> commit;
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- Test 3: make sure an exact key insert into unique key index blocks.
--------------------------------------------------------------------------------
set connection client_1;
ij(CLIENT_1)> autocommit off;
ij(CLIENT_1)> create table a (a varchar(1000), b varchar(1000)) ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> create unique index a_idx on a (a) ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> insert into a values (PADSTRING('b',1000), PADSTRING('b',1000));
1 row inserted/updated/deleted
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> autocommit off;
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- the following should cause a time out, as the previous
-- key lock will conflict with client_1's lock on 'b'
--------------------------------------------------------------------------------
insert into a values (PADSTRING('b',1000), PADSTRING('b',1000));
ERROR 40XL1: A lock could not be obtained within the time requested
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- Test 4: make sure that row lock wait in a heap scan works
--------------------------------------------------------------------------------
set connection client_1;
ij(CLIENT_1)> autocommit off;
ij(CLIENT_1)> create table test_4 (a int, b varchar(1000), c varchar(1000)) ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> autocommit off;
ij(CLIENT_2)> commit;
ij(CLIENT_2)> -- client_1 will get a single row lock in the heap.
set connection client_1;
ij(CLIENT_1)> insert into test_4 values (1, PADSTRING('a',1000), PADSTRING('b',1000));
1 row inserted/updated/deleted
ij(CLIENT_1)> -- client_2 scans table, blocking on a row lock on the client_1 insert row, 
-- will get timeout message.
set connection client_2;
ij(CLIENT_2)> select * from test_4;
A          |B                                                                                                                               |C                                                                                                                               
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ERROR 40XL1: A lock could not be obtained within the time requested
ij(CLIENT_2)> -- release the insert lock.
set connection client_1;
ij(CLIENT_1)> commit;
ij(CLIENT_1)> -- reader should be able to see row now.
set connection client_2;
ij(CLIENT_2)> select * from test_4;
A          |B                                                                                                                               |C                                                                                                                               
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1          |a                                                                                                                              &|b                                                                                                                              &
ij(CLIENT_2)> commit;
ij(CLIENT_2)> -- cleanup
set connection client_1;
ij(CLIENT_1)> drop table test_4;
0 rows inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> --------------------------------------------------------------------------------
-- Test 5: make sure a that a group fetch through a secondary index correctly
--         handles a row that is deleted after it has read a row from the index
--         but before it has read the row from the base table.
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- Test setup - create a 1 page btre, with the page ready to split.
--------------------------------------------------------------------------------
connect 'wombat' as scanner;
ij(SCANNER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(SCANNER)> connect 'wombat' as deleter;
ij(DELETER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(DELETER)> set connection scanner;
ij(SCANNER)> autocommit off;
ij(SCANNER)> create table test_5 (a int, a2 int, b varchar(1000), c varchar(1000)) ;
0 rows inserted/updated/deleted
ij(SCANNER)> insert into test_5 values (1, 10, PADSTRING('a',1000), PADSTRING('a',1000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_5 values (2, 20, PADSTRING('b',1000), PADSTRING('b',1000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_5 values (3, 30, PADSTRING('c',1000), PADSTRING('c',1000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_5 values (4, 40, PADSTRING('d',1000), PADSTRING('d',1000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_5 values (5, 50, PADSTRING('e',1000), PADSTRING('e',1000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_5 values (6, 60, PADSTRING('f',1000), PADSTRING('f',1000));
1 row inserted/updated/deleted
ij(SCANNER)> create index test_5_idx on test_5 (a);
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> set connection deleter;
ij(DELETER)> autocommit off;
ij(DELETER)> commit;
ij(DELETER)> --------------------------------------------------------------------------------
-- 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;
ij(SCANNER)> CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault','10');
0 rows inserted/updated/deleted
ij(SCANNER)> get cursor scan_cursor as 
    'select a, a2 from test_5 where a > 1 ';
ij(SCANNER)> call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault', '16');
0 rows inserted/updated/deleted
ij(SCANNER)> -- 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;
A          |A2         
-----------------------
2          |20         
ij(SCANNER)> --------------------------------------------------------------------------------
-- Delete a row that the scanner has looked at but not reported back to the
-- caller.
--------------------------------------------------------------------------------
set connection deleter;
ij(DELETER)> delete from test_5 where a = 4;
1 row inserted/updated/deleted
ij(DELETER)> --------------------------------------------------------------------------------
-- The scan will requalify rows when it goes to the base table, thus it will
-- see 3, but block when it gets to the key of deleted row (4).
--------------------------------------------------------------------------------
set connection scanner;
ij(SCANNER)> next scan_cursor;
A          |A2         
-----------------------
3          |30         
ij(SCANNER)> next scan_cursor;
ERROR 40XL1: A lock could not be obtained within the time requested
ij(SCANNER)> -- commit the delete
set connection deleter;
ij(DELETER)> commit;
ij(DELETER)> -- scanner should see 1,2,3,4,6
set connection scanner;
ij(SCANNER)> close scan_cursor;
ij(SCANNER)> select a,b from test_5;
A          |B                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------------------
1          |a                                                                                                                              &
2          |b                                                                                                                              &
3          |c                                                                                                                              &
5          |e                                                                                                                              &
6          |f                                                                                                                              &
ij(SCANNER)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------
set connection deleter;
ij(DELETER)> commit;
ij(DELETER)> disconnect;
ij> set connection scanner;
ij(SCANNER)> commit;
ij(SCANNER)> drop table test_5;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 6: make sure a that heap scans which cross page boundaries release
--         locks correctly.
--------------------------------------------------------------------------------

-- Test setup - create a heap with one row per page.

connect 'wombat' as scanner;
ij(SCANNER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(SCANNER)> connect 'wombat' as deleter;
ij(DELETER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(DELETER)> set connection scanner;
ij(SCANNER)> autocommit off;
ij(SCANNER)> create table test_6 (a int, a2 int, b varchar(2000), c varchar(2000)) ;
0 rows inserted/updated/deleted
ij(SCANNER)> insert into test_6 values (1, 10, PADSTRING('a',2000), PADSTRING('a',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_6 values (2, 20, PADSTRING('b',2000), PADSTRING('b',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_6 values (3, 30, PADSTRING('c',2000), PADSTRING('c',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_6 values (4, 40, PADSTRING('d',2000), PADSTRING('d',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_6 values (5, 50, PADSTRING('e',2000), PADSTRING('e',2000));
1 row inserted/updated/deleted
ij(SCANNER)> create index test_6_idx on test_6 (a);
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> set connection deleter;
ij(DELETER)> autocommit off;
ij(DELETER)> commit;
ij(DELETER)> --------------------------------------------------------------------------------
-- 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;
ij(SCANNER)> get cursor scan_cursor as 
    'select a, a2 from test_6';
ij(SCANNER)> next scan_cursor;
A          |A2         
-----------------------
1          |10         
ij(SCANNER)> next scan_cursor;
A          |A2         
-----------------------
2          |20         
ij(SCANNER)> next scan_cursor;
A          |A2         
-----------------------
3          |30         
ij(SCANNER)> next scan_cursor;
A          |A2         
-----------------------
4          |40         
ij(SCANNER)> next scan_cursor;
A          |A2         
-----------------------
5          |50         
ij(SCANNER)> --------------------------------------------------------------------------------
-- Delete all rows that the scanner has looked at, and should have released the
-- lock on.
--------------------------------------------------------------------------------
set connection deleter;
ij(DELETER)> delete from test_6 where a = 1;
1 row inserted/updated/deleted
ij(DELETER)> delete from test_6 where a = 2;
1 row inserted/updated/deleted
ij(DELETER)> delete from test_6 where a = 3;
1 row inserted/updated/deleted
ij(DELETER)> delete from test_6 where a = 4;
1 row inserted/updated/deleted
ij(DELETER)> --------------------------------------------------------------------------------
-- The scan should either block on the delete or continue and not return the
-- the deleted row.
--------------------------------------------------------------------------------
set connection scanner;
ij(SCANNER)> next scan_cursor;
No current row
ij(SCANNER)> close scan_cursor;
ij(SCANNER)> -- commit the delete
set connection deleter;
ij(DELETER)> delete from test_6 where a = 5;
1 row inserted/updated/deleted
ij(DELETER)> commit;
ij(DELETER)> -- scanner should see no rows.
set connection scanner;
ij(SCANNER)> select a,b from test_6;
A          |B                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------------------
ij(SCANNER)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------
set connection deleter;
ij(DELETER)> commit;
ij(DELETER)> disconnect;
ij> set connection scanner;
ij(SCANNER)> commit;
ij(SCANNER)> drop table test_6;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 7: make sure that 2 heap cursor scans in same transaction work (at one
--         point there was a problem where releasing locks in one of the cursors
--         released locks in the other cursor).
--------------------------------------------------------------------------------

-- Test setup - create a heap with one row per page.

connect 'wombat' as scanner;
ij(SCANNER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(SCANNER)> connect 'wombat' as deleter;
ij(DELETER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(DELETER)> --------------------------------------------------------------------------------
-- HEAP SCAN
--------------------------------------------------------------------------------
set connection scanner;
ij(SCANNER)> autocommit off;
ij(SCANNER)> create table test_7 (a int, a2 int, b varchar(2000), c varchar(2000)) ;
0 rows inserted/updated/deleted
ij(SCANNER)> insert into test_7 values (1, 10, PADSTRING('a',2000), PADSTRING('a',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_7 values (2, 20, PADSTRING('b',2000), PADSTRING('b',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_7 values (3, 30, PADSTRING('c',2000), PADSTRING('c',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_7 values (4, 40, PADSTRING('d',2000), PADSTRING('d',2000));
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_7 values (5, 50, PADSTRING('e',2000), PADSTRING('e',2000));
1 row inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> set connection deleter;
ij(DELETER)> autocommit off;
ij(DELETER)> commit;
ij(DELETER)> -- Set up scanner to be doing a row locked heap scan, going one row at a time. 
set connection scanner;
ij(SCANNER)> CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault','1');
0 rows inserted/updated/deleted
ij(SCANNER)> get cursor scan_cursor_1 as 
    'select a, a2 from test_7';
ij(SCANNER)> get cursor scan_cursor_2 as 
    'select a, a2 from test_7';
ij(SCANNER)> call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault', '16');
0 rows inserted/updated/deleted
ij(SCANNER)> next scan_cursor_1;
A          |A2         
-----------------------
1          |10         
ij(SCANNER)> next scan_cursor_1;
A          |A2         
-----------------------
2          |20         
ij(SCANNER)> next scan_cursor_1;
A          |A2         
-----------------------
3          |30         
ij(SCANNER)> next scan_cursor_1;
A          |A2         
-----------------------
4          |40         
ij(SCANNER)> next scan_cursor_1;
A          |A2         
-----------------------
5          |50         
ij(SCANNER)> next scan_cursor_2;
A          |A2         
-----------------------
1          |10         
ij(SCANNER)> close scan_cursor_2;
ij(SCANNER)> -- Get exclusive table lock on test_7.  Should fail with table cannot be locked.
set connection deleter;
ij(DELETER)> lock table test_7 in exclusive mode;
ERROR X0X02: Table 'TEST_7' cannot be locked in 'EXCLUSIVE' mode.
ERROR 40XL1: A lock could not be obtained within the time requested
ij(DELETER)> -- release all read locks, by moving the cursor past all the rows.
set connection scanner;
ij(SCANNER)> next scan_cursor_1;
No current row
ij(SCANNER)> close scan_cursor_1;
ij(SCANNER)> -- Get exclusive table lock on test_7.  Now that both scan closed this should
-- work.
set connection deleter;
ij(DELETER)> delete from test_7;
5 rows inserted/updated/deleted
ij(DELETER)> commit;
ij(DELETER)> -- scanner should see no rows.
set connection scanner;
ij(SCANNER)> select a,b from test_7;
A          |B                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------------------
ij(SCANNER)> commit;
ij(SCANNER)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------
set connection deleter;
ij(DELETER)> commit;
ij(DELETER)> disconnect;
ij> set connection scanner;
ij(SCANNER)> commit;
ij(SCANNER)> drop table test_7;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 8: Exercise post commit cases, force the code through the path, no easy
--         way to make sure the post commit work is actually doing something.
--         All these case were run with lock debugging by hand to make sure the
--         right thing was happening:
--         
--         8.1 - heap post commit successfully gets table X lock and cleans up.
--         8.2 - heap post commit can't get table X lock, so gives up and let's
--               client continue on with work.
--         8.3 - btree post commit successfully gets table X lock and cleans up.
--         8.4 - btree post commit can't get table X lock, so gives up and let's
--               client continue on with work.
--               client continue on with work.
--
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- 8.1 setup 
--------------------------------------------------------------------------------
set connection client_1;
ij(CLIENT_1)> create table test_8 (a int, a2 int, b varchar(2000), c char(10)) 
    ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> create index test_8_idx on test_8 (a);
0 rows inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (1, 10, PADSTRING('a',2000), 'test 8.1');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (2, 20, PADSTRING('b',2000), 'test 8.1');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (3, 30, PADSTRING('c',2000), 'test 8.1');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (4, 40, PADSTRING('d',2000), 'test 8.1');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (5, 50, PADSTRING('e',2000), 'test 8.1');
1 row inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> --------------------------------------------------------------------------------
-- 8.1 test - simply delete rows from table, heap post commit will run and 
--            reclaim all pages.
--------------------------------------------------------------------------------

set connection client_1;
ij(CLIENT_1)> delete from test_8;
5 rows inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> select a from test_8;
A          
-----------
ij(CLIENT_1)> commit;
ij(CLIENT_1)> --------------------------------------------------------------------------------
-- 8.2 setup 
--------------------------------------------------------------------------------
drop table test_8;
0 rows inserted/updated/deleted
ij(CLIENT_1)> create table test_8 (a int, a2 int, b varchar(1000), c char(10))
    ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> create index test_8_idx on test_8 (a);
0 rows inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (1, 10, PADSTRING('a',1000), 'test 8.2');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (2, 20, PADSTRING('b',1000), 'test 8.2');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (3, 30, PADSTRING('c',1000), 'test 8.2');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (4, 40, PADSTRING('d',1000), 'test 8.2');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (5, 50, PADSTRING('e',1000), 'test 8.2');
1 row inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> --------------------------------------------------------------------------------
-- 8.2 test - client 1 holds row lock which will prevent client 2 post commit.
--------------------------------------------------------------------------------

set connection client_1;
ij(CLIENT_1)> insert into test_8 values (6, 60, PADSTRING('a',1000), 'test 8.2');
1 row inserted/updated/deleted
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> delete from test_8 where a < 5;
4 rows inserted/updated/deleted
ij(CLIENT_2)> commit;
ij(CLIENT_2)> set connection client_1;
ij(CLIENT_1)> select a from test_8;
A          
-----------
5          
6          
ij(CLIENT_1)> commit;
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> select a from test_8;
A          
-----------
5          
6          
ij(CLIENT_2)> commit;
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- 8.3 setup 
--------------------------------------------------------------------------------
drop table test_8;
0 rows inserted/updated/deleted
ij(CLIENT_2)> create table test_8 (a int, a2 int, b varchar(1500), c char(10));
0 rows inserted/updated/deleted
ij(CLIENT_2)> create index test_8_idx on test_8 (a, b)
    ;
0 rows inserted/updated/deleted
ij(CLIENT_2)> insert into test_8 values (1, 10, PADSTRING('a',1500), 'test 8.3');
1 row inserted/updated/deleted
ij(CLIENT_2)> insert into test_8 values (2, 20, PADSTRING('b',1500), 'test 8.3');
1 row inserted/updated/deleted
ij(CLIENT_2)> insert into test_8 values (3, 30, PADSTRING('c',1500), 'test 8.3');
1 row inserted/updated/deleted
ij(CLIENT_2)> insert into test_8 values (4, 40, PADSTRING('d',1500), 'test 8.3');
1 row inserted/updated/deleted
ij(CLIENT_2)> insert into test_8 values (5, 50, PADSTRING('e',1500), 'test 8.3');
1 row inserted/updated/deleted
ij(CLIENT_2)> commit;
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- 8.3 test - simply delete rows from index table, btree post commit will run
--            and reclaim all pages.
--------------------------------------------------------------------------------

set connection client_1;
ij(CLIENT_1)> delete from test_8;
5 rows inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> select a from test_8;
A          
-----------
ij(CLIENT_1)> commit;
ij(CLIENT_1)> --------------------------------------------------------------------------------
-- 8.4 setup 
--------------------------------------------------------------------------------
drop table test_8;
0 rows inserted/updated/deleted
ij(CLIENT_1)> create table test_8 (a int, a2 int, b varchar(1500), c char(10)) ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> create index test_8_idx1 on test_8 (a);
0 rows inserted/updated/deleted
ij(CLIENT_1)> create index test_8_idx2 on test_8 (a, b)
    ;
0 rows inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (1, 10, PADSTRING('a',1500), 'test 8.4');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (2, 20, PADSTRING('b',1500), 'test 8.4');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (3, 30, PADSTRING('c',1500), 'test 8.4');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (4, 40, PADSTRING('d',1500), 'test 8.4');
1 row inserted/updated/deleted
ij(CLIENT_1)> insert into test_8 values (5, 50, PADSTRING('e',1500), 'test 8.4');
1 row inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> --------------------------------------------------------------------------------
-- 8.4 test - client 1 holds row lock which will prevent client 2 post commit.
--------------------------------------------------------------------------------

set connection client_1;
ij(CLIENT_1)> insert into test_8 values (6, 60, PADSTRING('a',1500), 'test 8.4');
1 row inserted/updated/deleted
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> delete from test_8 where a < 5;
4 rows inserted/updated/deleted
ij(CLIENT_2)> commit;
ij(CLIENT_2)> set connection client_1;
ij(CLIENT_1)> select a from test_8;
A          
-----------
5          
6          
ij(CLIENT_1)> commit;
ij(CLIENT_1)> set connection client_2;
ij(CLIENT_2)> select a from test_8;
A          
-----------
5          
6          
ij(CLIENT_2)> commit;
ij(CLIENT_2)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------

set connection client_1;
ij(CLIENT_1)> drop table test_8;
0 rows inserted/updated/deleted
ij(CLIENT_1)> commit;
ij(CLIENT_1)> disconnect;
ij> set connection client_2;
ij(CLIENT_2)> commit;
ij(CLIENT_2)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 9: Make sure scan positioning in the beginning of a unique scan
--         properly gets the scan lock to block with splits.
--         (Scan locks are no longer used after DERBY-2991.)
--
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- 9.1 setup 
--------------------------------------------------------------------------------
connect 'wombat' as scanner;
ij(SCANNER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(SCANNER)> autocommit off;
ij(SCANNER)> commit;
ij(SCANNER)> connect 'wombat' as splitter;
ij(SPLITTER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(SPLITTER)> autocommit off;
ij(SPLITTER)> commit;
ij(SPLITTER)> set connection scanner;
ij(SCANNER)> create table test_9 (a int, a2 int, b varchar(1000), c char(10)) 
    ;
0 rows inserted/updated/deleted
ij(SCANNER)> insert into test_9 values (5, 50, PADSTRING('e',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_9 values (4, 40, PADSTRING('d',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_9 values (3, 30, PADSTRING('c',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_9 values (2, 20, PADSTRING('b',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SCANNER)> insert into test_9 values (1, 10, PADSTRING('a',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SCANNER)> create unique index test_9_idx on test_9 (b) ;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> --------------------------------------------------------------------------------
-- 9.1 test - open a cursor for update on table, and make sure splitter waits
--            on the scan position.
--------------------------------------------------------------------------------

set connection scanner;
ij(SCANNER)> CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault','1');
0 rows inserted/updated/deleted
ij(SCANNER)> get cursor scan_cursor as
    'select b from test_9 where b >= ''a'' ';
ij(SCANNER)> call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.language.bulkFetchDefault', '16');
0 rows inserted/updated/deleted
ij(SCANNER)> next scan_cursor;
B                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------
b                                                                                                                              &
ij(SCANNER)> next scan_cursor;
B                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------
c                                                                                                                              &
ij(SCANNER)> -- the following will get a couple of rows and then block on the split.
set connection splitter;
ij(SPLITTER)> insert into test_9 values (0, 10, PADSTRING('aa',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SPLITTER)> commit;
ij(SPLITTER)> insert into test_9 values (0, 10, PADSTRING('ab',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SPLITTER)> commit;
ij(SPLITTER)> -- insert ahead in the cursor to make sure we pick it up later.
-- This would time out before DERBY-2991.
insert into test_9 values (0, 10, PADSTRING('dd',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(SPLITTER)> rollback;
ij(SPLITTER)> set connection scanner;
ij(SCANNER)> next scan_cursor;
B                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------
d                                                                                                                              &
ij(SCANNER)> next scan_cursor;
B                                                                                                                               
--------------------------------------------------------------------------------------------------------------------------------
e                                                                                                                              &
ij(SCANNER)> next scan_cursor;
No current row
ij(SCANNER)> next scan_cursor;
No current row
ij(SCANNER)> commit;
ij(SCANNER)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------

set connection scanner;
ij(SCANNER)> drop table test_9;
0 rows inserted/updated/deleted
ij(SCANNER)> commit;
ij(SCANNER)> disconnect;
ij> set connection splitter;
ij(SPLITTER)> commit;
ij(SPLITTER)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 10: Make sure a ddl does not block the lock table vti.
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- 10 setup 
--------------------------------------------------------------------------------
connect 'wombat' as ddl;
ij(DDL)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(DDL)> autocommit off;
ij(DDL)> commit;
ij(DDL)> connect 'wombat' as locktable;
ij(LOCKTABLE)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(LOCKTABLE)> autocommit off;
ij(LOCKTABLE)> commit;
ij(LOCKTABLE)> set connection ddl;
ij(DDL)> run resource '/org/apache/derbyTesting/functionTests/tests/store/LockTableQuery.subsql';
ij(DDL)> --
--   Licensed to the Apache Software Foundation (ASF) under one or more
--   contributor license agreements.  See the NOTICE file distributed with
--   this work for additional information regarding copyright ownership.
--   The ASF licenses this file to You under the Apache License, Version 2.0
--   (the "License"); you may not use this file except in compliance with
--   the License.  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing, software
--   distributed under the License is distributed on an "AS IS" BASIS,
--   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--   See the License for the specific language governing permissions and
--   limitations under the License.
--
create view lock_table as
select 
    cast(username as char(8)) as username,
    cast(t.type as char(15)) as trantype,
    cast(l.type as char(8)) as type,
    cast(lockcount as char(3)) as cnt,
    mode,
    cast(tablename as char(12)) as tabname,
    cast(lockname as char(10)) as lockname,
    state,
    status
from 
    syscs_diag.lock_table l  right outer join syscs_diag.transaction_table t
on l.xid = t.xid where l.tableType <> 'S' and t.type='UserTransaction';
0 rows inserted/updated/deleted
ij(DDL)> --on l.xid = t.xid where l.tableType <> 'S' or l.tableType is null
-- order by
--     tabname, type desc, mode, cnt, lockname

-- lock table with system catalog locks included.
create view full_lock_table as
select 
    cast(username as char(8)) as username,
    cast(t.type as char(8)) as trantype,
    cast(l.type as char(8)) as type,
    cast(lockcount as char(3)) as cnt,
    mode,
    cast(tablename as char(12)) as tabname,
    cast(lockname as char(10)) as lockname,
    state,
    status
from 
    syscs_diag.lock_table l right outer join syscs_diag.transaction_table t
on l.xid = t.xid where l.tableType <> 'S' ;
0 rows inserted/updated/deleted
ij(DDL)> -- lock table with no join.
-- DERBY-6183, removing CAST's to avoid background locks causing
-- data trunction warnings.

create view lock_table2 as
select 
    xid,
    type,
    lockcount as cnt,
    mode,
    tablename as tabname,
    lockname,
    state
from 
    syscs_diag.lock_table
where tableType <> 'S' ;
0 rows inserted/updated/deleted
ij(DDL)> -- transaction table with no join.
create view tran_table as
select 
    *
from 
    syscs_diag.transaction_table;
0 rows inserted/updated/deleted
ij(DDL)> commit;
ij(DDL)> --------------------------------------------------------------------------------
-- 10 test - do ddl in one connection and look at lock table in another 
--           connection.
--------------------------------------------------------------------------------

set connection ddl;
ij(DDL)> create table test_10 (a int, a2 int, b varchar(1000), c char(10)) 
    ;
0 rows inserted/updated/deleted
ij(DDL)> insert into test_10 values (4, 40, PADSTRING('d',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(DDL)> insert into test_10 values (3, 30, PADSTRING('c',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(DDL)> insert into test_10 values (2, 20, PADSTRING('b',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(DDL)> insert into test_10 values (1, 10, PADSTRING('a',1000), 'test 9.1');
1 row inserted/updated/deleted
ij(DDL)> set connection locktable;
ij(LOCKTABLE)> -- this should not block on the other thread.
select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |4   |IX  |TEST_10     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|TABLE   |1   |X   |TEST_10     |Tablelock |GRANT|ACTIVE  
ij(LOCKTABLE)> commit;
ij(LOCKTABLE)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------

set connection ddl;
ij(DDL)> drop table test_10;
0 rows inserted/updated/deleted
ij(DDL)> commit;
ij(DDL)> disconnect;
ij> set connection locktable;
ij(LOCKTABLE)> commit;
ij(LOCKTABLE)> disconnect;
ij> --------------------------------------------------------------------------------
-- Test 11: test update locks
--------------------------------------------------------------------------------

connect 'wombat' as t11scanner;
ij(T11SCANNER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(T11SCANNER)> connect 'wombat' as t11updater;
ij(T11UPDATER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(T11UPDATER)> connect 'wombat' as t11writer;
ij(T11WRITER)> -- by default, holdability of ResultSet objects created using this Connection object is true. Following will set it to false for this connection.
NoHoldForConnection;
ij(T11WRITER)> -- set up
set connection t11updater;
ij(T11UPDATER)> autocommit off;
ij(T11UPDATER)> create table test_11 (a int, b int);
0 rows inserted/updated/deleted
ij(T11UPDATER)> insert into test_11 values (1,1);
1 row inserted/updated/deleted
ij(T11UPDATER)> insert into test_11 values (2,2);
1 row inserted/updated/deleted
ij(T11UPDATER)> insert into test_11 values (8,8);
1 row inserted/updated/deleted
ij(T11UPDATER)> create index test11_idx on test_11 (a);
0 rows inserted/updated/deleted
ij(T11UPDATER)> commit;
ij(T11UPDATER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
ij(T11UPDATER)> --
--==================================================
-- t11updater gets an update lock on row where a=1
--==================================================

get cursor update_cursor as
    'select b from test_11 where a=1 for update of b';
ij(T11UPDATER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
ij(T11UPDATER)> next update_cursor;
B          
-----------
1          
ij(T11UPDATER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
ij(T11UPDATER)> set connection t11scanner;
ij(T11SCANNER)> autocommit off;
ij(T11SCANNER)> --
--------------------------------------------------
-- try to scan the table, readers are compatible with update lock.
--------------------------------------------------
select * from test_11;
A          |B          
-----------------------
1          |1          
2          |2          
8          |8          
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
ij(T11SCANNER)> --
--------------------------------------------------
-- try to update the table, should timeout
--------------------------------------------------
update test_11 set b=99 where a = 1;
ERROR 40XL1: A lock could not be obtained within the time requested
ij(T11SCANNER)> --
--------------------------------------------------
-- try to update the table, should go through
--------------------------------------------------
update test_11 set b=99 where a = 8;
1 row inserted/updated/deleted
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|TABLE   |2   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |3   |X   |TEST_11     |(1,9)     |GRANT|ACTIVE  
ij(T11SCANNER)> commit;
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
ij(T11SCANNER)> --
--------------------------------------------------
-- try to get an update lock
--------------------------------------------------
get cursor update_cursor2 as
    'select b from test_11 where a=1 for update of b';
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
ij(T11SCANNER)> --
--------------------------------------------------
-- should timeout (other transaction has a shared lock on this row)
--------------------------------------------------
next update_cursor2;
ERROR 40XL1: A lock could not be obtained within the time requested
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
ij(T11SCANNER)> --
--------------------------------------------------
-- should succeed (no other transaction has a shared lock on this row)
--------------------------------------------------
get cursor update_cursor3 as
    'select b from test_11 where a=8 for update of b';
ij(T11SCANNER)> select type, cnt, mode, tabname, lockname, state from lock_table2 order by tabname, type desc, mode, cnt, lockname;
TYPE |CNT  |MODE|TABNAME                                                                                                                         |LOCKNAME            |STATE
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE|1    |IX  |TEST_11                                                                                                                         |Tablelock           |GRANT
TABLE|1    |IX  |TEST_11                                                                                                                         |Tablelock           |GRANT
ROW  |1    |U   |TEST_11                                                                                                                         |(1,7)               |GRANT
ij(T11SCANNER)> next update_cursor3;
B          
-----------
99         
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,9)     |GRANT|ACTIVE  
ij(T11SCANNER)> commit;
ij(T11SCANNER)> close update_cursor2;
ij(T11SCANNER)> close update_cursor3;
ij(T11SCANNER)> set connection t11updater;
ij(T11UPDATER)> commit;
ij(T11UPDATER)> close update_cursor;
ij(T11UPDATER)> set connection t11scanner;
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
ij(T11SCANNER)> --
--==================================================
-- t11scanner gets a read lock
--==================================================

select b from test_11 where a=1;
B          
-----------
1          
ij(T11SCANNER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
ij(T11SCANNER)> --
--------------------------------------------------
-- should succeed (can get an update lock if there is already a shared lock)
--------------------------------------------------
set connection t11updater;
ij(T11UPDATER)> get cursor update_cursor as
    'select b from test_11 where a=1 for update of b';
ij(T11UPDATER)> next update_cursor;
B          
-----------
1          
ij(T11UPDATER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |1   |U   |TEST_11     |(1,7)     |GRANT|ACTIVE  
ij(T11UPDATER)> commit;
ij(T11UPDATER)> close update_cursor;
ij(T11UPDATER)> set connection t11scanner;
ij(T11SCANNER)> commit;
ij(T11SCANNER)> --
--==================================================
-- t11writer gets a write lock
--==================================================

set connection t11writer;
ij(T11WRITER)> autocommit off;
ij(T11WRITER)> update test_11 set b=77 where a=2;
1 row inserted/updated/deleted
ij(T11WRITER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |2   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |3   |X   |TEST_11     |(1,8)     |GRANT|ACTIVE  
ij(T11WRITER)> set connection t11updater;
ij(T11UPDATER)> get cursor update_cursor as
    'select b from test_11 where a=2 for update of b';
ij(T11UPDATER)> select * from lock_table order by tabname, type desc, mode, cnt, lockname;
USERNAME|TRANTYPE       |TYPE    |CNT |MODE|TABNAME     |LOCKNAME  |STATE|STATUS  
----------------------------------------------------------------------------------
APP     |UserTransaction|TABLE   |1   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|TABLE   |2   |IX  |TEST_11     |Tablelock |GRANT|ACTIVE  
APP     |UserTransaction|ROW     |3   |X   |TEST_11     |(1,8)     |GRANT|ACTIVE  
ij(T11UPDATER)> --
----------------------------------------------------
-- should timeout
----------------------------------------------------
next update_cursor;
ERROR 40XL1: A lock could not be obtained within the time requested
ij(T11UPDATER)> --------------------------------------------------------------------------------
-- cleanup.
--------------------------------------------------------------------------------
set connection t11updater;
ij(T11UPDATER)> close update_cursor;
ij(T11UPDATER)> drop table test_11;
ERROR 40XL1: A lock could not be obtained within the time requested
ij(T11UPDATER)> commit;
ij(T11UPDATER)> disconnect;
ij> set connection t11scanner;
ij(T11SCANNER)> disconnect;
ij> set connection t11writer;
ij(T11WRITER)> disconnect;
ij> exit;