最近我瀏覽了微型ORM,我喜歡SQLite的Massive,因爲它很簡單。但我現在有一個問題。使用Massite和SQLite時出現「數據庫被鎖定」異常
我只是運行一些select語句後跟一個更新語句,但我得到一個異常。下面是我的代碼:
var tbl = new Cust();
var customers = tbl.All(where: "CustomerID > @0", orderBy: "FirstName", columns: "CustomerID,FirstName", args: 4);
var firstCustomerName= customers.First().FirstName;
var c = tbl.Update(new { FirstName = "Updated2" }, 4); //Exception is here!
//Same happens even when using another object
//var tbl2 = new Cust();
//tbl2.Update(new { FirstName = "UpdatedName" }, 4);//Exception is here!
異常消息爲:「數據庫被鎖定」,在Massive.SQLite源
public virtual int Execute(IEnumerable<DbCommand> commands)
{
var result = 0;
using (var conn = OpenConnection())
{
using (var tx = conn.BeginTransaction())
{
foreach (var cmd in commands)
{
cmd.Connection = conn;
cmd.Transaction = tx;
result += cmd.ExecuteNonQuery();
}
tx.Commit();//Here is the Exception!
}
}
return result;
}
當我看着Massive.SQLite源,我看到下面的方法那麼巨大的永遠不會關閉連接,而是繼續使用using語句來處理連接對象,正如您在上面的代碼中看到的那樣。
上述代碼中的OpenConnection()是每次調用時都會返回一個新連接的方法。
public virtual DbConnection OpenConnection()
{
var result = _factory.CreateConnection();
result.ConnectionString = ConnectionString;
result.Open();
return result;
}
如果情況是海量沒有關閉連接,並根據this SO question SQLite是不擅長的併發連接,我應該關閉它,我怎麼可以關閉它呢? - 連接不會暴露給我。
我想聽聽開發人員使用SQLite的Massive的最佳做法。
SQLite的不應該抱着除非事務是打開的鎖。是持有交易的東西嗎?爲什麼? – 2013-03-26 13:12:49
Massive中的select語句不使用事務,但它看起來像這樣:「var firstCustomerName = customers.First()。FirstName;」鎖定數據庫,以便以下更新不起作用; var c = tbl.Update(new {FirstName =「Updated2」},4); – antew 2013-03-26 14:39:58