我有一个数据提供者类。下面是它的简化版本。
namespace EmployeeManager
{
public class EmployeeDataProvider : IEmployeeDataProvider
{
private DataObjects.EmployeeServiceContext databaseContext;
IDbContextTransaction transaction;
public EmployeeDataProvider(DataObjects.EmployeeServiceContext context)
{
this.databaseContext = context;
}
public void CreateEmployee(Id employeeId, string description)
{
transaction = databaseContext.Database.BeginTransaction();
var employeeDto = databaseContext.Employee.Where(x => x.EmployeeId == employeeId).FirstOrDefault();
if (employeeDto == null)
{
databaseContext.Employee.Add(new DataObjects.Employee() { EmployeeId = employeeId, Description = description });
}
}
public void AddOrUpdateEmployeeData(EmployeeData data)
{
// Add or updates multiple Employee related tables
}
public void CommitChanges()
{
if (databaseContext.ChangeTracker.HasChanges())
databaseContext.SaveChanges();
transaction.Commit();
transaction.Dispose();
}
public void RollbackChanges()
{
if (transaction != null)
{
transaction.Rollback();
transaction.Dispose();
}
}
}
}
多个客户端可以调用使用此提供程序的相关服务。其设置如下Startup.cs。
public class Startup
{
public Startup(IHostingEnvironment env)
{
.....
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
......
services.AddTransient(typeof(IEmployeeDataProvider), typeof(EmployeeDataProvider));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
app.UseDeveloperExceptionPage();
app.UseMvc();
}
}
在不同的负载条件下,我在日志文件中看到了很多内容。请注意,没有任何堆栈跟踪指向我的类中的任何方法。
Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory|An exception occurred in the database while iterating the results of a query.
System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception: The wait operation timed out
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMoveNext(Boolean buffer)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](Func`2 operation, Func`2 verifySucceeded, TState state)
at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
我在appsettings.json中的连接字符串如下。
“AppConnectionString”:“Server = MyPC; User ID = myuser;密码= mypassword;数据库= myDb;连接超时= 2000“
错误发生在一分钟内,因此它不像查询等待2000秒查询超时。我是否还需要在代码中的某处设置超时?