Cache connection and clear pool after all operations to avoid ObjectDisposedException

This commit is contained in:
Jack251970 2025-06-08 00:14:31 +08:00
parent 2a4b4de3ef
commit c8e82cbd09
2 changed files with 22 additions and 4 deletions

View file

@ -155,6 +155,9 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
return;
}
// Cache connection for pool clean
SqliteConnection connection1 = null;
try
{
// Since some bookmarks may have same favicon id, we need to record them to avoid duplicates
@ -216,8 +219,9 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
}
finally
{
// https://github.com/dotnet/efcore/issues/26580
SqliteConnection.ClearPool(connection);
// Cache connection and clear pool after all operations to avoid issue:
// ObjectDisposedException: Safe handle has been closed.
connection1 = connection;
connection.Close();
connection.Dispose();
}
@ -231,6 +235,11 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
// Delete temporary file
try
{
// https://github.com/dotnet/efcore/issues/26580
if (connection1 != null)
{
SqliteConnection.ClearPool(connection1);
}
File.Delete(tempDbPath);
}
catch (Exception ex)

View file

@ -142,6 +142,9 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
return;
}
// Cache connection for pool clean
SqliteConnection connection1 = null;
try
{
// Since some bookmarks may have same favicon id, we need to record them to avoid duplicates
@ -212,8 +215,9 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
}
finally
{
// https://github.com/dotnet/efcore/issues/26580
SqliteConnection.ClearPool(connection);
// Cache connection and clear pool after all operations to avoid issue:
// ObjectDisposedException: Safe handle has been closed.
connection1 = connection;
connection.Close();
connection.Dispose();
}
@ -227,6 +231,11 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
// Delete temporary file
try
{
// https://github.com/dotnet/efcore/issues/26580
if (connection1 != null)
{
SqliteConnection.ClearPool(connection1);
}
File.Delete(tempDbPath);
}
catch (Exception ex)