|
23 | 23 |
|
24 | 24 | import com.google.cloud.Timestamp;
|
25 | 25 | import com.google.cloud.spanner.AbortedException;
|
| 26 | +import com.google.cloud.spanner.BatchClient; |
| 27 | +import com.google.cloud.spanner.BatchReadOnlyTransaction; |
26 | 28 | import com.google.cloud.spanner.Database;
|
27 | 29 | import com.google.cloud.spanner.DatabaseClient;
|
28 | 30 | import com.google.cloud.spanner.ErrorCode;
|
29 | 31 | import com.google.cloud.spanner.IntegrationTest;
|
30 | 32 | import com.google.cloud.spanner.IntegrationTestEnv;
|
31 | 33 | import com.google.cloud.spanner.Key;
|
| 34 | +import com.google.cloud.spanner.KeySet; |
32 | 35 | import com.google.cloud.spanner.Mutation;
|
| 36 | +import com.google.cloud.spanner.PartitionOptions; |
33 | 37 | import com.google.cloud.spanner.ReadContext;
|
34 | 38 | import com.google.cloud.spanner.ResultSet;
|
35 | 39 | import com.google.cloud.spanner.SpannerException;
|
@@ -349,4 +353,126 @@ public Void run(TransactionContext transaction) throws SpannerException {
|
349 | 353 | .getLong(0))
|
350 | 354 | .isEqualTo(2);
|
351 | 355 | }
|
| 356 | + |
| 357 | + private void doNestedRwTransaction() { |
| 358 | + client |
| 359 | + .readWriteTransaction() |
| 360 | + .run( |
| 361 | + new TransactionCallable<Void>() { |
| 362 | + @Override |
| 363 | + public Void run(TransactionContext transaction) throws SpannerException { |
| 364 | + client |
| 365 | + .readWriteTransaction() |
| 366 | + .run( |
| 367 | + new TransactionCallable<Void>() { |
| 368 | + @Override |
| 369 | + public Void run(TransactionContext transaction) throws Exception { |
| 370 | + return null; |
| 371 | + } |
| 372 | + }); |
| 373 | + |
| 374 | + return null; |
| 375 | + } |
| 376 | + }); |
| 377 | + } |
| 378 | + |
| 379 | + @Test |
| 380 | + public void nestedReadWriteTxnThrows() { |
| 381 | + try { |
| 382 | + doNestedRwTransaction(); |
| 383 | + fail("Expected exception"); |
| 384 | + } catch (SpannerException e) { |
| 385 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INTERNAL); |
| 386 | + assertThat(e.getMessage()).contains("not supported"); |
| 387 | + } |
| 388 | + } |
| 389 | + |
| 390 | + @Test |
| 391 | + public void nestedReadOnlyTxnThrows() { |
| 392 | + try { |
| 393 | + client |
| 394 | + .readWriteTransaction() |
| 395 | + .run( |
| 396 | + new TransactionCallable<Void>() { |
| 397 | + @Override |
| 398 | + public Void run(TransactionContext transaction) throws SpannerException { |
| 399 | + client |
| 400 | + .readOnlyTransaction() |
| 401 | + .getReadTimestamp(); |
| 402 | + |
| 403 | + return null; |
| 404 | + } |
| 405 | + }); |
| 406 | + fail("Expected exception"); |
| 407 | + } catch (SpannerException e) { |
| 408 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INTERNAL); |
| 409 | + assertThat(e.getMessage()).contains("not supported"); |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + @Test |
| 414 | + public void nestedBatchTxnThrows() { |
| 415 | + try { |
| 416 | + client |
| 417 | + .readWriteTransaction() |
| 418 | + .run( |
| 419 | + new TransactionCallable<Void>() { |
| 420 | + @Override |
| 421 | + public Void run(TransactionContext transaction) throws SpannerException { |
| 422 | + BatchClient batchClient = env.getTestHelper().getBatchClient(db); |
| 423 | + BatchReadOnlyTransaction batchTxn = batchClient |
| 424 | + .batchReadOnlyTransaction(TimestampBound.strong()); |
| 425 | + batchTxn.partitionReadUsingIndex( |
| 426 | + PartitionOptions.getDefaultInstance(), |
| 427 | + "Test", |
| 428 | + "Index", |
| 429 | + KeySet.all(), |
| 430 | + Arrays.asList("Fingerprint")); |
| 431 | + |
| 432 | + return null; |
| 433 | + } |
| 434 | + }); |
| 435 | + fail("Expected exception"); |
| 436 | + } catch (SpannerException e) { |
| 437 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INTERNAL); |
| 438 | + assertThat(e.getMessage()).contains("not supported"); |
| 439 | + } |
| 440 | + } |
| 441 | + |
| 442 | + @Test |
| 443 | + public void nestedSingleUseReadTxnThrows() { |
| 444 | + try { |
| 445 | + client |
| 446 | + .readWriteTransaction() |
| 447 | + .run( |
| 448 | + new TransactionCallable<Void>() { |
| 449 | + @Override |
| 450 | + public Void run(TransactionContext transaction) throws SpannerException { |
| 451 | + client.singleUseReadOnlyTransaction(); |
| 452 | + |
| 453 | + return null; |
| 454 | + } |
| 455 | + }); |
| 456 | + fail("Expected exception"); |
| 457 | + } catch (SpannerException e) { |
| 458 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INTERNAL); |
| 459 | + assertThat(e.getMessage()).contains("not supported"); |
| 460 | + } |
| 461 | + } |
| 462 | + |
| 463 | + @Test |
| 464 | + public void nestedTxnSucceedsWhenAllowed() { |
| 465 | + client |
| 466 | + .readWriteTransaction() |
| 467 | + .allowNestedTransaction() |
| 468 | + .run( |
| 469 | + new TransactionCallable<Void>() { |
| 470 | + @Override |
| 471 | + public Void run(TransactionContext transaction) throws SpannerException { |
| 472 | + client.singleUseReadOnlyTransaction(); |
| 473 | + |
| 474 | + return null; |
| 475 | + } |
| 476 | + }); |
| 477 | + } |
352 | 478 | }
|
0 commit comments