最近我浏览了微型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