2011-05-10 77 views
0

我有一個簡單的while循環來檢查數據庫的插入。如果它的循環太長,我的try catch會得到「最大池大小已達到」錯誤。所以在循環的底部我有一個連接clearallpools();但是這仍然不能解決問題。c#達到SQL最大池大小

while (!quit) 
{ 
connection to database strings timeout=400 

read from database 


connection.clearallpools(); 
} 
+0

把你的陳述分成幾個批次。達到最大池大小是一件非常糟糕的事情。 – 2011-05-10 20:31:20

回答

3

你最有可能保持在循環打開新的連接。

上面的循環打開連接是一個using聲明,然後在循環中使用它。另外還要注意clearallpools去除

using(create new connection) 
{ 
    while (!quit) 
    { 
    connection to database strings timeout=400 

    read from database 


    // connection.clearallpools(); REMOVE THIS!!!! 
    } 
} 
4

也許你不關閉你的連接......你可能需要使用

while(!quit){ 
    //do something here 
    using(var connection = GetMyConnection()){ 
     //do your db reads here 
    } 
    //do validations and something more here 
} 

這將確保您的連接部配置/正常關閉。

此外,您不需要清除您的池。

SQLConnection/DBConnection對象實現IDisposable。你可能想去這些直通

0

你用盡了連接池。在每次讀取之後,您應關閉連接,將其釋放回池中。