/ Check-in [fd4798cb]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add test cases for concurrent transactions and long-lived SELECT statements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent
Files: files | file ages | folders
SHA1: fd4798cb7af263409c20d3cf81236b830bd68570
User & Date: dan 2015-08-27 19:22:56
Wiki:begin-concurrent
Context
2015-08-27
19:57
Add header comments for new methods in pager.c. check-in: 437c7e21 user: dan tags: begin-concurrent
19:22
Add test cases for concurrent transactions and long-lived SELECT statements. check-in: fd4798cb user: dan tags: begin-concurrent
17:42
Fix a problem whereby concurrent transactions would not consider pages read by the transaction before the first write statement. check-in: fc17f731 user: dan tags: begin-concurrent
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to test/concurrent2.test.

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
  } {}
  do_test 9.$tn.4.2 {
    sql2 { DELETE FROM pp WHERE i=1 }
    list [catch { sql1 COMMIT } msg] $msg
  } {0 {}}
}






do_multiclient_test tn {
  do_test 10.$tn.1 {
    sql1 {
      PRAGMA journal_mode = wal;
      CREATE TABLE t1(a);
      CREATE TABLE t2(b);

      INSERT INTO t1 VALUES(1), (2), (3);
      INSERT INTO t2 VALUES(1), (2), (3);

    }
  } {wal}

  do_test 10.$tn.2 {
    sql1 {
      BEGIN CONCURRENT;
        SELECT * FROM t1;
        INSERT INTO t2 VALUES(4);
    }
  } {1 2 3}

  do_test 10.$tn.3 {
    sql2 { INSERT INTO t1 VALUES(4) }
    list [catch {sql1 COMMIT} msg] $msg
  } {1 {database is locked}}

}


























































































finish_test








>
>
>
>
>

|




>


>



|







|



>
|
>
>
>
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


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
  } {}
  do_test 9.$tn.4.2 {
    sql2 { DELETE FROM pp WHERE i=1 }
    list [catch { sql1 COMMIT } msg] $msg
  } {0 {}}
}

#-------------------------------------------------------------------------
# Test that even if a SELECT statement appears before all writes within
# a CONCURRENT transaction, the pages it reads are still considered when
# considering whether or not the transaction may be committed.
#
do_multiclient_test tn {
  do_test 10.$tn.1.1 {
    sql1 {
      PRAGMA journal_mode = wal;
      CREATE TABLE t1(a);
      CREATE TABLE t2(b);
      CREATE TABLE t3(c);
      INSERT INTO t1 VALUES(1), (2), (3);
      INSERT INTO t2 VALUES(1), (2), (3);
      INSERT INTO t3 VALUES(1), (2), (3);
    }
  } {wal}

  do_test 10.$tn.1.2 {
    sql1 {
      BEGIN CONCURRENT;
        SELECT * FROM t1;
        INSERT INTO t2 VALUES(4);
    }
  } {1 2 3}

  do_test 10.$tn.1.3 {
    sql2 { INSERT INTO t1 VALUES(4) }
    list [catch {sql1 COMMIT} msg] $msg
  } {1 {database is locked}}
  sql1 ROLLBACK

  # In this case, because the "SELECT * FROM t1" is first stepped before
  # the "BEGIN CONCURRENT", the pages it reads are not recorded by the
  # pager object. And so the transaction can be committed. Technically
  # this behaviour (the effect of an ongoing SELECT on a BEGIN CONCURRENT
  # transacation) is undefined.
  #
  do_test 10.$tn.2.1 {
    code1 {
      set ::stmt [sqlite3_prepare db "SELECT * FROM t1" -1 dummy]
      sqlite3_step $::stmt
    }
  } {SQLITE_ROW}
  do_test 10.$tn.2.2 {
    sql1 {
      BEGIN CONCURRENT; 
        INSERT INTO t2 VALUES(4);
    }
    code1 {
      set res [list]
      lappend res [sqlite3_column_int $::stmt 0]
      while {[sqlite3_step $::stmt]=="SQLITE_ROW"} {
        lappend res [sqlite3_column_int $::stmt 0]
      }
      sqlite3_finalize $::stmt
      set res
    }
  } {1 2 3 4}
  do_test 10.$tn.2.3 {
    sql2 { INSERT INTO t1 VALUES(5) }
    sql1 COMMIT
  } {}

  # More tests surrounding long-lived prepared statements and concurrent
  # transactions.
  do_test 10.$tn.3.1 {
    sql1 {
      BEGIN CONCURRENT;
        SELECT * FROM t1;
      COMMIT;
    }
    sql1 {
      BEGIN CONCURRENT;
        INSERT INTO t2 VALUES(5);
    }
    sql2 {
      INSERT INTO t1 VALUES(5);
    }
    sql1 COMMIT
    sql3 {
      SELECT * FROM t2;
    }
  } {1 2 3 4 5}
  do_test 10.$tn.3.2 {
    sql1 {
      BEGIN CONCURRENT;
        SELECT * FROM t1;
      ROLLBACK;
    }
    sql1 {
      BEGIN CONCURRENT;
        INSERT INTO t2 VALUES(6);
    }
    sql2 {
      INSERT INTO t1 VALUES(6);
    }
    sql1 COMMIT
    sql3 { SELECT * FROM t2 }
  } {1 2 3 4 5 6}
  do_test 10.$tn.3.3 {
    sql1 { BEGIN CONCURRENT }
    code1 {
      set ::stmt [sqlite3_prepare db "SELECT * FROM t1" -1 dummy]
      sqlite3_step $::stmt
    }
    sql1 {
      INSERT INTO t2 VALUES(7);
      SELECT * FROM t3;
      ROLLBACK;
      BEGIN CONCURRENT;
    }
    sql2 { INSERT INTO t3 VALUES(5) }
    code1 { sqlite3_finalize $::stmt }
    sql1 {
      INSERT INTO t2 VALUES(8);
      COMMIT;
    }
  } {}
}


finish_test