Skip to content

Commit 90cd1c5

Browse files
committed
Split transaction statement into two commands. Fixes #774
1 parent 22d8be6 commit 90cd1c5

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/MySqlConnector/Core/StandardEnlistedTransaction.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ protected override void OnStart()
2929
_ => "repeatable read",
3030
};
3131

32-
var consistentSnapshotText = Transaction.IsolationLevel == IsolationLevel.Snapshot ? " with consistent snapshot" : string.Empty;
33-
using var cmd = new MySqlCommand($"set transaction isolation level {isolationLevel}; start transaction{consistentSnapshotText};", Connection);
32+
using var cmd = new MySqlCommand($"set transaction isolation level {isolationLevel};", Connection);
33+
cmd.ExecuteNonQuery();
34+
35+
var consistentSnapshotText = Transaction.IsolationLevel == IsolationLevel.Snapshot ? " with consistent snapshot" : "";
36+
cmd.CommandText = $"start transaction{consistentSnapshotText};";
3437
cmd.ExecuteNonQuery();
3538
}
3639

src/MySqlConnector/MySql.Data.MySqlClient/MySqlConnection.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private async ValueTask<MySqlTransaction> BeginDbTransactionAsync(IsolationLevel
5757
throw new InvalidOperationException("Cannot begin a transaction when already enlisted in a transaction.");
5858
#endif
5959

60-
string isolationLevelValue = isolationLevel switch
60+
var isolationLevelValue = isolationLevel switch
6161
{
6262
IsolationLevel.ReadUncommitted => "read uncommitted",
6363
IsolationLevel.ReadCommitted => "read committed",
@@ -71,10 +71,14 @@ private async ValueTask<MySqlTransaction> BeginDbTransactionAsync(IsolationLevel
7171
_ => throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(isolationLevel))
7272
};
7373

74-
var consistentSnapshotText = isolationLevel == IsolationLevel.Snapshot ? " with consistent snapshot" : string.Empty;
74+
using (var cmd = new MySqlCommand($"set transaction isolation level {isolationLevelValue};", this))
75+
{
76+
await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
7577

76-
using (var cmd = new MySqlCommand($"set transaction isolation level {isolationLevelValue}; start transaction{consistentSnapshotText};", this))
78+
var consistentSnapshotText = isolationLevel == IsolationLevel.Snapshot ? " with consistent snapshot" : "";
79+
cmd.CommandText = $"start transaction{consistentSnapshotText};";
7780
await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
81+
}
7882

7983
var transaction = new MySqlTransaction(this, isolationLevel);
8084
CurrentTransaction = transaction;

tests/SideBySide/Transaction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void DbConnectionTransactionCommand(IsolationLevel inputIsolationLevel, s
9494
using (var trans = connection.BeginTransaction(inputIsolationLevel))
9595
trans.Commit();
9696

97-
var results = connection.Query<string>($"select convert(argument USING utf8) from mysql.general_log where thread_id = @ServerThread and convert(argument using utf8) like '%start transaction%' and argument not like 'select%' order by event_time;", new { m_connection.ServerThread });
97+
var results = connection.Query<string>($"select convert(argument USING utf8) from mysql.general_log where thread_id = @ServerThread and convert(argument using utf8) like 'start transaction%' order by event_time;", new { m_connection.ServerThread });
9898
var lastStartTransactionQuery = results.Last();
9999

100100
Assert.Contains(expectedTransactionIsolationLevel.ToLower(), lastStartTransactionQuery.ToLower());

0 commit comments

Comments
 (0)