2016-06-10 33 views
0

我有6000個項目和msSql上的每個項目進程並行foreach。 當我開始應用程序拋出一段時間後,達到最大池大小異常。 我試過MaxDegreeOfParallelism設置50,我沒有得到一個錯誤,但應用程序工作很慢。工作單元,存儲庫,實體框架/達到最大池大小/平行foreach

我該怎麼辦這個錯誤。

我的代碼是這樣的;

using RPandUOW.EntityModel; 
using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Linq.Expressions; 

namespace RPandUOW.Repositories 
{ 
    public interface IGenericRepository<T> 
     where T:class 
    { 
     T FindById(object EntityId); 
     IEnumerable<T> Select(Expression<Func<T, bool>> Filter = null); 
     void Insert(T Entity); 
     void Update(T Entity); 
     void Delete(object EntityId); 
     void Delete(T Entity); 
    } 

    public class ShopRepository<T> 
     :IGenericRepository<T> 
     where T:class 
    { 
     private ShopContext _context; 
     private DbSet<T> _dbSet; 
     public ShopRepository(ShopContext Context) 
     { 
      _context = Context; 
      _dbSet = _context.Set<T>(); 
     } 
     public virtual T FindById(object EntityId) 
     { 
      return _dbSet.Find(EntityId); 
     } 
     public virtual IEnumerable<T> Select(Expression<Func<T, bool>> Filter = null) 
     { 
      if (Filter != null) 
      { 
       return _dbSet.Where(Filter); 
      } 
      return _dbSet; 
     } 
     public virtual void Insert(T entity) 
     { 
      _dbSet.Add(entity); 
     } 
     public virtual void Update(T entityToUpdate) 
     { 
      _dbSet.Attach(entityToUpdate); 
      _context.Entry(entityToUpdate).State = EntityState.Modified; 
     } 
     public virtual void Delete(object EntityId) 
     { 
      T entityToDelete = _dbSet.Find(EntityId); 
      Delete(entityToDelete); 
     } 
     public virtual void Delete(T Entity) 
     { 
      if (_context.Entry(Entity).State == EntityState.Detached) //Concurrency için 
      { 
       _dbSet.Attach(Entity); 
      } 
      _dbSet.Remove(Entity); 
     } 
    }} 

using RPandUOW.EntityModel; 
using RPandUOW.Repositories; 
using System; 
using System.Transactions; 

namespace RPandUOW.UnitOfWorks 
{ 
    public interface IUnitOfWork 
     :IDisposable 
    { 
     void Save(); 
     // Başka operasyonlar da tanımlanabilir. 
     // void OpenTransaction(); 
     // void CloseTransaction(); 
     // gibi 
    } 

    public class ShopUnitOfWork 
     :IUnitOfWork 
    { 
     private ShopContext _context = new ShopContext(); 
     private ShopRepository<Category> _categoryRepository; 
     private ShopRepository<Product> _productRepository; 
     private bool _disposed = false; 
     public ShopRepository<Category> CategoryRepository 
     { 
      get 
      { 
       if (_categoryRepository == null) 
        _categoryRepository = new ShopRepository<Category>(_context); 
       return _categoryRepository; 
      } 
     } 
     public ShopRepository<Product> ProductRepository 
     { 
      get 
      { 
       if (_productRepository == null) 
        _productRepository = new ShopRepository<Product>(_context); 
       return _productRepository; 
      } 
     } 
     public void Save() 
     { 
      using (TransactionScope tScope = new TransactionScope()) 
      { 
       _context.SaveChanges(); 
       tScope.Complete(); 
      } 
     } 
     protected virtual void Dispose(bool disposing) 
     { 
      if (!this._disposed) 
      { 
       if (disposing) 
       { 
        _context.Dispose(); 
       } 
      } 
      this._disposed = true; 
     } 
     public void Dispose() 
     { 
      Dispose(true); 
      GC.SuppressFinalize(this); 
     } 
    } 
} 

而且我有兩種方法這樣的原始SQL EXECUT:Context.Database.SqlQuery<string>(sqlQuery)

Parallel.ForEach(items, item => 
{ 
result = bl.run(item); 
}); 
+0

Yeap,這是工作'MaxDegreeOfParallelism = 50' –

回答

0

您可以增加池的大小,增加並行線程數。

我還懷疑你的UoW/Repo模式可能存在實例/關閉問題。你如何實例化你的課程?更多的代碼會有幫助。

+0

嗨,加我的代碼示例。 –

+0

您將每個保存方法作爲一個事務,但它已經是一個事務 - 刪除該事務代碼。同樣如上所述,並行任務不一定適合查詢。你的Run()方法會發生什麼?選擇,然後插入/更新?你並行做了很多這些事情,並且它們都在一個包裝事務中,可能會導致表鎖定。 –

+1

最後,這聽起來像你需要找出性能問題的實際位置(稱爲熱點),然後重構代碼以提高效率 –