为什么"超时到期"异常抛出?

时间:2017-09-17 10:41:25

标签: c# .net

  

超时已过期。从池中获取连接之前经过的超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。

public class ModelDBContext : DbContext
{
    public ModelDBContext()
    {
    }

    public ModelDBContext(string strConnectionString)
        : base("SqlConnection")
    {
    }

    public DbSet<Users> Users { get; set; }
    public DbSet<Region> Region { get; set; }
    public DbSet<State> State { get; set; }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }
}

存储库实现类:从&#34; ExecuteProcedure&#34;抛出超时错误。

public class RepositoryBase<TResult, TModel> : IDisposable, IRepository<TResult, TModel>
    where TModel : class, new()
    where TResult : class, new()
{
    public RepositoryBase(ModelDBContext context)
    {
        Context = context;
    }

    protected ModelDBContext Context { get; set; }

    protected IEnumerable<TResult> ToList(IDbCommand command)
    {
        using (var reader = command.ExecuteReader())
        {
            var oColl = new List<TResult>();
            while (reader.Read())
            {
                var item = new TResult();
                Map(reader, item);
                oColl.Add(item);
            }
            return oColl;
        }
    }

    public virtual void Map(IDataRecord record, TResult item)
    {
        // method to map reader properties to model 
    }

    public IEnumerable<TResult> ExecuteProcedure(TModel model, string storedProcedure)
    {
        var propertyInfos = typeof(TModel).GetProperties();

        using (var command = CreateCommand())
        {
            command.CommandText = storedProcedure;
            command.CommandType = CommandType.StoredProcedure;

            foreach (var property in propertyInfos)
            {                
                var value = property.GetValue(model, null);
                command.AddParameter(property.Name, value);
            }
            if (command.Connection.State == ConnectionState.Closed)
                command.Connection.Open();
            return ToList(command);
        }
    }

    public IDbCommand CreateCommand()
    {
        var cmd = Context.Database.Connection.CreateCommand();
        return cmd;
    }

    #region dispose or cleanup

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    ~RepositoryBase()
    {
        Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.Context.Dispose();
        }
    }

    #endregion dispose or cleanup
}

方法&#34; ExecuteProcedure&#34;在循环内调用(约1600次)。

0 个答案:

没有答案