我正在使用其中包含事务的递归函数,但是该事务导致“由于 ROLLBACK 导致回调请求的查询中止中止”错误。这是我的代码:
public Dictionary<int, string> GetFolders(string connectstring)
{
var connection = new SQLiteConnection(connectstring);
connection.Open();
Dictionary<int, string> folderdictionary = new Dictionary<int, string>();
using (var transaction = connection.BeginTransaction())
{
using (SQLiteCommand command = connection.CreateCommand())
{
command.CommandText = "select * from moz_bookmarks AS bookmarks WHERE type=2";
SQLiteDataReader commandread = command.ExecuteReader();
int current_id = 0;
List<string> currentfolders = new List<string>();
while (commandread.Read()) // <--------------- thrown here
{
var title = commandread.GetString(5);
current_id = commandread.GetInt32(0);
var parent = commandread.GetInt32(3);
List<string> folders = new List<string>();
folder = GetChild(connection, (string)commandread["title"], commandread.GetInt32(3), ref folders, true);
currentfolders.Add(folder);
folderdictionary.Add(current_id, folder);
folder = "";
}
}
transaction.Commit();
}
connection.Close();
return folderdictionary;
}
递归本身在 GetChild 函数中:
private static string GetChild(SQLiteConnection sqlite_connection2, string title, int id, ref List<string> folder, bool recursive, SQLiteTransaction transaction)
{
if (recursive)
folder.Add(title);
using (SQLiteCommand folder_get = sqlite_connection2.CreateCommand())
{
folder_get.Connection = sqlite_connection2;
folder_get.CommandText = "SELECT * FROM moz_bookmarks WHERE id = " + id;
SQLiteDataReader get_folders = folder_get.ExecuteReader();
while (get_folders.Read())
{
if (get_folders.GetInt32(1) == 1)
{
break;
}
else if (get_folders.GetInt32(1) == 2 && get_folders.GetInt32(0) != 1)
{
folder.Add(get_folders["title"].ToString());
if (get_folders.GetInt32(3) != 0 && get_folders.GetInt32(0) != 1)
{
GetChild(sqlite_connection2, (string)get_folders["title"], get_folders.GetInt32(3), ref folder, false);
}
break;
}
}
if (recursive)
folder.Reverse();
get_folders.Close();
}
return string.Join("\\", folder);
}
如果没有 SQLiteTransaction,GetChild 会造成巨大的内存泄漏并最终引发 OutOfMemoryException。