@@ -412,6 +412,145 @@ public void testExecuteSetAutocommitOff() {
412
412
}
413
413
}
414
414
415
+ @ Test
416
+ public void testSetAutocommitToTrue_inAutoCommitAndNotInTransaction_noop () {
417
+ try (ConnectionImpl subject =
418
+ createConnection (
419
+ ConnectionOptions .newBuilder ()
420
+ .setCredentials (NoCredentials .getInstance ())
421
+ .setUri (URI )
422
+ .build ())) {
423
+ assertThat (subject .isAutocommit (), is (true ));
424
+
425
+ subject .setAutocommit (true );
426
+
427
+ assertTrue (subject .isAutocommit ());
428
+ }
429
+ }
430
+
431
+ @ Test
432
+ public void testSetAutocommitToTrue_inAutoCommitAndInTransaction_noop () {
433
+ try (ConnectionImpl subject =
434
+ createConnection (
435
+ ConnectionOptions .newBuilder ()
436
+ .setCredentials (NoCredentials .getInstance ())
437
+ .setUri (URI )
438
+ .build ())) {
439
+ assertThat (subject .isAutocommit (), is (true ));
440
+ subject .execute (Statement .of ("begin transaction" ));
441
+
442
+ subject .setAutocommit (true );
443
+
444
+ assertTrue (subject .isAutocommit ());
445
+ }
446
+ }
447
+
448
+ @ Test
449
+ public void testSetAutocommitToFalse_inAutoCommitAndNotInTransaction_autocommitModeChanged () {
450
+ try (ConnectionImpl subject =
451
+ createConnection (
452
+ ConnectionOptions .newBuilder ()
453
+ .setCredentials (NoCredentials .getInstance ())
454
+ .setUri (URI )
455
+ .build ())) {
456
+ assertThat (subject .isAutocommit (), is (true ));
457
+
458
+ subject .setAutocommit (false );
459
+
460
+ assertFalse (subject .isAutocommit ());
461
+ }
462
+ }
463
+
464
+ @ Test
465
+ public void testSetAutocommitToFalse_inAutoCommitAndInTransaction_throwsException () {
466
+ try (ConnectionImpl subject =
467
+ createConnection (
468
+ ConnectionOptions .newBuilder ()
469
+ .setCredentials (NoCredentials .getInstance ())
470
+ .setUri (URI )
471
+ .build ())) {
472
+ assertThat (subject .isAutocommit (), is (true ));
473
+ subject .execute (Statement .of ("begin transaction" ));
474
+
475
+ SpannerException exception =
476
+ assertThrows (SpannerException .class , () -> subject .setAutocommit (false ));
477
+ assertEquals (ErrorCode .FAILED_PRECONDITION , exception .getErrorCode ());
478
+ assertTrue (
479
+ exception
480
+ .getMessage ()
481
+ .contains ("Cannot set autocommit while in a temporary transaction" ));
482
+ }
483
+ }
484
+
485
+ @ Test
486
+ public void testSetAutocommitToFalse_notInAutoCommitAndTransactionNotStarted_noop () {
487
+ try (ConnectionImpl subject =
488
+ createConnection (
489
+ ConnectionOptions .newBuilder ()
490
+ .setCredentials (NoCredentials .getInstance ())
491
+ .setUri (URI + ";autocommit=false" )
492
+ .build ())) {
493
+ assertThat (subject .isAutocommit (), is (false ));
494
+
495
+ subject .setAutocommit (false );
496
+
497
+ assertFalse (subject .isAutocommit ());
498
+ }
499
+ }
500
+
501
+ @ Test
502
+ public void testSetAutocommitToFalse_notInAutoCommitAndTransactionStarted_noop () {
503
+ try (ConnectionImpl subject =
504
+ createConnection (
505
+ ConnectionOptions .newBuilder ()
506
+ .setCredentials (NoCredentials .getInstance ())
507
+ .setUri (URI + ";autocommit=false" )
508
+ .build ())) {
509
+ assertThat (subject .isAutocommit (), is (false ));
510
+ subject .executeQuery (Statement .of (SELECT ));
511
+
512
+ subject .setAutocommit (false );
513
+
514
+ assertFalse (subject .isAutocommit ());
515
+ }
516
+ }
517
+
518
+ @ Test
519
+ public void
520
+ testSetAutocommitToTrue_notInAutoCommitAndTransactionNotStarted_autocommitModeChanged () {
521
+ try (ConnectionImpl subject =
522
+ createConnection (
523
+ ConnectionOptions .newBuilder ()
524
+ .setCredentials (NoCredentials .getInstance ())
525
+ .setUri (URI + ";autocommit=false" )
526
+ .build ())) {
527
+ assertThat (subject .isAutocommit (), is (false ));
528
+
529
+ subject .setAutocommit (true );
530
+
531
+ assertTrue (subject .isAutocommit ());
532
+ }
533
+ }
534
+
535
+ @ Test
536
+ public void testSetAutocommitToTrue_notInAutoCommitAndTransactionStarted_throwsException () {
537
+ try (ConnectionImpl subject =
538
+ createConnection (
539
+ ConnectionOptions .newBuilder ()
540
+ .setCredentials (NoCredentials .getInstance ())
541
+ .setUri (URI + ";autocommit=false" )
542
+ .build ())) {
543
+ assertThat (subject .isAutocommit (), is (false ));
544
+ subject .executeQuery (Statement .of (SELECT ));
545
+
546
+ SpannerException exception =
547
+ assertThrows (SpannerException .class , () -> subject .setAutocommit (true ));
548
+ assertEquals (ErrorCode .FAILED_PRECONDITION , exception .getErrorCode ());
549
+ assertTrue (
550
+ exception .getMessage ().contains ("Cannot set autocommit while a transaction is active" ));
551
+ }
552
+ }
553
+
415
554
@ Test
416
555
public void testExecuteGetAutocommit () {
417
556
try (ConnectionImpl subject =
0 commit comments