2017-07-14 85 views
0

SqLite的連接字符串可以包含使用連接池的選項,例如(僞代碼)如何在C#或VB.NET中使用SqLite連接池?

Dim connectionString = "Data Source=" + Path + ";" + 
            "Version=3;" + 
            "Pooling=True;" + 
            "Max Pool Size=100;" 

我認爲有一些額外的類或工廠方法使用連接池,例如,

dim connectionPool = new SqLiteConnectionPool(connectionString) 

dim firstConnection = connectionPool.getConnection() 
... 
firstConnection.dispose() 

... 
dim secondConnection = connectionPool.getConnection() 

但是,我找不到這樣的班級。

=>如何返回到連接池的連接?

=>如何重新使用先前已返回到池的連接?

https://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki上搜索「pool」沒有給出任何結果。

a)我必須多次調用連接的構造函數嗎?

dim firstConnection = new SqLiteConnection(connectionString) 
... 
firstConnection.dispose() 


dim secondConnection = new SqLiteConnection(connectionString) 

傳遞連接字符串幾次似乎並不直觀我。

b)或者我只會創建連接一次,並在關閉/處理後將其喚醒?

dim connection = new SqLiteConnection(connectionString)) 
using connection 
... 
end using 

connection.open() 
using connection 
... 
end using 
+0

我有一箇中風或是這個代碼是C#和VB的奇怪組合嗎? – maccettura

+0

可能是。 :)只是一些僞代碼,以更好地解釋我的問題。 – Stefan

+1

我想它的工作方式與SQL Server連接池相同。如果啓用了池化,關閉/處置打開的連接會將其返回到池而不是關閉到服務器的連接。因此,您應該簡單地在一個使用塊中打開您的連接,以確保在完成時處理它,即使發生異常也是如此。 – Joe

回答

2

SQLite驅動程序將爲您管理池,但是,有一個關鍵點很多人在使用連接時並未意識到。

晚開,關EARY

的連接不能返回到池中,如果它是開放的。因此,您可以創建連接,準備語句,但是,只有在執行查詢之前立即打開連接。