Merge branch 'v1.5.x-fix-sqlite-locked' into v1.5.x

croire 3 years ago
commit e838202f43

@ -1,20 +1,37 @@
using System; using DownKyi.Core.Logging;
using System;
using System.Collections.Generic;
using System.Data.SQLite; using System.Data.SQLite;
namespace DownKyi.Core.Storage.Database namespace DownKyi.Core.Storage.Database
{ {
public class DbHelper public class DbHelper
{ {
private readonly string connStr;
private readonly SQLiteConnection conn; private readonly SQLiteConnection conn;
private static readonly Dictionary<string, SQLiteConnection> database = new Dictionary<string, SQLiteConnection>();
/// <summary> /// <summary>
/// 创建一个数据库 /// 创建一个数据库
/// </summary> /// </summary>
/// <param name="dbPath"></param> /// <param name="dbPath"></param>
public DbHelper(string dbPath) public DbHelper(string dbPath)
{ {
string connStr = $"Data Source={dbPath};Version=3;"; connStr = $"Data Source={dbPath};Version=3;";
if (database.ContainsKey(connStr))
{
conn = database[connStr];
if (conn != null)
{
return;
}
}
conn = new SQLiteConnection(connStr); conn = new SQLiteConnection(connStr);
database.Add(connStr, conn);
} }
/// <summary> /// <summary>
@ -25,8 +42,20 @@ namespace DownKyi.Core.Storage.Database
public DbHelper(string dbPath, string secretKey) public DbHelper(string dbPath, string secretKey)
{ {
string connStr = $"Data Source={dbPath};Version=3;"; string connStr = $"Data Source={dbPath};Version=3;";
if (database.ContainsKey(connStr))
{
conn = database[connStr];
if (conn != null)
{
return;
}
}
conn = new SQLiteConnection(connStr); conn = new SQLiteConnection(connStr);
conn.SetPassword(secretKey); conn.SetPassword(secretKey);
database.Add(connStr, conn);
} }
/// <summary> /// <summary>
@ -57,6 +86,8 @@ namespace DownKyi.Core.Storage.Database
if (IsOpen()) if (IsOpen())
{ {
conn.Close(); conn.Close();
database.Remove(connStr);
} }
} }
@ -65,6 +96,8 @@ namespace DownKyi.Core.Storage.Database
/// </summary> /// </summary>
/// <param name="sql"></param> /// <param name="sql"></param>
public void ExecuteNonQuery(string sql, Action<SQLiteParameterCollection> action = null) public void ExecuteNonQuery(string sql, Action<SQLiteParameterCollection> action = null)
{
try
{ {
lock (conn) lock (conn)
{ {
@ -82,6 +115,12 @@ namespace DownKyi.Core.Storage.Database
} }
} }
} }
catch (SQLiteException e)
{
Utils.Debugging.Console.PrintLine("DbHelper ExecuteNonQuery()发生异常: {0}", e);
LogManager.Error("DbHelper ExecuteNonQuery()", e);
}
}
/// <summary> /// <summary>
/// 执行一条SQL语句并执行提供的操作一般用于查询 /// 执行一条SQL语句并执行提供的操作一般用于查询
@ -89,6 +128,8 @@ namespace DownKyi.Core.Storage.Database
/// <param name="sql"></param> /// <param name="sql"></param>
/// <param name="action"></param> /// <param name="action"></param>
public void ExecuteQuery(string sql, Action<SQLiteDataReader> action) public void ExecuteQuery(string sql, Action<SQLiteDataReader> action)
{
try
{ {
lock (conn) lock (conn)
{ {
@ -101,6 +142,12 @@ namespace DownKyi.Core.Storage.Database
} }
} }
} }
catch (SQLiteException e)
{
Utils.Debugging.Console.PrintLine("DbHelper ExecuteQuery()发生异常: {0}", e);
LogManager.Error("DbHelper ExecuteQuery()", e);
}
}
} }
} }

