Skip to content

Commit 95d7441

Browse files
committed
Fix transaction isolation level tests for MySQL 5.6.
MySQL 5.6's general_log table only has second resolution, so ordering by event_time doesn't put most recent events first. Instead, assume the general_log table doesn't have many entries, select them all, and use the last (i.e., most recent) one. MariaDB puts both commands in the same entry in the general_log table, so we can't assume 'start transaction' will be at the beginning.
1 parent 1144249 commit 95d7441

File tree

1 file changed

+10
-39
lines changed

1 file changed

+10
-39
lines changed

tests/SideBySide/Transaction.cs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,16 @@ public void DbConnectionCommit()
6464
public void DbConnectionIsolationLevel(IsolationLevel inputIsolationLevel, string expectedTransactionIsolationLevel)
6565
{
6666
DbConnection connection = m_connection;
67-
m_connection.Execute(@"set global log_output = 'table';");
68-
m_connection.Execute(@"set global general_log = 1;");
67+
connection.Execute(@"set global log_output = 'table';");
68+
connection.Execute(@"set global general_log = 1;");
6969
using (var trans = connection.BeginTransaction(inputIsolationLevel))
70-
{
7170
trans.Commit();
72-
}
7371

74-
m_connection.Execute(@"set global general_log = 0;");
75-
var results = connection.Query<string>($"select convert(argument USING utf8) from mysql.general_log where thread_id = {m_connection.ServerThread} order by event_time desc limit 10;");
76-
var lastIsolationLevelQuery = results.First(x => x.ToLower().Contains("isolation"));
72+
connection.Execute(@"set global general_log = 0;");
7773

78-
if (IsMySqlAndVersionLessThan57(m_connection.ServerVersion))
79-
{
80-
Assert.Contains("serializable", lastIsolationLevelQuery.ToLower());
81-
return;
82-
}
74+
// we could use @tx_isolation (in MySQL 5.6/5.7) and @transaction_isolation (in MySQL 5.7/8.0), but would require a different query for different server versions
75+
var results = connection.Query<string>($"select convert(argument USING utf8) from mysql.general_log where thread_id = @ServerThread and convert(argument using utf8) like '%isolation level%' and argument not like 'select%' order by event_time;", new { m_connection.ServerThread });
76+
var lastIsolationLevelQuery = results.Last();
8377

8478
Assert.Contains(expectedTransactionIsolationLevel.ToLower(), lastIsolationLevelQuery.ToLower());
8579
}
@@ -95,22 +89,13 @@ public void DbConnectionIsolationLevel(IsolationLevel inputIsolationLevel, strin
9589
public void DbConnectionTransactionCommand(IsolationLevel inputIsolationLevel, string expectedTransactionIsolationLevel)
9690
{
9791
DbConnection connection = m_connection;
98-
m_connection.Execute(@"set global log_output = 'table';");
99-
m_connection.Execute(@"set global general_log = 1;");
92+
connection.Execute(@"set global log_output = 'table';");
93+
connection.Execute(@"set global general_log = 1;");
10094
using (var trans = connection.BeginTransaction(inputIsolationLevel))
101-
{
10295
trans.Commit();
103-
}
10496

105-
m_connection.Execute(@"set global general_log = 0;");
106-
var results = connection.Query<string>($"select convert(argument USING utf8) from mysql.general_log where thread_id = {m_connection.ServerThread} order by event_time desc limit 10;");
107-
var lastStartTransactionQuery = results.First(x => x.ToLower().Contains("start"));
108-
109-
if (IsMySqlAndVersionLessThan57(m_connection.ServerVersion))
110-
{
111-
Assert.Contains("start transaction", lastStartTransactionQuery.ToLower());
112-
return;
113-
}
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 });
98+
var lastStartTransactionQuery = results.Last();
11499

115100
Assert.Contains(expectedTransactionIsolationLevel.ToLower(), lastStartTransactionQuery.ToLower());
116101
}
@@ -382,20 +367,6 @@ public async Task DisposeAsync()
382367
}
383368
#endif
384369

385-
private bool IsMySqlAndVersionLessThan57(string currentVersionStr)
386-
{
387-
var version = new Version("5.7");
388-
Version currentVersion = null;
389-
390-
if (Version.TryParse(currentVersionStr, out currentVersion))
391-
{
392-
var result = version.CompareTo(currentVersion);
393-
return result > 0;
394-
}
395-
396-
return false;
397-
}
398-
399370
readonly TransactionFixture m_database;
400371
readonly MySqlConnection m_connection;
401372
}

0 commit comments

Comments
 (0)