2017-06-16 89 views
1

我以下列ARTICAL小巧玲瓏與UOW - 不要關閉連接

Dapper Implementation

實現我的DAL與小巧玲瓏的卻是創造了每分貝和每一個數據庫調用的連接。它不重用連接池。我相信我已經關閉並正確處理了連接。

這是如何從服務層調用示例

dtoList = unitofWork.RegionalSettingRepository.GetCurrencySymbols(); unitofWork.Commit(); // To close the connections

這是庫調用

public List<CurrencySymbolDTO> GetCurrencySymbols() 
    { 
     List<CurrencySymbolDTO> dtoList = null; 
     try 
     { 
      string strSQL = "SELECT * from CurrencySymbol"; 
      dtoList = this.Connection.Query<CurrencySymbolDTO>(strSQL, null, transaction: Transaction).ToList(); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ex.Message); 
     } 
     finally 
     { 

     } 
     return dtoList; 
    } 

有人能告訴我爲什麼很多連接都爲每一個創建數據庫調用。 我確實使用了下面的SQL查詢來監視

SELECT DB_NAME(dbid) as DBName, COUNT(dbid) as NumberOfConnections, loginame as LoginName FROM sys.sysprocesses WHERE DB_NAME(dbid) ='database name' GROUP BY dbid, loginame

提前感謝連接數

回答

0

你提到的文章是非常適合與精緻小巧學習UOW;我自己從那篇文章開始研究UoW。我在實現它的同時遇到了類似的問題,以滿足我的業務需求,並修改了一些代碼。你可以在this的答案找到詳細的代碼。

使用此代碼,您可以更好地控制連接/事務;你這樣稱呼它:

隨着交易:

using(DalSession dalSession = new DalSession()) 
{ 
    UnitOfWork unitOfWork = dalSession.UnitOfWork; 
    unitOfWork.Begin(); 
    try 
    { 
     //Your database code here 
     unitOfWork.Commit(); 
    } 
    catch 
    { 
     unitOfWork.Rollback(); 
     throw; 
    } 
} 

沒有交易:

using(DalSession dalSession = new DalSession()) 
{ 
    //Your database code here 
} 
+0

感謝您的答覆,我通過看你的建議,解決了我的問題。 – Denuka