|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
| 4 | +using System.Threading.Tasks; |
4 | 5 | using System.Transactions;
|
5 | 6 | using Dapper;
|
6 | 7 | using MySql.Data.MySqlClient;
|
@@ -533,6 +534,58 @@ public void TwoSimultaneousConnectionsThrowsWithNonXaTransactions()
|
533 | 534 | }
|
534 | 535 | }
|
535 | 536 |
|
| 537 | + [Fact] |
| 538 | + public void SimultaneousConnectionsWithTransactionScopeReadCommittedWithNonXaTransactions() |
| 539 | + { |
| 540 | + var connectionString = AppConfig.ConnectionString; |
| 541 | +#if !BASELINE |
| 542 | + connectionString += ";UseXaTransactions=False"; |
| 543 | +#endif |
| 544 | + |
| 545 | + // from https://github.com/mysql-net/MySqlConnector/issues/605 |
| 546 | + using (var connection = new MySqlConnection(connectionString)) |
| 547 | + { |
| 548 | + connection.Open(); |
| 549 | + connection.Execute(@"DROP TABLE IF EXISTS orders; |
| 550 | + CREATE TABLE `orders`( |
| 551 | + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 552 | + `description` VARCHAR(50), |
| 553 | + PRIMARY KEY (`id`) |
| 554 | + );"); |
| 555 | + } |
| 556 | + |
| 557 | + var task = Task.Run(() => UseTransaction()); |
| 558 | + UseTransaction(); |
| 559 | + task.Wait(); |
| 560 | + |
| 561 | + void UseTransaction() |
| 562 | + { |
| 563 | + // This TransactionScope may be overly configured, but let's stick with the one I am actually using |
| 564 | + using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, |
| 565 | + new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }, |
| 566 | + TransactionScopeAsyncFlowOption.Enabled)) |
| 567 | + { |
| 568 | + using (var connection = new MySqlConnection(connectionString)) |
| 569 | + using (var command = connection.CreateCommand()) |
| 570 | + { |
| 571 | + command.CommandText = @"SELECT MAX(id) FROM orders FOR UPDATE;"; |
| 572 | + connection.Open(); |
| 573 | + command.ExecuteScalar(); |
| 574 | + } |
| 575 | + |
| 576 | + using (var connection = new MySqlConnection(connectionString)) |
| 577 | + using (var command = connection.CreateCommand()) |
| 578 | + { |
| 579 | + command.CommandText = @"INSERT INTO orders (description) VALUES ('blabla'), ('blablabla');"; |
| 580 | + connection.Open(); |
| 581 | + command.ExecuteNonQuery(); |
| 582 | + } |
| 583 | + |
| 584 | + transactionScope.Complete(); |
| 585 | + } |
| 586 | + } |
| 587 | + } |
| 588 | + |
536 | 589 | [Fact]
|
537 | 590 | public void TwoDifferentConnectionStringsThrowsWithNonXaTransactions()
|
538 | 591 | {
|
@@ -594,3 +647,4 @@ public void CannotMixNonXaAndXaTransactions()
|
594 | 647 | DatabaseFixture m_database;
|
595 | 648 | }
|
596 | 649 | }
|
| 650 | + |
0 commit comments