Reputation: 91
I created an RecurringJob with Hangfire, but when the job is finish I'm getting the following ERROR:
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()
the code like this:
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
}
}
The code above does not report an error during debugging,I'm confused what's wrong.
Upvotes: 9
Views: 20564
Reputation: 1259
Im my case I just changed AddScoped to AddTransient
e.g
services.AddScoped<DbSession>();
To
services.AddTransient<DbSession>();
Upvotes: 0
Reputation: 411
If you are using Entity Framework and creating a new query using a property from a list of items that has not been resolved yet, then you can get this error.
What fixed it for me was simply adding .ToList()
before using the property :)
In your case it would be:
var someCustomerList = GetSomeCustomerList().ToList() //adding ToList will resolve it;
foreach (var customer in someCustomerList )
{
//do some db operations
}```
Upvotes: 7
Reputation: 614
Although i'm not entirely sure of what's happening, consider doing the following:
Upvotes: -1
Reputation: 978
If you are using asynchronous programming as was my case, make sure that in your flow you are not doing a round trip to DB using async Task and a new asynchronous round trip to DB without use Task, when I noticed and I changed my code, it was fixed
Upvotes: 6