一段很普通的代码,没有并发,没有同时写入,导致出现这种异常:
public static ProcessTimeTrigger GetTimeTrigger(int id) { string sql = string.Format("Select * From process_time_trigger Where id = {0}", id); using (SQLiteConnection conn = GetConnection()) { conn.Open(); using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; SQLiteDataReader dr = cmd.ExecuteReader() if (dr.Read()) { return 。。。 } } return null; } }
它获取连接,读取了一条数据。
public static bool Update(ProcessTimeTrigger timeTrigger) { ProcessTimeTrigger old = GetTimeTrigger(timeTrigger.Id); string sql = "update ..." using (SQLiteConnection conn = GetConnection()) { conn.Open(); using (SQLiteCommand cmd = new SQLiteCommand(sql, conn)) { int changed = cmd.ExecuteNonQuery(); conn.Close(); if (changed > 0) { log.Info(string.Format("更新processTimeTrigger,之后数据:{0}", timeTrigger.ToString())); } return changed > 0; } } }
更新数据,几经搜索,最终发现是SQLiteDataReader
没有关闭导致的。