|
21 | 21 | import static org.junit.Assert.assertFalse;
|
22 | 22 | import static org.junit.Assert.assertTrue;
|
23 | 23 |
|
| 24 | +import com.google.cloud.spanner.MockSpannerServiceImpl; |
24 | 25 | import com.google.cloud.spanner.ResultSet;
|
25 | 26 | import com.google.cloud.spanner.SpannerOptions;
|
26 | 27 | import com.google.cloud.spanner.SpannerOptions.SpannerEnvironment;
|
@@ -472,6 +473,59 @@ public void testMultiUseReadWriteAborted() {
|
472 | 473 | assertParent("CloudSpanner.ReadWriteTransaction", "CloudSpannerOperation.Commit", spans);
|
473 | 474 | }
|
474 | 475 |
|
| 476 | + @Test |
| 477 | + public void testSavepoint() { |
| 478 | + Statement statement1 = Statement.of("insert into foo (id) values (1)"); |
| 479 | + Statement statement2 = Statement.of("insert into foo (id) values (2)"); |
| 480 | + mockSpanner.putStatementResult(MockSpannerServiceImpl.StatementResult.update(statement1, 1)); |
| 481 | + mockSpanner.putStatementResult(MockSpannerServiceImpl.StatementResult.update(statement2, 1)); |
| 482 | + |
| 483 | + try (Connection connection = createTestConnection()) { |
| 484 | + connection.setAutocommit(false); |
| 485 | + connection.setReadOnly(false); |
| 486 | + connection.setSavepointSupport(SavepointSupport.ENABLED); |
| 487 | + assertEquals(1L, connection.executeUpdate(statement1)); |
| 488 | + connection.savepoint("test"); |
| 489 | + assertEquals(1L, connection.executeUpdate(statement2)); |
| 490 | + connection.rollbackToSavepoint("test"); |
| 491 | + connection.commit(); |
| 492 | + } |
| 493 | + assertEquals(CompletableResultCode.ofSuccess(), spanExporter.flush()); |
| 494 | + List<SpanData> spans = spanExporter.getFinishedSpanItems(); |
| 495 | + assertContains("CloudSpannerJdbc.ReadWriteTransaction", spans); |
| 496 | + assertContains("CloudSpanner.ReadWriteTransaction", spans); |
| 497 | + // Statement 1 is executed 2 times, because the original transaction needs to be |
| 498 | + // retried after the transaction was rolled back to the savepoint. |
| 499 | + assertContains( |
| 500 | + "CloudSpannerOperation.ExecuteUpdate", |
| 501 | + 2, |
| 502 | + Attributes.of(AttributeKey.stringKey("db.statement"), statement1.getSql()), |
| 503 | + spans); |
| 504 | + assertContains( |
| 505 | + "CloudSpannerOperation.ExecuteUpdate", |
| 506 | + 1, |
| 507 | + Attributes.of(AttributeKey.stringKey("db.statement"), statement2.getSql()), |
| 508 | + spans); |
| 509 | + assertContains("CloudSpannerOperation.Commit", spans); |
| 510 | + |
| 511 | + // Verify that we have two Cloud Spanner transactions, and that these are both children of one |
| 512 | + // JDBC transaction. |
| 513 | + List<SpanData> transactionSpans = |
| 514 | + getSpans("CloudSpanner.ReadWriteTransaction", Attributes.empty(), spans); |
| 515 | + assertEquals(2, transactionSpans.size()); |
| 516 | + assertEquals( |
| 517 | + transactionSpans.get(0).getParentSpanId(), transactionSpans.get(1).getParentSpanId()); |
| 518 | + List<SpanData> jdbcTransactionSpans = |
| 519 | + getSpans("CloudSpannerJdbc.ReadWriteTransaction", Attributes.empty(), spans); |
| 520 | + assertEquals(1, jdbcTransactionSpans.size()); |
| 521 | + assertEquals( |
| 522 | + jdbcTransactionSpans.get(0).getSpanId(), transactionSpans.get(0).getParentSpanId()); |
| 523 | + List<SpanData> commitSpans = |
| 524 | + getSpans("CloudSpannerOperation.Commit", Attributes.empty(), spans); |
| 525 | + assertEquals(1, commitSpans.size()); |
| 526 | + assertEquals(transactionSpans.get(1).getSpanId(), commitSpans.get(0).getParentSpanId()); |
| 527 | + } |
| 528 | + |
475 | 529 | @Test
|
476 | 530 | public void testTransactionTag() {
|
477 | 531 | try (Connection connection = createTestConnection()) {
|
|
0 commit comments