@ -7,6 +7,16 @@ namespace DownKyi.Services.Download
{ {
public class DownloadStorageService public class DownloadStorageService
{ {
~DownloadStorageService()
{
DownloadingDb downloadingDb = new DownloadingDb();
downloadingDb.Close();
DownloadedDb downloadedDb = new DownloadedDb();
downloadedDb.Close();
DownloadBaseDb downloadBaseDb = new DownloadBaseDb();
downloadBaseDb.Close();
}
#region 下载中数据 #region 下载中数据
/// <summary> /// <summary>
@ -25,7 +35,7 @@ namespace DownKyi.Services.Download
{ {
downloadingDb.Insert(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); downloadingDb.Insert(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading);
} }
downloadingDb.Close(); //downloadingDb.Close();
} }
/// <summary> /// <summary>
@ -40,7 +50,7 @@ namespace DownKyi.Services.Download
DownloadingDb downloadingDb = new DownloadingDb(); DownloadingDb downloadingDb = new DownloadingDb();
downloadingDb.Delete(downloadingItem.DownloadBase.Uuid); downloadingDb.Delete(downloadingItem.DownloadBase.Uuid);
downloadingDb.Close(); //downloadingDb.Close();
} }
/// <summary> /// <summary>
@ -52,7 +62,7 @@ namespace DownKyi.Services.Download
// 从数据库获取数据 // 从数据库获取数据
DownloadingDb downloadingDb = new DownloadingDb(); DownloadingDb downloadingDb = new DownloadingDb();
Dictionary<string, object> dic = downloadingDb.QueryAll(); Dictionary<string, object> dic = downloadingDb.QueryAll();
downloadingDb.Close(); //downloadingDb.Close();
// 遍历 // 遍历
List<DownloadingItem> list = new List<DownloadingItem>(); List<DownloadingItem> list = new List<DownloadingItem>();
@ -85,7 +95,7 @@ namespace DownKyi.Services.Download
DownloadingDb downloadingDb = new DownloadingDb(); DownloadingDb downloadingDb = new DownloadingDb();
downloadingDb.Update(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); downloadingDb.Update(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading);
downloadingDb.Close(); //downloadingDb.Close();
} }
#endregion #endregion
@ -108,7 +118,7 @@ namespace DownKyi.Services.Download
{ {
downloadedDb.Insert(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); downloadedDb.Insert(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded);
} }
downloadedDb.Close(); //downloadedDb.Close();
} }
/// <summary> /// <summary>
@ -123,7 +133,7 @@ namespace DownKyi.Services.Download
DownloadedDb downloadedDb = new DownloadedDb(); DownloadedDb downloadedDb = new DownloadedDb();
downloadedDb.Delete(downloadedItem.DownloadBase.Uuid); downloadedDb.Delete(downloadedItem.DownloadBase.Uuid);
downloadedDb.Close(); //downloadedDb.Close();
} }
/// <summary> /// <summary>
@ -135,7 +145,7 @@ namespace DownKyi.Services.Download
// 从数据库获取数据 // 从数据库获取数据
DownloadedDb downloadedDb = new DownloadedDb(); DownloadedDb downloadedDb = new DownloadedDb();
Dictionary<string, object> dic = downloadedDb.QueryAll(); Dictionary<string, object> dic = downloadedDb.QueryAll();
downloadedDb.Close(); //downloadedDb.Close();
// 遍历 // 遍历
List<DownloadedItem> list = new List<DownloadedItem>(); List<DownloadedItem> list = new List<DownloadedItem>();
@ -168,7 +178,7 @@ namespace DownKyi.Services.Download
DownloadedDb downloadedDb = new DownloadedDb(); DownloadedDb downloadedDb = new DownloadedDb();
downloadedDb.Update(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); downloadedDb.Update(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded);
downloadedDb.Close(); //downloadedDb.Close();
} }
#endregion #endregion
@ -189,7 +199,7 @@ namespace DownKyi.Services.Download
{ {
downloadBaseDb.Insert(downloadBase.Uuid, downloadBase); downloadBaseDb.Insert(downloadBase.Uuid, downloadBase);
} }
downloadBaseDb.Close(); //downloadBaseDb.Close();
} }
/// <summary> /// <summary>
@ -200,7 +210,7 @@ namespace DownKyi.Services.Download
{ {
DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); DownloadBaseDb downloadBaseDb = new DownloadBaseDb();
downloadBaseDb.Delete(uuid); downloadBaseDb.Delete(uuid);
downloadBaseDb.Close(); //downloadBaseDb.Close();
} }
/// <summary> /// <summary>
@ -211,7 +221,7 @@ namespace DownKyi.Services.Download
{ {
DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); DownloadBaseDb downloadBaseDb = new DownloadBaseDb();
object obj = downloadBaseDb.QueryById(uuid); object obj = downloadBaseDb.QueryById(uuid);
downloadBaseDb.Close(); //downloadBaseDb.Close();
return obj is DownloadBase downloadBase ? downloadBase : null; return obj is DownloadBase downloadBase ? downloadBase : null;
} }
@ -226,7 +236,7 @@ namespace DownKyi.Services.Download
DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); DownloadBaseDb downloadBaseDb = new DownloadBaseDb();
downloadBaseDb.Update(downloadBase.Uuid, downloadBase); downloadBaseDb.Update(downloadBase.Uuid, downloadBase);
downloadBaseDb.Close(); //downloadBaseDb.Close();
} }
#endregion #endregion

Loading…
Cancel
Save