diff --git a/DownKyi.Core/Storage/Database/DbHelper.cs b/DownKyi.Core/Storage/Database/DbHelper.cs index 8b6db35..e4e46fc 100644 --- a/DownKyi.Core/Storage/Database/DbHelper.cs +++ b/DownKyi.Core/Storage/Database/DbHelper.cs @@ -1,20 +1,37 @@ -using System; +using DownKyi.Core.Logging; +using System; +using System.Collections.Generic; using System.Data.SQLite; namespace DownKyi.Core.Storage.Database { public class DbHelper { + private readonly string connStr; private readonly SQLiteConnection conn; + private static readonly Dictionary database = new Dictionary(); + /// /// 创建一个数据库 /// /// 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); + database.Add(connStr, conn); } /// @@ -25,8 +42,20 @@ namespace DownKyi.Core.Storage.Database public DbHelper(string dbPath, string secretKey) { string connStr = $"Data Source={dbPath};Version=3;"; + + if (database.ContainsKey(connStr)) + { + conn = database[connStr]; + + if (conn != null) + { + return; + } + } + conn = new SQLiteConnection(connStr); conn.SetPassword(secretKey); + database.Add(connStr, conn); } /// @@ -57,6 +86,8 @@ namespace DownKyi.Core.Storage.Database if (IsOpen()) { conn.Close(); + + database.Remove(connStr); } } @@ -66,21 +97,29 @@ namespace DownKyi.Core.Storage.Database /// public void ExecuteNonQuery(string sql, Action action = null) { - lock (conn) + try { - Open(); - using (var tr = conn.BeginTransaction()) + lock (conn) { - using (var command = conn.CreateCommand()) + Open(); + using (var tr = conn.BeginTransaction()) { - command.CommandText = sql; - // 添加参数 - action?.Invoke(command.Parameters); - command.ExecuteNonQuery(); + using (var command = conn.CreateCommand()) + { + command.CommandText = sql; + // 添加参数 + action?.Invoke(command.Parameters); + command.ExecuteNonQuery(); + } + tr.Commit(); } - tr.Commit(); } } + catch (SQLiteException e) + { + Utils.Debugging.Console.PrintLine("DbHelper ExecuteNonQuery()发生异常: {0}", e); + LogManager.Error("DbHelper ExecuteNonQuery()", e); + } } /// @@ -90,16 +129,24 @@ namespace DownKyi.Core.Storage.Database /// public void ExecuteQuery(string sql, Action action) { - lock (conn) + try { - Open(); - using (var command = conn.CreateCommand()) + lock (conn) { - command.CommandText = sql; - var reader = command.ExecuteReader(); - action(reader); + Open(); + using (var command = conn.CreateCommand()) + { + command.CommandText = sql; + var reader = command.ExecuteReader(); + action(reader); + } } } + catch (SQLiteException e) + { + Utils.Debugging.Console.PrintLine("DbHelper ExecuteQuery()发生异常: {0}", e); + LogManager.Error("DbHelper ExecuteQuery()", e); + } } } diff --git a/DownKyi/Services/Download/DownloadStorageService.cs b/DownKyi/Services/Download/DownloadStorageService.cs index d0bab9e..92df1e4 100644 --- a/DownKyi/Services/Download/DownloadStorageService.cs +++ b/DownKyi/Services/Download/DownloadStorageService.cs @@ -7,6 +7,16 @@ namespace DownKyi.Services.Download { public class DownloadStorageService { + ~DownloadStorageService() + { + DownloadingDb downloadingDb = new DownloadingDb(); + downloadingDb.Close(); + DownloadedDb downloadedDb = new DownloadedDb(); + downloadedDb.Close(); + DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); + downloadBaseDb.Close(); + } + #region 下载中数据 /// @@ -25,7 +35,7 @@ namespace DownKyi.Services.Download { downloadingDb.Insert(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); } - downloadingDb.Close(); + //downloadingDb.Close(); } /// @@ -40,7 +50,7 @@ namespace DownKyi.Services.Download DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Delete(downloadingItem.DownloadBase.Uuid); - downloadingDb.Close(); + //downloadingDb.Close(); } /// @@ -52,7 +62,7 @@ namespace DownKyi.Services.Download // 从数据库获取数据 DownloadingDb downloadingDb = new DownloadingDb(); Dictionary dic = downloadingDb.QueryAll(); - downloadingDb.Close(); + //downloadingDb.Close(); // 遍历 List list = new List(); @@ -85,7 +95,7 @@ namespace DownKyi.Services.Download DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Update(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); - downloadingDb.Close(); + //downloadingDb.Close(); } #endregion @@ -108,7 +118,7 @@ namespace DownKyi.Services.Download { downloadedDb.Insert(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); } - downloadedDb.Close(); + //downloadedDb.Close(); } /// @@ -123,7 +133,7 @@ namespace DownKyi.Services.Download DownloadedDb downloadedDb = new DownloadedDb(); downloadedDb.Delete(downloadedItem.DownloadBase.Uuid); - downloadedDb.Close(); + //downloadedDb.Close(); } /// @@ -135,7 +145,7 @@ namespace DownKyi.Services.Download // 从数据库获取数据 DownloadedDb downloadedDb = new DownloadedDb(); Dictionary dic = downloadedDb.QueryAll(); - downloadedDb.Close(); + //downloadedDb.Close(); // 遍历 List list = new List(); @@ -168,7 +178,7 @@ namespace DownKyi.Services.Download DownloadedDb downloadedDb = new DownloadedDb(); downloadedDb.Update(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); - downloadedDb.Close(); + //downloadedDb.Close(); } #endregion @@ -189,7 +199,7 @@ namespace DownKyi.Services.Download { downloadBaseDb.Insert(downloadBase.Uuid, downloadBase); } - downloadBaseDb.Close(); + //downloadBaseDb.Close(); } /// @@ -200,7 +210,7 @@ namespace DownKyi.Services.Download { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); downloadBaseDb.Delete(uuid); - downloadBaseDb.Close(); + //downloadBaseDb.Close(); } /// @@ -211,7 +221,7 @@ namespace DownKyi.Services.Download { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); object obj = downloadBaseDb.QueryById(uuid); - downloadBaseDb.Close(); + //downloadBaseDb.Close(); return obj is DownloadBase downloadBase ? downloadBase : null; } @@ -226,7 +236,7 @@ namespace DownKyi.Services.Download DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); downloadBaseDb.Update(downloadBase.Uuid, downloadBase); - downloadBaseDb.Close(); + //downloadBaseDb.Close(); } #endregion