EFCore.BulkExtensions icon indicating copy to clipboard operation
EFCore.BulkExtensions copied to clipboard

MySql/MariaDB error "The transaction associated with this command is not the connection’s active transaction" occurs when a timeout error occurs

Open KaRom31 opened this issue 1 year ago • 1 comments

Hello, I have a problem when using any method with MySql/MariaDb when a timeout error occurs. When the operation timeout is exceeded, an error "The transaction associated with this command is not the connection’s active transaction" occurs, which hides the real problem. This problem is solved by the "IgnoreCommandTransaction=true" flag, but I would not like to always force this option to be used explicitly.

   at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 309
   at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlRaw(DatabaseFacade databaseFacade, String sql, IEnumerable`1 parameters)
   at EFCore.BulkExtensions.SqlAdapters.MySql.MySqlAdapter.MergeAsync[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress, Boolean isAsync, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.SqlAdapters.MySql.MySqlAdapter.Merge[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, Type type, IEnumerable`1 entities, OperationType operationType, BulkConfig bulkConfig, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkExtensions.BulkDelete[T](DbContext context, IEnumerable`1 entities, BulkConfig bulkConfig, Action`1 progress, Type type)

Code example:

using var db = new MainContext();
{
        try
        {
            var entitiesToDelete = db.Elements
                    .AsNoTracking()
                    .ToList();

            db.BulkDelete(entitiesToDelete);
        }
        catch (Exception ex)
        {

        }
}

Stack Trace:

at System.Threading.Tasks.ValueTask`1.get_Result()
   at MySqlConnector.MySqlCommand.<ExecuteNonQueryAsync>d__78.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at MySqlConnector.MySqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlRaw(DatabaseFacade databaseFacade, String sql, IEnumerable`1 parameters)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlRaw(DatabaseFacade databaseFacade, String sql, Object[] parameters)
   at EFCore.BulkExtensions.SqlAdapters.MySql.MySqlAdapter.<MergeAsync>d__7`1.MoveNext()
   at EFCore.BulkExtensions.SqlAdapters.MySql.MySqlAdapter.Merge[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress)
   at EFCore.BulkExtensions.SqlBulkOperation.Merge[T](DbContext context, Type type, IEnumerable`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, Type type, IEnumerable`1 entities, OperationType operationType, BulkConfig bulkConfig, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkExtensions.BulkDelete[T](DbContext context, IEnumerable`1 entities, BulkConfig bulkConfig, Action`1 progress, Type type)

EFCore.BulkExtensions: 8.0.3 Microsoft.EntityFrameworkCore: 8.0.4 Pomelo.EntityFrameworkCore.MySql: 8.0.2

KaRom31 avatar Apr 19 '24 11:04 KaRom31

I'm still facing the same issue on EFCore.BulkExtensions.MySql: 8.1.3 and Pomelo.EntityFrameworkCore.MySql: 8.0.3.

baltermia avatar Jul 25 '25 08:07 baltermia