0

I want to add read uncommitted isolation level.i know it is possible to do with SQL statement. but I want to try with TransactionScope and I tried but not getting any isolation statements on MySQL general logs. Questions 1 does transactionscope class work with MySQL. If yes then how to verify.

Atulya
  • 153
  • 1
  • 11

1 Answers1

2

I want to add read uncommitted isolation level.i know it is possible to do with SQL statement.

The "standard" way to do this would be to use the MySqlConnection.BeginTransaction API:

using (var transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted))
{
    // do stuff
    transaction.Commit();
}

does transactionscope class work with MySQL

Yes, MySQL Connector/NET can support TransactionScope, but not for distributed transactions. It's a known issue that Connector/NET doesn't support XA Transactions with TransactionScope.

If you want to use TransactionScope, then MySQL Connector/NET should inherit its isolation level:

var txOptions = new System.Transactions.TransactionOptions();
txOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;

using(var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions))
using (var connection = new MySqlConnection("... connection string ..."))
{
    connection.Open();
    // ...
    transaction.Complete();
}

If you need to support true distributed transactions with TransactionScope, then switch to MySqlConnector as your ADO.NET driver. Unlike Connector/NET, it fully supports distributed transactions.

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
  • I am sure about the first part of your answer as I am getting general log in Mysql server as `SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED` But in case of TransactionScope I am not getting any log and I do not have distributed transaction it is just select records from single table and I am using MySql.Data package. would you please check for the logs? – Atulya Aug 07 '18 at 16:12
  • @Atulya I see `SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED` `BEGIN` in the general log when I use the `TransactionScope` code I posted above. If you're not seeing this, I suggest you accept this answer, then ask a new question _with your exact code_ and we can investigate further. – Bradley Grainger Aug 07 '18 at 16:27
  • OK, I can do that but please do mention your package(mysql.data) and MySQL version so that I can investigate further and post new if required. – Atulya Aug 07 '18 at 18:33
  • `SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED` was with `TransactionScope` and MySql.Data (aka Connector/NET). With MySqlConnector, you will see `XA START '(guid)'` in the logs because `TransactionScope` uses distributed transactions. – Bradley Grainger Aug 07 '18 at 19:32
  • Got it, but I need you DLL( MySql.Data) version and MySQL version. – Atulya Aug 08 '18 at 04:16
  • I tested with MySql.Data 8.0.11 and MySQL Server 5.7.21 on Windows x64. – Bradley Grainger Aug 08 '18 at 05:11