gpt4 book ai didi

c# - 此 MySqlConnection 已在使用中

转载 作者:行者123 更新时间:2023-11-30 16:39:06 25 4
gpt4 key购买 nike

我用 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# - 此 MySqlConnection 已在使用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53627973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com