该MySqlConnection已被使用

时间:2018-12-05 08:23:48

标签: c# aspnetboilerplate

我用Hangfire创建了一个RecurringJob,但是当作业完成时,出现以下错误:

ERROR 2018-12-05 15:20:05,982 [fc937] Hangfire.AutomaticRetryAttribute - Failed to process the job '81': an exception occurred.
System.InvalidOperationException: This MySqlConnection is already in use.   See https://fl.vu/mysql-conn-reuse
at MySqlConnector.Core.ServerSession.StartQuerying(MySqlCommand command) in    C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 149
at MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(String commandText, MySqlParameterCollection parameterCollection, CommandBehavior  behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 33
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
at MySql.Data.MySqlClient.MySqlTransaction.CommitAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlTransaction.cs:line 25
at MySql.Data.MySqlClient.MySqlTransaction.Commit() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlTransaction.cs:line 12
at Microsoft.EntityFrameworkCore.Storage.RelationalTransaction.Commit()
at Abp.EntityFrameworkCore.Uow.DbContextEfCoreTransactionStrategy.Commit() in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\Uow\DbContextEfCoreTransactionStrategy.cs:line 66
at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.CommitTransaction() in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\Uow\EfCoreUnitOfWork.cs:line 92
at Abp.Domain.Uow.UnitOfWorkBase.Complete() in D:\Github\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkBase.cs:line 256
at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Github\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 68
at Castle.DynamicProxy.AbstractInvocation.Proceed()

这样的代码:

public class TestJob : ITransientDependency
{
    private readonly IDapperRepository<Customer> _customerDapperRepository;
    private readonly IRepository<Customer> _customerRepository;
    private readonly IRepository<User, long> _userRepository;
    public TestJob(...)
    {
     ...
    }
    [UnitOfWork]
    [AutomaticRetry(Attempts = 0)]
    public virtual void ExecuteJob()
    {
       var someCustomerList = GetSomeCustomerList();
       foreach (var customer in someCustomerList )
       {
          //do some db operations
       }
    }
    private List<Customer> GetSomeCustomerList()
    {
       //get some Customer with conditions
    }
}

上面的代码在调试过程中没有报告错误,我很困惑这是怎么回事。

1 个答案:

答案 0 :(得分:2)

如果像我这样使用异步编程,请确保在您的流程中,您没有发现使用异步Task进行DB往返,而没有使用Task进行新的DB异步往返。我的代码是固定的