此MySqlConnection已在使用中

问题描述 投票:3回答:2

我用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
    }
}

上面的代码在调试期间没有报告错误,我很困惑什么是错的。

c# aspnetboilerplate
2个回答
2
投票

如果您正在使用异步编程,请确保在您的流程中,您没有使用异步任务和数据库进行新的异步往返,而不使用任务,当我注意到并且我更改了代码时,它是固定的


0
投票

虽然我不完全确定发生了什么,但请考虑做以下事情:

  • 每次使用时关闭连接;
  • 考虑在using范围内执行所有数据库操作。这样,每次查询数据库时,都会正确处理连接实例,而不必费心关闭它。
© www.soinside.com 2019 - 2024. All rights reserved.