This commit is contained in:
2026-01-15 16:26:22 -08:00
parent dabc8f9d51
commit cb679235bd
7 changed files with 125 additions and 54 deletions

View File

@@ -323,10 +323,16 @@ class API_folder_store:
self.connection = sqlite3.connect(
str(self.db_path),
check_same_thread=False,
timeout=5.0
timeout=20.0
)
self.connection.row_factory = sqlite3.Row
# Ensure busy_timeout is set immediately for all subsequent ops (including pragmas)
try:
self.connection.execute("PRAGMA busy_timeout = 20000")
except Exception:
pass
# Performance & Size Optimizations
# 1. WAL mode for better concurrency and fewer locks
self.connection.execute("PRAGMA journal_mode=WAL")
@@ -344,10 +350,11 @@ class API_folder_store:
self.connection.execute("PRAGMA foreign_keys = ON")
# Bound how long sqlite will wait on locks before raising.
try:
self.connection.execute("PRAGMA busy_timeout = 5000")
except Exception:
pass
# Already set to 20000 above, no need to lower it.
# try:
# self.connection.execute("PRAGMA busy_timeout = 5000")
# except Exception:
# pass
self._create_tables()
@@ -2623,6 +2630,10 @@ class API_folder_store:
logger.error(f"Error closing database: {e}", exc_info=True)
def __enter__(self):
# Acquire shared lock to serialize access across threads
self._lock_cm = self._with_db_lock()
self._lock_cm.__enter__()
if not self.connection:
self._init_db()
return self
@@ -2631,7 +2642,8 @@ class API_folder_store:
try:
self.close()
finally:
pass
if hasattr(self, "_lock_cm"):
self._lock_cm.__exit__(exc_type, exc_val, exc_tb)
# ============================================================================
@@ -3728,7 +3740,7 @@ class LocalLibrarySearchOptimizer:
return []
try:
with self.db._db_lock:
with self.db._with_db_lock():
cursor = self.db.connection.cursor()
cursor.execute(
"""
@@ -3760,7 +3772,7 @@ class LocalLibrarySearchOptimizer:
return []
try:
with self.db._db_lock:
with self.db._with_db_lock():
cursor = self.db.connection.cursor()
cursor.execute(
"""