diff --git a/apps/desktop/src-tauri/src/app_state.rs b/apps/desktop/src-tauri/src/app_state.rs index 8d5ce40..4b6f45b 100644 --- a/apps/desktop/src-tauri/src/app_state.rs +++ b/apps/desktop/src-tauri/src/app_state.rs @@ -67,14 +67,13 @@ impl AppState { let _ = self.initialize_database(); } - let db_guard = self.database.lock().unwrap(); - if let Some(ref _database) = *db_guard { - // 创建一个新的数据库连接,使用相同的路径 + // 使用静态数据库实例,避免重复初始化 + use std::sync::OnceLock; + static DATABASE_INSTANCE: OnceLock> = OnceLock::new(); + + DATABASE_INSTANCE.get_or_init(|| { Arc::new(Database::new().unwrap()) - } else { - // 如果仍然失败,创建一个新的 - Arc::new(Database::new().unwrap()) - } + }).clone() } /// 用于测试的构造函数 diff --git a/apps/desktop/src-tauri/src/presentation/commands/video_classification_commands.rs b/apps/desktop/src-tauri/src/presentation/commands/video_classification_commands.rs index d19b155..33084b3 100644 --- a/apps/desktop/src-tauri/src/presentation/commands/video_classification_commands.rs +++ b/apps/desktop/src-tauri/src/presentation/commands/video_classification_commands.rs @@ -19,10 +19,10 @@ static QUEUE_INSTANCE: tokio::sync::OnceCell> = to async fn get_queue_instance(state: &AppState) -> Arc { QUEUE_INSTANCE.get_or_init(|| async { let database = state.get_database(); - + let video_repo = Arc::new(VideoClassificationRepository::new(database.clone())); let ai_classification_repo = Arc::new(AiClassificationRepository::new(database.clone())); - let material_repo = Arc::new(MaterialRepository::new(state.get_database().get_connection()).unwrap()); + let material_repo = Arc::new(MaterialRepository::new(database.get_connection()).unwrap()); let gemini_config = Some(GeminiConfig::default()); let service = Arc::new(VideoClassificationService::new(