@@ -469,6 +469,58 @@ func TestContextCancelBegin(t *testing.T) {
469
469
})
470
470
}
471
471
472
+ func TestContextBeginIsolationLevel (t * testing.T ) {
473
+ runTests (t , dsn , func (dbt * DBTest ) {
474
+ dbt .mustExec ("CREATE TABLE test (v INTEGER)" )
475
+ ctx , cancel := context .WithCancel (context .Background ())
476
+ defer cancel ()
477
+
478
+ tx1 , err := dbt .db .BeginTx (ctx , & sql.TxOptions {
479
+ Isolation : sql .LevelRepeatableRead ,
480
+ })
481
+ if err != nil {
482
+ dbt .Fatal (err )
483
+ }
484
+
485
+ tx2 , err := dbt .db .BeginTx (ctx , & sql.TxOptions {
486
+ Isolation : sql .LevelReadCommitted ,
487
+ })
488
+ if err != nil {
489
+ dbt .Fatal (err )
490
+ }
491
+
492
+ _ , err = tx1 .ExecContext (ctx , "INSERT INTO test VALUES (1)" )
493
+ if err != nil {
494
+ dbt .Fatal (err )
495
+ }
496
+
497
+ var v int
498
+ row := tx2 .QueryRowContext (ctx , "SELECT COUNT(*) FROM test" )
499
+ if err := row .Scan (& v ); err != nil {
500
+ dbt .Fatal (err )
501
+ }
502
+ // Because writer transaction wasn't commited yet, it should be available
503
+ if v != 0 {
504
+ dbt .Errorf ("expected val to be 0, got %d" , v )
505
+ }
506
+
507
+ err = tx1 .Commit ()
508
+ if err != nil {
509
+ dbt .Fatal (err )
510
+ }
511
+
512
+ row = tx2 .QueryRowContext (ctx , "SELECT COUNT(*) FROM test" )
513
+ if err := row .Scan (& v ); err != nil {
514
+ dbt .Fatal (err )
515
+ }
516
+ // Data written by writer transaction is already commited, it should be selectable
517
+ if v != 1 {
518
+ dbt .Errorf ("expected val to be 1, got %d" , v )
519
+ }
520
+ tx2 .Commit ()
521
+ })
522
+ }
523
+
472
524
func TestContextBeginReadOnly (t * testing.T ) {
473
525
runTests (t , dsn , func (dbt * DBTest ) {
474
526
dbt .mustExec ("CREATE TABLE test (v INTEGER)" )
0 commit comments